gpt4 book ai didi

c# - 事务化时 CommandText 属性未正确初始化错误

转载 作者:行者123 更新时间:2023-11-29 07:34:42 28 4
gpt4 key购买 nike

此代码之前运行良好。然而,我需要事务化我的读写,当我试图通过事务化我的读取来开始该过程时,我遇到了这个 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;
    }

我之前已经将查询打印到控制台以确保它不是空白的。

引用方法。

    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();
}

最佳答案

问题/解决方案是删除这一行

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

关于c# - 事务化时 CommandText 属性未正确初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49516353/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com