Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
// please anyone could help me write the update code

public DataTable GetCustomers()
                  {
                           string query = "SELECT * FROM Customer_2";
                           SqlDataAdapter da = new SqlDataAdapter(query, constr);
                           DataTable table = new DataTable();
                           da.Fill(table);
                           return table;

                  }

                  // For Inserting Customers
                  public void InsertCustomer(string customerName)
                  {
                           string query = "INSERT INTO Customer_2 (CustomerName) VALUES (@CustomerName)";
                           SqlConnection con = new SqlConnection(constr);
                           SqlCommand com = new SqlCommand(query, con);
                           com.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = customerName;
                           con.Open();
                           com.ExecuteNonQuery();
                           con.Close();
                  }


                  // For Deleting Customers

                  public void DeleteCustomers(List<int> customerIDsToDelete)
                  {
                           string query = "DELETE FROM Customer_2 WHERE CustomerID = @CustomerID";
                           SqlConnection con = new SqlConnection(constr);
                           SqlCommand com = new SqlCommand(query, con);
                           SqlTransaction tr = null;

                           try
                           {
                                    con.Open();
                                    tr = con.BeginTransaction();
                                    com.Transaction = tr;
                                    com.Parameters.Add("@CustomerID", SqlDbType.Int);

                                    foreach (int item in customerIDsToDelete)
                                    {
                                             com.Parameters["@CustomerID"].Value = item;
                                             com.ExecuteNonQuery();
                                    }
                                    tr.Commit();
                           }
                           catch (Exception ex)
                           {
                                    tr.Rollback();
                                    throw ex;
                           }
                           finally
                           {
                                    con.Close();
                           }
                  }


// please anyone could help me write the update
Posted

1 solution

Not that I use it myself, but: DataAdapter.Update
 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900