码迷,mamicode.com
首页 > 其他好文 > 详细

关于以DataTable形式批量写入数据的案例

时间:2015-10-19 19:03:41      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

void IDataAccess.CommandDataTable(DataTable dt, string ProcedureName, System.Data.Common.DbParameter[] para)
{
  _sqlConnection = new SqlConnection(strConnet);
  SqlDataAdapter MyAdapter = new SqlDataAdapter();
  MyAdapter.InsertCommand = new SqlCommand();
  MyAdapter.InsertCommand.CommandType = CommandType.StoredProcedure;
  MyAdapter.InsertCommand.CommandText = ProcedureName;
  MyAdapter.InsertCommand.Connection = _sqlConnection;
  foreach (SqlParameter parm in para)
    MyAdapter.InsertCommand.Parameters.Add(parm);
  MyAdapter.Update(dt);
  if (_sqlConnection != null)
  {
    _sqlConnection.Close();
  }
  GC.Collect();
}


public System.Data.DataTable CreateCartTable()
{
  System.Data.DataTable dt = new System.Data.DataTable();
  System.Data.DataRow dr;

  dt.Columns.Add(new System.Data.DataColumn("UserID"));
  dt.Columns.Add(new System.Data.DataColumn("ProductID"));
  dt.Columns.Add(new System.Data.DataColumn("Count"));

  IDataReader reader = new MssqlDataReader();
  string[] s = Request.QueryString["ids"].ToString().Split(new char[] { ‘,‘ });
  if (s != null && s.Length > 0)
  {
    for (int i = 0; i < s.Length; i++)
    {
      TempCart[] tc = reader.GetTempCartByID(Convert.ToInt32(s[i]));
      if (tc.Length > 0)
      {
        dr = dt.NewRow();
        dr[0] = Session["ID_Member"].ToString();
        dr[1] = tc[0].ProductID.ToString();
        dr[2] = tc[0].Count.ToString();
        dt.Rows.Add(dr);
      }
    }
  }
return dt;
}


void IDataWriter.AddCartMore(DataTable dt)
{
  IDataAccess access = DataCenter.GetDbConnection();
  SqlParameter[] para ={
    new SqlParameter("@UserID",SqlDbType.Int),
    new SqlParameter("@ProductID",SqlDbType.Int),
    new SqlParameter("@Count",SqlDbType.Int)
  };
  para[0].SourceColumn = dt.Columns[0].ColumnName;
  para[1].SourceColumn = dt.Columns[1].ColumnName;
  para[2].SourceColumn = dt.Columns[2].ColumnName;
  access.CommandDataTable(dt, "Sy_Cart_Add", para);
}

关于以DataTable形式批量写入数据的案例

标签:

原文地址:http://www.cnblogs.com/sky6699/p/4892487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!