- Sql data Adapter:
Insert/update/Delete Records:
1. Establish the connection to the sql server by using sql connection class.
SqlConnection cnn = new SqlConnection("Data Source = .\\SQLEXPRESS; Initial Catalog = master;Integrated Security= true");
2. Open the connection
3. Execute the sql query to insert/update/delete the record(s) by using sql data adapter.
SqlDataAdapter da = new SqlDataAdapter(new SqlCommand("Insert into student values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')",cnn));
4. Use one of the following combinations of properties and methods as per the requirements.
· InsertCommand.ExecuteNonQuery();
· UpdateCommand.ExecuteNonQuery();
· DeleteCommand.ExecuteNonQuery();
· SelectCommand.ExecuteNonQuery();
da.InsertCommand.ExecuteNonQuery();
cnn.Close();
Alternately u can use SelectCommand.ExecuteNonQuery(); to perform insert/delete/update operations as shown below.
cnn.Open();
SqlDataAdapter da = new SqlDataAdapter("Insert into student values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "'",cnn);
da.SelectCommand.ExecuteNonQuery();
cnn.Close();
5. Finally close the connection.
No comments:
Post a Comment