如何实现快速的将DataTable插入到临时表

 我来答
Anigado1992
2016-11-18 · TA获得超过125个赞
知道小有建树答主
回答量:147
采纳率:0%
帮助的人:122万
展开全部
string selectCommand = 
dataTable.Columns.Cast<DataColumn>().Aggregate("select ", 
(current, col) => current + (col.ColumnName + ","));
            selectCommand = selectCommand.Substring(0, selectCommand.Length - 1);
            selectCommand += " from " + TableName;
DataRow[] errorRows = SaveDataTable(dataTable, TableName, commandText,
                continueUpdateOnError);
/// <summary>
        /// 保存DataTable
        /// </summary>
        /// <param name="dataTable">DataTable</param>
        /// <param name="tableName">表名</param>
        /// <param name="commandText">sql文本命名</param>
        /// <param name="continueUpdateOnError">行更新过程中遇到错误是否成异常</param>
        /// <returns>错误行集合</returns>
        public static DataRow[] SaveDataTable(DataTable dataTable, string tableName, string commandText,
            bool continueUpdateOnError)
        {
            var helper = SQLiteDBHelperService.Instanse();
            var trans = helper.BeginTrans();
            var conn = trans.Connection;
            if (conn.State == ConnectionState.Closed)
                conn.Open();

            try
            {
                var cmd = conn.CreateCommand();
                cmd.CommandText = commandText;
                cmd.Connection = conn;
                cmd.Transaction = trans;

                var adp = helper.DBComm.CreateDataAdapter(cmd);
                adp.ContinueUpdateOnError = continueUpdateOnError;
                DbCommandBuilder cmdBuilder = helper.DBComm.CreateCommandBuilder(adp);
                cmdBuilder.DataAdapter = adp;
                cmdBuilder.QuotePrefix = "[";
                cmdBuilder.QuoteSuffix = "]";
                adp.InsertCommand = cmdBuilder.GetInsertCommand();
                adp.UpdateCommand = cmdBuilder.GetUpdateCommand();
                adp.DeleteCommand = cmdBuilder.GetDeleteCommand();
                adp.Update(dataTable);
                dataTable.AcceptChanges();
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                MessageBox.Show(ex.ToString());
            }

            conn.Close();
            return dataTable.GetErrors();
        }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式