将List中所有tableInfo数据提交到数据库中

批量表格提交逻辑

public static bool TableSubmit(List<tableInfo> tableList)
        {
            bool bolResult = false;
            using (SQLiteConnection connection = new SQLiteConnection(DbHelperSQL.connectionString))
            {
                if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }
                using (SQLiteTransaction trans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    try
                    {
                         SQLiteDataAdapter adpAdapter;
                         SQLiteCommand command ;
                         SQLiteCommandBuilder commandBuilder;
                         foreach (tableInfo table in tableList)
                         {
                             //获取表信息
                             command = new SQLiteCommand() { CommandText = string.Format("SELECT {0} FROM {1}", getTableColumn(table.dtTable), table.sName) };
                             command.Connection = connection;
                             command.Transaction = trans;
                             //创建commandBuilder
                             adpAdapter = new SQLiteDataAdapter(command);
                             commandBuilder = new SQLiteCommandBuilder(adpAdapter);
                             //设置增删改SQLCommand
                             adpAdapter.InsertCommand = commandBuilder.GetInsertCommand();
                             adpAdapter.DeleteCommand = commandBuilder.GetDeleteCommand();
                             adpAdapter.UpdateCommand = commandBuilder.GetUpdateCommand();
                             //设置事务提交数据
                             adpAdapter.InsertCommand.Transaction = trans;
                             adpAdapter.UpdateCommand.Transaction = trans;
                             adpAdapter.DeleteCommand.Transaction = trans;
                             //尝试提交数据
                             adpAdapter.Update(table.dtTable);
                         }
                        trans.Commit();//提交数据
                        bolResult = true;
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();//回滚数据
                    }
                    finally
                    {
                        //释放资源
                        trans.Dispose();
                        connection.Close();
                        connection.Dispose();
                    }
                }
            }
            return bolResult;
        }       

 

 

public struct tableInfo
    {
        public readonly string sName;   //表名
        public readonly DataTable dtTable; //数据

        public tableInfo(string Name,DataTable Table)
        {
            this.sName = Name;         
            this.dtTable = Table;
        }

转载于:https://www.cnblogs.com/nicvscs/archive/2013/06/03/3115461.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值