Connect to MySQL via C# .NET (Visual studion 2008)

Which client library can we use to connect to a MySQL 5.0 database from MS .NET Visual Studio 2008 code?

I use
using System.Data.OracleClient;;
using System.Data.SqlClient;

for Oracle and SQL server databases respectively.

toookiAsked:
Who is Participating?
 
Rose BabuSenior Team ManagerCommented:
Hi,

Alternatively you can test this too.

You need to download and install the mysql connector/net. you can download it from http://dev.mysql.com/downloads/connector/net/1.0.html

then in your project, add reference to MySql.Data

then add "using MySql.Data.MySqlClient;"

in your code part add the following code,

private void button1_Click(object sender, System.EventArgs e)
{
	string MyConString = "SERVER=localhost;" +
		"DATABASE=Northwind;" +
		"UID=testuser;" +
		"PASSWORD=testpassword;";
	MySqlConnection connection = new MySqlConnection(MyConString);
	MySqlCommand command = connection.CreateCommand();
	MySqlDataReader Reader;
	command.CommandText = "select * from Customers";
	connection.Open();
	Reader = command.ExecuteReader();

	string rowData = "";
	while (Reader.Read())
	{

		for (int i= 0;i<Reader.FieldCount;i++)
		{
			rowData += Reader.GetValue(i).ToString() + ", ";
		}

		rowData += "<br />"
	}

	literal1.text = rowData;

	connection.Close();
}

Open in new window

Happy Programming.
0
 
DhaestCommented:
You can either use ODBC or MySql Client for .NET.

 

1. Download MySql Client dll.
(http://dev.mysql.com/downloads/connector/net/)
2. Add the dll to your project.

3. Add connection string to web.config

<connectionStrings>
    <add name="Connstring" connectionString="server=localhost; userid=uid;password=pwd;pooling=yes;Database=DB" providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>
  <system.data>
    <DbProviderFactories>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.0.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
    </DbProviderFactories>
  </system.data>

 

thatt's it you can follow same sqlclient connection.

Source: http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/878d58e5-0f54-4cde-b8b5-e66ee92d53ce/
0
 
toookiAuthor Commented:
Thank you.
I am going to check the details you sent to me.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Mohamed AbowardaSoftware EngineerCommented:
You can try OleDbConnection class:
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection(v=vs.80).aspx

This way will allow you to connect to any of the most common databases by just changing the connection string.

The following website can help you to determine the appropriate connection string:
http://www.connectionstrings.com
0
 
toookiAuthor Commented:
Thank you. I am going to check this.
0
 
toookiAuthor Commented:
Thanks srosebabu. I am going to try this soon.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.