在Transactionalized时,CommandText属性未正确初始化错误

时间:2018-03-27 15:01:06

标签: c# mysql

此代码以前一直运行良好。但是,我需要对我的读写进行事务处理,当尝试通过事务化我的读取来开始该过程时,我遇到了这个commandText未初始化的错误。

确切错误,第156行是DA.Fill(tbl);

Error: System.InvalidOperationException: The CommandText property has not been properly initialized.
   at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
   at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   at MDB_N2000.DatabaseManager.GetTable(TableQuery query) in C:\Users\Tcordeau.pub\DocumentsException thrown: 'System.InvalidOperationException' in MySql.Data.dll
\MDB-N2000\MDB-N2000\Model\DatabaseManager.cs:line 156 The CommandText property has not been properly initialized.
  1. 调用LoadTable。

    public void LoadTable()
    {
        if (Db.IsConnOpen)
        {
            Db.BeginTransaction();
            // Persist the selected row across table reload by keeping track of the row's first value.
            // For this to work, tables need to always have an autonumbered integer index in the first column.
            // Store the row index.
            if (Vals.Any())
                if (Vals[0].Val != null)
                    if (Vals[0].Val.GetType() == typeof(int))
                        RowID = Vals[0].Val;
    
            // For tables in the sub-tables viewer, get the key value of the main table of the module
            // to use as a parameter in the SQL select query.
            if (!IsMainTable)
            {
                LoadTableQuery.Prms[0].Val = MainVM.Modules.SelectedModule.Vwr.Table.Vals[0].Val;
            }
    
            // Get the table from the database
            Tbl = Db.GetTable(LoadTableQuery);
            DataTable MainTable = Db.GetTable(LoadTableQuery);
            MainTable.TableName = "MainTable";
            dv.Table = MainTable;
    
            // Retrieve the row index and set the selected row.
            if (Vals.Any())
                if (Vals[0].Val != null)
                    if (Vals[0].Val.GetType() == typeof(int))
                        Tbl.DefaultView.Sort = Tbl.Columns[0].ColumnName.ToString();
            //SelectedRowIndex = Tbl.DefaultView.Find(RowID);
            Db.CommitTransaction();
        }
    }
    
  2. 转到GetTable。

    public DataTable GetTable(TableQuery query)
    {
        DataTable tbl = new DataTable();
        if (IsConnOpen)
        {
            try
            {
                SelectCmd.Parameters.Clear();
                SelectCmd.CommandText = query.Qry;
                for (int i = 0; i < query.Prms.Count; i++)
                {
                    SelectCmd.Parameters.AddWithValue(query.Prms[i].Col, query.Prms[i].Val);
                }
                // DA = MySQLDataAdapter
                DA.Fill(tbl);
                tbl.DefaultView.Sort = tbl.Columns[0].ColumnName.ToString() + " ASC";
    
            }
            catch (MySqlException ex)
            {
                Msg = "Error: " + ex.Number + " " + ex.Message + " from Qry = " + query.Qry;
            }
            catch (Exception ex)
            {
                Msg = "Error: " + ex + " " + ex.Message;
            }
        }
        else
        {
            Msg = "Not Connected";
        }
        return tbl;
    }
    
  3. 我之前已将查询打印到控制台,以确保它不是空白。

    参考方法。

        private DatabaseManager()
        {
            Acct = new Account();
    
            SelectCmd = new MySqlCommand() { Connection = Conn };
            UpdateCmd = new MySqlCommand() { Connection = Conn };
            Cmd = new MySqlCommand() { Connection = Conn };
            DA = new MySqlDataAdapter(SelectCmd)
            {
                UpdateCommand = UpdateCmd
            };
    
            Msg = "Ready to Connect";
        }
    
        // Creates a new transaction and returns the command it's attached to.
        public void BeginTransaction()
        {
            SelectCmd = Conn.CreateCommand();
            MySqlTransaction NewTransaction = Conn.BeginTransaction();
            SelectCmd.Connection = Conn;
            SelectCmd.Transaction = NewTransaction;
        }
    
        //rollsback transaction
        public void RevertTransaction()
        {
            Cmd.Transaction.Rollback();
        }
    
        //adds a query to the transaction
        public void TransactionQuery(TableQuery NewQuery)
        {
            Cmd.CommandText = NewQuery.Qry;
            Cmd.ExecuteNonQuery();
        }
    
        //commits transaction
        public void CommitTransaction()
        {
            SelectCmd.Transaction.Commit();
        }
    

1 个答案:

答案 0 :(得分:1)

问题/解决方案是删除此行

public void BeginTransaction()
{
   // SelectCmd = Conn.CreateCommand();  //Problem Line
    MySqlTransaction NewTransaction = Conn.BeginTransaction();
    SelectCmd.Connection = Conn;
    SelectCmd.Transaction = NewTransaction;
}