Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Adapter with crud operations in ADO.NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 123
    Comment on it

    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();

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: