A small tutorial on linking C#.NET with MySql Database

July 9, 2009

Well,I was just trying out some stuff in ADO.NET , i.e. relating C#.net with databases.So here are some basic stuff you need to do before you get started with c#.net and database handling.

1. Get MySql installed on your computer and set the userid and password.

2. Get the MySql connector. get it @ http://dev.mysql.com/downloads/connector/net/6.0.html

So,lets’ get started.First create a windows form application(we will use it to view the tables ,when we do a query).

Step 1 : Right click the properties,and add reference.

Select MySql.Data

and include using Mysql.Data.MysqlClient

Step2 : Establish Connection
1.string connect = “DataSource=localhost;Initial Catalog=login;User ID=root;Password=admin;” ;
2.MySqlConnection con = new MySqlConnection(connect);
3.con.Open();

1. Create a connection string.Here,DataSource refers to the ip address where the MySql database is present,Initial Catalog being the Database you are connecting to, UserId being the Mysql userid and password ,the password for the same.

2. Create a new MySqlConnection object and pass the connection string.This creates an object and you are ready for connecting

3. It establishes the connection.

Step 3 :

string querystr = textBox1.Text;
MySqlCommand cmd = new MySqlCommand(querystr, con);
cmd.ExecuteNonQuery();

querystr takes a query from the text box in the form.

MySqlcommand cmd passes the query along with the connection string.

ExecuteNonQuery() method is necessary to execute the same.

You can execute any kind of MySqlstatement..All you need to do is type the exact statement in the TextBox

Sample MySQL query statements :

insert into marks_table (sl_no,name,marks) values ( 5,’xyz’,89);

this inserts a row into the table called marks_table with values 5 , xyz and 89 under serial no,name and marks

Hope this post was useful.Will continue on how to display later.Feelin sleepy.!

Cheerio

Entry Filed under: computers, linux. Tags: , , , , , , .

1 Comment Add your own

  • 1. seesharpgears  |  September 11, 2009 at 11:09 pm

    Hey, this is the real basic tutorial that you need in order to get a MySQL database connected to C#. However, I think that ADO.NET allows utilizing of other programming aspects in the OOP, such as inheritance, polymorphism and generics in order to write flexible, robust and reliable data layers. I have written a post about connecting MySQL to C# with higher level of abstraction. I would appreciate if you read it and provide me with feedback of your opinion.

    Reply

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Tweets

Visitor Count

Archives

Tag Cloud

.deb .deb.deb anatomy .tar .tar.gz ac milan argentina berbatov bunny carlitos compress configure control control.tar.gz data.tar.gz debian fans Football gravity lean Humour install linux make Manchester United Manutd md5sums Michael Jackson MJ dead MJ dies Old trafford opensuse package packages perez POP KING postinst postrm prerm Race Racism Racist Real Madrid Ronaldo Russel Peters SAF tevez

Blogroll

Categories

SocialVibe