Latest Entries »

Wednesday, December 2, 2009

Mysql with C sharp

dot net commonly use sql server as DBMS. But now it is possible to us Mysql which is open source DBMS commonly use for web developers.
First download latestConnector/Net from http://dev.mysql.com/downloads/connector/net/ link. Install the connector to your pc. This will install the documentation too. You need to add Mysql.Data into the project references before use mysql in your project.
(Solution Explorer -> Your solution -> Your project -> References -> right click -> add reference... -> double click on Mysql.Data)
Now you have to add 'using MySql.Data.MySqlClient;' to the top of your source code.


Try to understand following simple code :

string MyConnString = "SERVER=localhost;" + "DATABASE=database;" + "UID=root;" + "PASSWORD=passoword;";

MySqlConnection connection = newMySqlConnection(MyConnString);

MySqlCommand command = connection.CreateCommand();

MySqlDataReader Reader;

command.CommandText = "select * from tablename";

connection.Open(); Reader = command.ExecuteReader();

while (Reader.Read()) {

string row = "";

for (int i = 0; i <>
row += Reader.GetValue(i).ToString() + " , ";

System.Console.WriteLine(row);

}

connection.Close();

0 comments: