Csharp windows application MySql connection - c#

I cannot connect to MySql from my C# windows form application. I also have posted a question already in
invalid username/password
but got nothing solution.
Suppose my website name is www.google.com and cpanel user name is imrancpanel, my cpanel password is imranpd, I create a database imrandb, create a database user imrandbuser and check all permissions to this user for this website. whats the problem in this code?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace CSharpMySql
{
class Program
{
static string getString(string msg)
{
Console.Write(msg);
return Console.ReadLine();
}
static void Main(string[] args)
{
string myConnectionString = #"server=google.com;userid=imrancpanel_imrandbuser;
password=imranpd;database=imrandb";
MySqlConnection con = null;
MySqlDataReader reader = null;
string cretablestr = "CREATE TABLE IF NOT EXISTS AutoCaptchaTable (LoginID char(25), Password char(25), ActivationStatus char(25), NofTransactions int );"
try
{
//MySqlConnection
conn = new MySqlConnection(myConnectionString);
conn.Open();
MessageBox.Show("OK");
conn.Close();
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1042:
MessageBox.Show("Can't get hostname address. Check your internet connection. If does not solve, contact Administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password");
break;
}
}
}
}
}

Most website hosters don't allow remote access to MySQL
only with localhost
you need to create a webservice with php to allow that or get an alternative

Enable remote access in cpanel by adding host(IP OR DOMIAN etc)

Related

Connection to mysql database does not work c #

I'm trying to connect to my mysql database (I'm hosted by infinityfree.net), entering these credentials:
connection = new MySqlConnection("Server=sql306.epizy.com; Port=3306; Database=epiz_27674946_db1; Uid=epiz_27674946; Pwd=**********; ");
But I get this exception:
Unable to connect to any of the specified mysql hosts
I don't know why this happens, the credentials should be right...
Can you help me?
Full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace sharetru
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MySqlConnection connection;
private void Form1_Load(object sender, EventArgs e)
{
try
{
connection = new MySqlConnection("Server=sql306.epizy.com; Port=3306; Database=epiz_27674946_db1; Uid=epiz_27674946; Pwd=*******; ");
connection.Open();
if (connection.State == ConnectionState.Open)
{
label1.Text = "Connected";
label1.ForeColor = Color.Green;
}
else
{
label1.Text = "Not Connected";
label1.ForeColor = Color.Red;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Regarding this service (infinityfree) it is not possible to access mysql from desktop applications (outside the infinityfree host)
https://support.infinityfree.net/mysql/how-to-connect-with-mysql/
The code looks correct. The issue probably is in the connection string details.
Are you able to connect using the following connection details via some GUI tool?
Workbench for example https://dev.mysql.com/downloads/workbench/

unable to connect to other machine's mysql server using C#?

i need to connect to the other machine's mysql server using C# coding,below is my coding:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace c_mysql
{
class Program
{
static void Main(string[] args)
{
string connection = "SERVER=192.168.1.5; Database=b2b; Uid=root;";
MySqlConnection con = new MySqlConnection(connection);
con.Open();
MySqlCommand cmd = new MySqlCommand("select * from area",con);
MySqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Console.WriteLine("connection successfull");
}
else
{
Console.WriteLine("not connected...");
}
}
}
}
the ip address 192.168.1.5 is the ip address of other computer which is in LAN,I am able to connect through 192.168.1.5/phpmyadmin in url bar but when connecting through C# coding it says,
Host 'pc1' is not allowed to connect to this MySQL server
Pc 1 is my pc on which i am coding.
please help me.
Enable Remote Root Access to MySql. click here for more details
Snippet taken from above:
create a new HOST for root and allow root to login from anywhere.
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
mysql> exit

Connection Strings and Dapper

I am trying to connect to an SQL db to extract data and I am using Dapper for the process. I assume I am having some problems with the connection string; I am trying to access a remote sql server 2012 db but my connection keeps failing:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data.SqlClient;
namespace Data_Connection
{
public static class Program
{
static void Main(string[] args)
{
string connetionString = null;
SqlConnection cnn ;
connetionString = "Server=datanet.local.net/datadb; Database=ddata;Trusted_Connection=True";
cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
Console.WriteLine("Connection Open !");
cnn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Can not open connection !");
}
}
}
}
I have never tried connecting to an sql db using c# and I am fairly new to the environment. Any help would be appreciated
Thanks!

Connecting to SQL Server database using default connection string from C#

I've been trying to make a connection with my SQL Server database (Mynewdatabase) using the default connection string that was generated (supposedly) when I added the data source for this project. However, I'm thus far unable to make a connection: I'm getting an error that "the server was not found or was not accessible".
This my code. Thanks so much for any help you can offer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace Parsevcf2sql
{
class Program
{
static void Main(string[] args)
{
// here is where I get what I believe is an appropriate connection string
string cs = Parsevcf2sql.Properties.Settings.Default.MynewdatabaseConnectionString;
SqlConnection myconnection = new SqlConnection(cs);
Console.WriteLine(cs);
try
{
myconnection.Open();
Console.WriteLine("This worked!");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
This might help you out, change localhost to your address and database name as well.. But Please check sql server must be in running mode
{
// connection string!
SqlConnection myConn = new SqlConnection("Server=localhost\\SQLEXPRESS;Integrated security=SSPI;database=Mynewdatabase;");
try
{
myConn.Open();
Console.WriteLine(myConn );
}
catch (System.Exception)
{
// some exception
}
finally
{
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
}
myConn.Dispose();
}
}
or check out this tutorial

Can't able to connect to Database

I have installed the MySQL server using XAMMP.Created a new database with some data using phpmyadmin.Then i tried to connect to database using this code.But it did not connect.It shows me an error.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Testing!");
SqlConnection myco = new SqlConnection("server=localhost;User Id=root;database=customer;Trusted_Connection=yes");
try
{
myco.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.ReadKey();
}
}
}
Error:
http://pastebin.com/MyEtk4w6
You have MySql then you need to use the classes for MySql not the ones used for SqlServer
MySqlConnection myco = new MySqlConnection("Server=localhost;Database=customer;" +
"Uid=username;Pwd=password;");
Also the connection string for MySql is different
Server=localhost;Database=customer;Uid=username;Pwd=password;
The connection strings for MySql could be very numerous, you should check the correct one for your requirement here at connectionstrings.com
It's because you're using a SqlConnection object which is for Microsoft SQL Server.
Use a MySQLConnection instead.

Categories

Resources