While working with SqlDataAdapter we always use select operation as it is used like a buffer for storing and retrieving data.
But what if we want to perform any crud operation like insert update delete while interacting with the datasource.
We can also do this thing with SqlDataAdapter. We can use the SqlDataAdapter methods for it.

SqlDataAdapter will use these methods for defining and doing the required operation.
- Select Command
- Insert Command
- Update Command
- Delete Command
Select Command : To retrieve data from the table or from the datasource.
InsertCommand : To insert data into the database or the datasource.
UpdateCommand : To update data into the database or the datasource.
DeleteCommand : To delete data into the database or the datasource.
The insert operation using SqlDataAdapter can be done as :
SqlConnection conObj=new SqlConnection();
SqlDataAdapter adapObj=new SqlDataAdapter();
adapObj.SelectCommand=new SqlCommand("insert into student (name,age) values (@name,@age)",conObj);
adapObj.SelectCommand.Parameters.Add(new SqlParameter("@name",txtName.Text));
adapObj.SelectCommand.Parameters.Add(new SqlParameter("@age",txtAge.Text));
conObj.Open();
adapObj.SelectCommand.ExecuteNonQuery();
conObj.Close();
The update operation using SqlDataAdapter can be done as:
SqlConnection conObj=new SqlConnection();
SqlDataAdapter adapObj=new SqlDataAdapter();
adapObj.UpdateCommand=new SqlCommand("update student set(name,age) values (@name,@age)",conObj);
adapObj.UpdateCommand.Parameters.Add(new SqlParameter("@name",txtName.Text));
adapObj.UpdateCommand.Parameters.Add(new SqlParameter("@age",txtAge.Text));
conObj.Open();
adapObj.UpdateCommand.ExecuteNonQuery();
conObj.Close();
0 Comment(s)