当UpdateBatchSize大于1时,Batch Insertion会给出NullReferenceException

时间:2015-04-16 08:59:50

标签: c# mysql nullreferenceexception bulkinsert

我尝试使用dataAdapter.Update()功能批量插入10,000行(或更多行)。但每当我将DataAdapter.UpdateBatchSize更改为1以外的某个值时,它都会抛出NullReferenceException(对象引用未设置为对象的实例。)

我目前的实现是在带有MySQL的C#上。我的数据表大约有50列。

StackTrace如下

  

at MySql.Data.MySqlClient.MySqlCommand.GetCommandTextForBatching()

     

at MySql.Data.MySqlClient.MySqlDataAdapter.AddToBatch(IDbCommand)   System.Data.Common.DbDataAdapter.Update(DataRow [])   dataRows,DataTableMapping tableMapping)

     

at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable)   dataTable,DataTableMapping tableMapping)

     

at System.Data.Common.DbDataAdapter.Update(DataSet dataSet,String   srcTable)在DTAR.DataAccessLib.DataX.BulkInsert(DataTable dTable,   String tableName,String&在"项目" \ DataX.cs:第991行

我的功能如下

    public int BulkInsert(DataTable dTable, string tableName, out string message)
    {
        try
        {
            message = string.Empty;
            m_DBType = ConfigurationManager.AppSettings["DBMS"].ToLower();
            int batchSize = int.Parse(ConfigurationManager.AppSettings["UpdateBatchSize"].ToString());
            MySqlDataAdapter dataAdaptor = GetDataAdaptor("select * from " + tableName + " limit 1", CommandType.Text) as MySqlDataAdapter;
            DataSet dataSet = new DataSet(tableName);
            dataAdaptor.Fill(dataSet, tableName);
            MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder((MySqlDataAdapter)dataAdaptor);
            MySqlCommand command = commandBuilder.GetInsertCommand();
            command.UpdatedRowSource = UpdateRowSource.None;
            foreach (DataRow row in dTable.Rows)
            {
                if (row.RowState == DataRowState.Unchanged)
                    row.SetAdded();

                dataSet.Tables[0].ImportRow(row);
            }
            commandBuilder.Dispose();
            dataAdaptor.InsertCommand = command;
            dataAdaptor.UpdateBatchSize = batchSize;
            dataAdaptor.AcceptChangesDuringUpdate = false;
            int result = dataAdaptor.Update(dataSet, tableName);
            return result;
        }
        catch (Exception exp)
        {
            throw exp;
        }
        finally
        {
            this.m_DBConnection.Close();
        }
    }

0 个答案:

没有答案