Connect mysql database to c# asp.net

Connecting mysql database using c#
Connect mysql to asp.net
Connection between C# and mysql database
Inserting data from C# into mysql


Before connecting C# to mysql we have to install Mysql Connector/Net Download now

Here we using mysql database

Step to add MySql.Data to your project

Step 1:-
Right Click on your project and select Add Reference


Step 2:-
Click on Browse and move to C:\Program Files\MySQL\MySQL Connector Net 6.5.6\Assemblies\v4.0



Note:- Here MySQL Connector Net version is 6.5

 Step 3:-

Select MySql.Data.dll file and press ok


Now Write Program for connecting mysql to .net project

Herder file:-

using MySql.Data.MySqlClient;

Program:-

string MyConString = "SERVER=localhost;" +
                "DATABASE=speed;" +
                "UID=root;" +
                "PASSWORD=test;";

/* speed is database name, root is mysql username, test is mysql password */

MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand()

// open connection
connection.Open();
// entry is mysql table name
//from,to and speedval are column names
string cmdText = "INSERT INTO entry (`from`, `to`, `speedval`) VALUES (@from, @to, @speed )";

                MySqlCommand cmd = new MySqlCommand(cmdText, connection);
                cmd.Parameters.AddWithValue("@from", Label1.Text);
                cmd.Parameters.AddWithValue("@to", Label2.Text);
                cmd.Parameters.AddWithValue("@speed", Label3.Text);
                cmd.ExecuteNonQuery();

//close connection
connection.Close();


3 Comments

  1. Thanh you,
    This topic is very good and useful.

    ReplyDelete
  2. http://csharp-sqlconnection.blogspot.in/ visit this for easy and short code for database access

    ReplyDelete
  3. This is exactely what I long needed, thank you.

    ReplyDelete

Add your comments here

Back to Top