用于更新数据表的代码. [英] code for updating datatable.

查看:50
本文介绍了用于更新数据表的代码.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

///请任何人帮助我编写更新代码

public DataTable GetCustomers()
               {
                          字符串查询="SELECT * FROM Customer_2";
                           SqlDataAdapter da =新的SqlDataAdapter(query,constr);
                           DataTable表=新的DataTable();
                        da.Fill(table);
                         返回表;

                 }

                  //用于插入客户
                  public void InsertCustomer(string customerName)
                 {
                          字符串查询="INSERT INTO Customer_2(CustomerName)VALUES(@CustomerName)";
                           SqlConnection con =新的SqlConnection(constr);
                           SqlCommand com =新的SqlCommand(query,con);
                           com.Parameters.Add("@ CustomerName",SqlDbType.NVarChar).Value = customerName;
                           con.Open();
                          com.ExecuteNonQuery();
                           con.Close();
                 }


                  //用于删除客户

                  public void DeleteCustomers(List& lt; int& amp; customerIDsToDelete)
               {
                          字符串查询=从Customer_2删除,其中CustomerID = @CustomerID";
                           SqlConnection con =新的SqlConnection(constr);
                           SqlCommand com =新的SqlCommand(query,con);
                           SqlTransaction tr = null;

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

                                    foreach(customerIDsToDelete中的int项目)
                                    {
                                             com.Parameters ["@ CustomerID"].Value = item;
                                            com.ExecuteNonQuery();
                                    }
                                    tr.Commit();
                           }
                           catch(异常例外)
                           {
                                    tr.Rollback();
                                   抛出ex;
                           }
                          终于
                           {
                                    con.Close();
                          }
                  }


//请任何人都可以帮助我编写更新

// 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

推荐答案

不是我自己使用它,而是:DataAdapter.Update
Not that I use it myself, but: DataAdapter.Update


这篇关于用于更新数据表的代码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆