المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : code for updating datatable.



C# Programming
07-31-2009, 02:22 PM
// 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