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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 39
    Comment on it

    While working with ADO.NET you need to understand the thing why SqlCommand is used.

    We have two kind of operations related to database:

    1 That will make changes in the database

    •  Insert
    • Update
    • Delete

     

    2 That will not make any change to the database

    • Select

     

    For updating the query will change the parameter except that everything remains the same.

    try
        {
            string connectionString =
                "server=.;" +
                "initial catalog=employee;" +
                "user id=sa;" +
                "password=sa123";
            using (SqlConnection conn =
                new SqlConnection(connectionString))
            {
                conn.Open();
                using (SqlCommand cmd =
                    new SqlCommand("UPDATE EmployeeDetails SET Name=@NewName, Address=@NewAddress" +
                        " WHERE Id=@Id", conn))
                {
                    cmd.Parameters.AddWithValue("@Id", 1);
                    cmd.Parameters.AddWithValue("@Name", "Munna Hussain");
                    cmd.Parameters.AddWithValue("@Address", "Kerala");
    
                    int rows = cmd.ExecuteNonQuery();
    
                    //rows number of record got updated
                }
            }
        }
        catch (SqlException ex)
        {
            //Log exception
            //Display Error message
        }

     

    Here we pass the parameters that needs to be updated.

    .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: