A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Connect to MySQL using C# and Connector/NetThis tutorial explains how to connect to a MySQL database using the Connector/Net ADO.NET driver. |
On Saturday, September 29th 2007 at 07:53 AM By Syed Hussain (View Profile) ![]() ![]() ![]() ![]() (Rated 4.5 with 35 votes) |
||
|
In this mini tutorial I will teach you how to get connected to a MySQL database. This is really quite simple. All you need to do is download Connector/Net which is a fully-managed ADO.NET driver written in 100% pure C#. Download the installer and install Connector/Net. After installation load your C# IDE. Begin a new console project. The first thing you need to do is add a reference to your project. Open the “Add Reference” dialog box. Under the .Net tab scroll down to MySQL.Data and add this reference to your project. Before we start with the actual code we need to add two namespaces. Add the following namespaces. using MySql.Data.MySqlClient; Finally it’s now time to write some code. When connecting to any database you usually need to set up a provider. This provder is of type string and simply consists of information such as database to connect to, username, password and url/name of the machine in which the database is hosted. This provider sring is different for different databases. The provider string needed to connect to a MySQL database is listed below.
string strProvider = "Data Source=" + host + ";Database=" + database + ";User ID=" + user + ";Password=" + password; In the provider string above we simply supply the data source, which is the url/ip/name of the computer which the database is hosted on. We also supply the database name to connected to and the username and password. If your using a default MySQL installation, the username should be “root” and the password should be the password you used in the installation process. After setting up a provider, you need to create a connection to the database. You do this by using the MySqlConnection object. When creating an instance of MySqlConnection, you supply the provider in it’s constructor. MySqlConnection mysqlCon = new MySqlConnection(strProvider); We then use the Open() method of mysqlCon object to open a connection to the database. MySqlConnection mysqlCon = new MySqlConnection(strProvider); Now that the connection is open, you need to query the database. You need to send an SQL statement to get the results from the database. This is done using the MySqlCommand object. In the constructor of the MySqlCommand you supply an SQL statement and also the connection object. string strSQL = "SELECT * FROM [Your Table Here]"; So now you can send an SQL statement to the database. But you need a way to store the records, for this you use the MySqlDataReader object. MySqlDataReader mysqlReader = mysqlCmd.ExecuteReader(); Now the MySqlDataReader object will contain all the records from the database. The MySqlDataReader object is a read only onject, which allows you to quickly get records out of a database table. It can not be used to update a database table. To get records from the MySqlDataReader object you use a while loop. The loop uses the MySqlDataReader objects Read() method, to get the data for each row. while (mysqlReader.Read()) Finally you use the appropriate methods of the MySqlDataReader object to get the data from each column. For example the first column is returned as an Integer using the GetInt32() method, while the second and third columns return string data. |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current Commentshi
your exampe coding gave me some idea but i m not able to implement
can u help me out in adding just the user name and pwd to the database using mysql in asp.net.
Hepl me out
WHAT A LIFE SAVER!!! Thanks so much for this AWESOME code. Can I simply include the MySql.Data.dll fiel with my application and it will work on my user\'s computers? Or does each person need to have the Connector installed on their machine?
No they don't need the Net/Connector installed on their computer, just the MySql.Data.dll.
Awesome tut!
WOW! You're the best!!!
Thank you very much.
It works great but only with localhost and generates error {Un able to connect any of Mysql Host}if Mysql database is hosted on remote server.Kindly explain breifly that what settings to make from server side to get connect with server remotely if we have Mysql 3.23.58 on server and how to give access to Mysql
Thankyou! Thankyou! Thankyou!
Very Good ,Thank you ,
Syed,
Great writeup- short and to the point. Perfect, thanks.
Exactly ... short and to the point...All that u need ... other sites confuse u ..
Thanks Syed ..
Thanks, this article helped me understand a bit more clear than the docs did. I furthered the code and created a 'fetchAll' function which will return all data from a table or based on a where clause. It then does type checking and stores the field names and values in an array.
Great tutorial!
I am not able to add the MySql.Data.Dll and for this reason the name space
using MySql.Data.MySqlClient;
is throwing exceptin.
Please Help me.
I want to display all record in the databse field when i m using while(dr.read()) it cannot execute it on textbox box..coding below
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using MySql.Data.MySqlClient;
using System.Web.Mail;
//using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
public conn c1 = new conn();
public MySql.Data.MySqlClient.MySqlCommand cmd;
public MySql.Data.MySqlClient.MySqlCommand cmd1;
public MySql.Data.MySqlClient.MySqlDataReader dr;
public MySql.Data.MySqlClient.MySqlDataAdapter da;
public MySql.Data.MySqlClient.MySqlDataAdapter da1;
public string q, q1, q2, cid;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
etview();
}
}
public void etview()
{
bool co = c1.oc();
if (!co)
{
}
else
{
q1 = "select Email_Id from customer_details ";
cmd = new MySqlCommand(q1, c1.con);
dr = cmd.ExecuteReader(CommandBehavior.Default);
while(dr.Read())
{
//while (reader.Read())
//{ // Read the value...
// string sap = reader.GetValue(27).ToString();
//txtto.Text = sap;
//}
txtto.Text = dr["Email_Id"].ToString();
txtto.Text = txtto.Text ",";
}
}
//dr.Close();
c1.cc();
}
I've spent almost half an hour looking for something to help me with this. I was looking for a way to connect to my mysql database using C#. I needed to start from scratch.
Everything I needed to know I was able to find right here.
thank you for putting together a simple yet effective guide.
Worked like a charm. Thanks!
Can this work with Windows Application??
Don't forget to close your connections when you have finished with them!!
[code]
mysqlReader.Close();
mysqlCon.Close();
[/code]
it's hard to understand
it's hard to understand
it's hard to understand
when i go to install the mysql-connector-net-1.0.10.1.exe it shows that "setup is required for .net framework 1.1" But my .net framework version is 3.5 ...how can i solve this problem?
where i can get mysql.Data.dll file ?
thank you...:)
thank you...:)
mukta, go and download a newer version of mysql-connector.
hi! Great tutorial!
please can you tell me how to connect C# mysql with a server not a locahost but on server like 192.168.0.2?
thanks alot,
short and to the point
http://dev.mysql.com/doc/refman/6.0/en/connector-net-visual-studio-making-a-connection.html
Detail Tutorial
I had to add my PC address to Remove SQL in cpanel on my server and then it just worked. It's not often that a tutorial works so easily. Thank you.
I had to add my PC address to Remove SQL in cpanel on my server and then it just worked. It's not often that a tutorial works so easily. Thank you.
Isn't the MySql.Data.Reader an class? The author of this minitutorial must learn to separate different terms.
Great tutorial... Very informative.. Thanks for sharing.. 'twas a life saver.. I needed this for school..
I just have one question... Which is better to use? this one? or odbc connector? please reply.. Thanks..
nice tutorial
How to execute dos command in ASP.NET.
you can see this tutorial for more details.http://learneveryday.net/asp-net/mysql/insert-in-to-mysql-with-asp-net-part-1/
Thanks dude!!
I am very much thankfull to u,becoz i am trying to connect c# with Mysql from last 4 hrs.but after so many search,i came to these site and after that followed ur steps.
Thanks once again
If u need any help frm me,just let me knw.I will be happy if i can help u.
hi,
can u help me out in adding just the user name and pwd to the database using mysql in c#.net
Help me out.
for inserting values in the database we need only to query like:
string strSQL = "INSERT INTO [Your Table Here] VALUES .... ";
MySqlCommand mysqlCmd = new MySqlCommand(strSQL,mysqlCon);
or i will need another object, something like " MySqlDataWriter"
???
Related Tutorials
Related Source Code
C# Job Search