question about mysql and c#
#2

Suggest using   MySqlClient, instead of SqlClient (since SqlClient is for MSSQL)

 

You first need to get the Mysql Connector  from this site:

2

 

Install it: Then start your VC# - >RightClick on your project ->Add reference->.NET->Mysql.Data->OK

 

The connection string is this:

server=YourMysqlServer;database=YourDB;uid=YourMysqlUser;password=YourMysqlUserPassword
Your code:

using MySql.Data.MySqlClient;
...
string dbServer = "127.0.0.1";
string dBase = "testdb";
string dbuser = "root";
string dbPass = "test";
...
try{
    //Connecting to  mysql
    string strConnection = string.Format("server={0};database={1};uid={2};password={3}", dbServer, dBase, dbuser, dbPass);
  MySqlConnection dbConnerction = new MySqlConnection(strConnection);
   dbConnerction.Open();

   //executing a query
    string query  = "SELECT * FROM test_table";
    MySqlCommand dbCommand = new MySqlCommand(query, dbConnerction);
    MySqlDataReader dbReader = dbCommand.ExecuteReader();

    //Reading the data
    while (dbReader.Read()){
          string stuff = dbReader["test_column1"].ToString();
float f= dbReader.GetFloat("test_column2");
    }

    //closing the reader
    dbReader.Close();
    dbCommand.Dispose();

   //Close the connection
    dbConnerction.Close();
}catch (Exception ex){
                MessageBox.Show(ex.Message, "SQL ERROR");
}
You can use OleDb  or ODBC if you want to connect to the MySql as a alternative.



Messages In This Thread
[No subject] - by soryjero - 07-08-2013, 01:03 PM
[No subject] - by someone - 07-08-2013, 05:23 PM
[No subject] - by Wizatek - 07-08-2013, 06:52 PM
[No subject] - by soryjero - 07-08-2013, 10:03 PM
[No subject] - by soryjero - 07-09-2013, 09:58 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)