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


    //批量添加数据交互ADO.NET部分
    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(); } //创建要添加的批量数据Table 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); }
  • 相关阅读:
    (笔试题)镇长选举
    (笔试题)最小的非“重复的数”
    ( 笔试题)只出现一次的数
    (算法)二叉树中两个结点的最近公共父结点
    (笔试题)区间最大重叠
    (剑指Offer)面试题61:按之字形顺序打印二叉树
    (算法)Partition方法求数组第k大的数
    (剑指Offer)面试题60:把二叉树打印成多行
    整理一些不错的、网上好评的电影、电视、视频等资源地址
    个人网站/博客,建站好的域名和网站供应商网站整理
  • 原文地址:https://www.cnblogs.com/sky6699/p/4892487.html
Copyright © 2020-2023  润新知