执行Reader:连接属性尚未在C#和Visual Studio中初始化

时间:2017-04-30 14:36:11

标签: c# visual-studio-2015

我一直收到错误

  

ExecuteReader:尚未初始化连接属性

在C#和Visual Studio中。我尝试了一些我研究过的解决方案,但他们似乎没有做到这一点。

这是我的代码:

public static int AddCustomer(Customer customer)
{
    MySqlConnection connection = MySqlCommand.GetConnection();

    string strInsertStatement =
            "INSERT Customers (Name, Address, City, State, ZipCode) " +
            "VALUES (@Name, @Address, @City, @State, @ZipCode)";

    MySql.Data.MySqlClient.MySqlCommand insertCommand = new MySql.Data.MySqlClient.MySqlCommand(strInsertStatement);

    insertCommand.Parameters.AddWithValue("@Name", customer.strFirstName);
    insertCommand.Parameters.AddWithValue("@Address", customer.strStreetName);
    insertCommand.Parameters.AddWithValue("@City", customer.strCity);
    insertCommand.Parameters.AddWithValue("@State", customer.strState);
    insertCommand.Parameters.AddWithValue("@ZipCode", customer.strPhoneNumber);

    try
    {
        connection.Open();
        insertCommand.ExecuteNonQuery();

        string strSelectStatement = "SELECT IDENT_CURRENT('Customers') FROM Customers";
        MySql.Data.MySqlClient.MySqlCommand selectCommand = new MySql.Data.MySqlClient.MySqlCommand(strSelectStatement, connection);

        int customerID = Convert.ToInt32(selectCommand.ExecuteScalar());
        return customerID;
    }
    catch (MySqlException ex)
    {
        throw ex;
    }
    finally
    {
        connection.Close();
    }
}

我错过了什么使这个错误出现?

4 个答案:

答案 0 :(得分:1)

错误说明了一切:尚未设置连接属性。

您有一个Connection,并且您有insertCommand。代码中没有任何地方告诉insertCommand使用Connection。

insertCommand.Connection = connection;

或者,可能作为连接器的一部分,就像使用Select。

一样

答案 1 :(得分:1)

您必须将连接作为第二个参数提供给MySQlCommand:

MySql.Data.MySqlClient.MySqlCommand insertCommand =
            new MySql.Data.MySqlClient.MySqlCommand(strInsertStatement, connection);

答案 2 :(得分:0)

你在哪一行得到错误?我只是快速查看一下你的代码并认为你忘了将你的连接添加到insert命令而是使用以下代码:

编辑:将所有代码放在此处,并将错误打印添加到控制台并分配连接。

我猜你在open()方法调用中得到错误。您的解决方案中没有连接字符串。不要忘记输入您的凭据。

public static int AddCustomer(Customer customer)
{
    MySqlConnection connection = new MySqlConnection();
    connection.ConnectionString = "server=127.0.0.1;uid=root;" +
        "pwd=12345;database=test;"
    string strInsertStatement =
        "INSERT Customers " +
        "(Name, Address, City, State, ZipCode) " +
        "VALUES (@Name, @Address, @City, @State, @ZipCode)";
    MySql.Data.MySqlClient.MySqlCommand insertCommand =
        new MySql.Data.MySqlClient.MySqlCommand(strInsertStatement);
    insertCommand.Parameters.AddWithValue(
        "@Name", customer.strFirstName);
    insertCommand.Parameters.AddWithValue(
        "@Address", customer.strStreetName);
    insertCommand.Parameters.AddWithValue(
        "@City", customer.strCity);
    insertCommand.Parameters.AddWithValue(
        "@State", customer.strState);
    insertCommand.Parameters.AddWithValue(
        "@ZipCode", customer.strPhoneNumber);
    try
    {
        connection.Open();
        insertCommand.Connection = connection;
        insertCommand.ExecuteNonQuery();
        string strSelectStatement =
            "SELECT IDENT_CURRENT('Customers') FROM Customers";
        MySql.Data.MySqlClient.MySqlCommand selectCommand =
            new MySql.Data.MySqlClient.MySqlCommand(strSelectStatement, connection);
        int customerID = Convert.ToInt32(selectCommand.ExecuteScalar());
        return customerID;
    }
    catch (MySqlException ex)
    {
        Console.WriteLine(ex.ToString());
        throw ex;
    }
    finally
    {
        connection.Close();
    }
}

答案 3 :(得分:0)

您需要设置命令的Connection属性。使用接受MySqlConnection的构造函数。

MySql.Data.MySqlClient.MySqlCommand insertCommand = new MySql.Data.MySqlClient.MySqlCommand(strInsertStatement, connection);