hi,
i am trying to create a database driven application using Sql2005 and c#.. i have alittle problem
can anyone tell me how to connect to sql database using c# and do simple transactions such as insert update delete and search ...but it shud be fully code based.
if possible please provide a sample.....
Hi,
First add Using System.SqlClient; namespace to your cs file.
Then write connection string like this
//Connection String
SqlConnection conn = new SqlConnection("data source=<NameOfYourSqlServerInstance>;Initial catalog=<NameOfYourDataBase>; username = <UserName>; password =<Password>;Integrated Security=True");
//Insert Method
SqlConnection conn = new SqlConnection("data source=<NameOfYourSqlServerInstance>;Initial catalog=<NameOfYourDataBase>; username = <UserName>; password =<Password>;Integrated Security=True");
SqlCommand cmd = new SqlCommand("InsertQuery",conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
//Update Method
SqlConnection conn = new SqlConnection("data source=<NameOfYourSqlServerInstance>;Initial catalog=<NameOfYourDataBase>; username = <UserName>; password =<Password>;Integrated Security=True");
SqlCommand cmd = new SqlCommand("UpdateQuery",conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
//Delete Method
SqlConnection conn = new SqlConnection("data source=<NameOfYourSqlServerInstance>;Initial catalog=<NameOfYourDataBase>; username = <UserName>; password =<Password>;Integrated Security=True");
SqlCommand cmd = new SqlCommand("DeleteQuery",conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();