Posts filed under 'computers'
A small tutorial on linking C#.NET with MySql Database
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
1 comment July 9, 2009