Click here to Skip to main content
15,914,014 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I find difficult in connecting the database in C#? Can you give me some techniques?
Posted

 
Share this answer
 
Hi,

You can implement the below way to connect with mySQL db.

1) Add namespaces in your page
i)using MySql.Data;
ii)using MySql.Data.MySqlClient;

2) get connection string or create in format

String ConnectionString= "server=127.0.0.1;uid=test;pwd=test@123;database=test;";

3) In the class from where you want to connect to database write below Line of code,

Public Class Test
{

// appilcation logic goes here

try
{

// Application logic goes here

MySqlConnection Myconn = new MySqlConnection();
Myconn.ConnectionString = ConnectionString;
Myconn.Open();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}

finally
{
if(Myconn.State==ConnectionState.Open)
{
Myconn.Close();
}

}

}


// if you want to pass some command then follow below steps

// create command as below after opening the connection in MyConn.Open()statement.

MySqlCommand cmd=MyConn.CreateCommand();
cmd.CommandText="Select * FROM Employee";
MySqlDataAdapter adpt=new MySqlDataAdapter(cmd);
Dataset ds=new Dataset();
adpt.Fill(ds);
//find the dataset with any control or manipulate accordingly

Hope you understand the concept now..
 
Share this answer
 
First of all you need Connector/Net
http://dev.mysql.com/downloads/connector/net/[^]

Moreover you should read MySQL Connector/Net Developer Guide
http://dev.mysql.com/doc/connector-net/en/index.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900