0
string server = "192.168.1.28";
string database = "xxxxxx";
string uid = "yyyyy";
string password = "zzzzz";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

SqlConnection conn = new SqlConnection(connectionString);
ada = new SqlDataAdapter("select * from markdetail", conn);
ds = new DataSet();
ada.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];

I am using this code to connect mysql database to my c# project.But it shows error message like this enter image description here

How to solve this anyone have idea?

user9371102
  • 1,278
  • 3
  • 21
  • 43
r.vengadesh
  • 1,721
  • 3
  • 20
  • 36
  • 1
    On which line? Did you debug your code? Are you sure your connection string is right? – Soner Gönül Jul 26 '13 at 08:53
  • Probably resolved here [enter link description here][1] [1]: http://stackoverflow.com/questions/6454809/sqlexception-was-unhandled – Xavier W. Jul 26 '13 at 08:54
  • `ada.Fill(ds)` in this line @SonerGönül – r.vengadesh Jul 26 '13 at 08:54
  • 1
    Like the error describes: your server might not be found or accessible. Check if you can connect to it outside of Visual Studio? – Recipe Jul 26 '13 at 08:54
  • I've never tried `mysql` in c#, but I think you are using a `SqlConnection` in `System.Data.SqlClient` which is used for `SQL Server`? – King King Jul 26 '13 at 08:56
  • Of course that line, in this line the `SqlDataAdapter` establishes the connection to the server. I guess your credentials are wrong. – jAC Jul 26 '13 at 08:56
  • Ensure that you can connect to the database from outside of the application, ways to test this would include, setting up an ODBC or accessing Microsoft SQL Server Managment Studio, using the same above server / login credentials. – Hexie Jul 26 '13 at 09:23
  • Using this URL ( 'http://stackoverflow.com/questions/10479763/how-to-get-the-connection-string-from-a-database' ) it will help you check the SQL connection string from an already connected session through SQL Server Managment Studio. (i.e. you can confirm if your connection string is correct). – Hexie Jul 26 '13 at 09:32

4 Answers4

3

you need to download mysql drive for mysql connection. Your code is showing that you are trying to connect with sql instead. You can download mysql driver for.net from below link

http://dev.mysql.com/downloads/connector/net/

usman allam
  • 274
  • 1
  • 5
  • @JanesAbouChleih You can not use sqlobjetc to connect to mysql. If add as refernce the MySql dll may have to use msqconnection and all these. Else must use System.Data.Common. – kostas ch. Jul 26 '13 at 08:59
  • Generaly is needed to add as reference MySql.dll. I don't know in case of obbc. – kostas ch. Jul 26 '13 at 09:02
0

use the mysql.data library

here you can find all that you required.

also not forget

Login your cpanel and click on Remote MYSQL and then add Remote Database Access Hosts put % for giving access to all host.

Ajit Bhandari
  • 1,119
  • 14
  • 21
0

Download NuGet, and add the SQL package .

0

May be some of your services had stopped running , Please go to sqlserverconfiguration manager and check if SqlServer(MSSQLSERVER) service is running ?If not enable it .

Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools

SQL Server Configuration Manager >> SQL Server Services, and check if SQL Server service status is “Running”.

Suraj Singh
  • 4,041
  • 1
  • 21
  • 36