connect to sql database using c#
add these library first
using System.Data;
using System.Data.SqlClient;
create string to connect with database
string str = @"Data Source=(local);Initial Catalog=database;Persist Security Info=True;User ID=username;Password=password";
SqlConnection con = new SqlConnection(); //create object to connect sql
SqlCommand cmd = new SqlCommand(); // create object to send command in sql
DataTable dt = new DataTable(); //create object to store data in temprary
con.ConnectionString = str; //it open connection for database
cmd.Connection = con; // it make connection between connection and command
SqlDataAdapter da = new SqlDataAdapter(cmd); //adatapter object to talk database
con.Open();//open connection
cmd.CommandText = "insert into table(comuln1,column2)values('a','b')"; // specify command
da.Fill(dt);//fire command
con.close();//close connection
//note: if id column is auto then no need to specify id column name to insert
now read database from sql database
create connection as above
System.Data.SqlClient.SqlDataReader SqlDR; // create object to read database
con.Open();
cmd.CommandText = "SELECT column1,column2 FROM table";
System.Data.SqlClient.SqlDataReader SqlDR2;
SqlDR = cmd.ExecuteReader();
while (SqlDR.Read())
{
complainno = SqlDR["column1"].ToString();
}
con.Close();
Monday, 17 June 2013
by Rupesh Sharma on 23:44
No comments
Rupesh Sharma
If you need any kind of support Contact me. i'll reply with solution. roopeshsharma00031@gmail.com
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment