“ExecuteScalar:尚未初始化连接属性。”

时间:2017-11-23 04:04:00

标签: c#

我是c#的新手,目前在没有调试的情况下运行此程序时遇到了一些问题。

这是我项目的登录页面。我创建了一个基于服务的数据库,我想连接数据库中表'Table'中的用户名和密码数据。 但是,我遇到了一个问题,即“ExecuteScalar:Connection属性尚未初始化”。当我运行此代码时。

任何人都可以帮我吗?

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\hp\documents\visual studio 2015\Projects\PersonalFinancialSoftware\PersonalFinancialSoftware\Login.mdf;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "SELECT COUNT (*) FROM Table where UserID = @userid AND Password = @password";
        cmd.Parameters.AddWithValue("@userid", textBox1.Text);
        cmd.Parameters.AddWithValue("@password", textBox2.Text);
        object result = cmd.ExecuteScalar();

        conn.Open();
        string useridlogin = Convert.ToString(result);
        conn.Close();

        if (useridlogin != " ")
        {
            Home_Page homepage = new Home_Page();
            homepage.Show();
        }
        else
        {
            MessageBox.Show("Invalid ID or password, please try again!", "Info", MessageBoxButtons.OK);
        }
    }

2 个答案:

答案 0 :(得分:1)

我可以看到您在打开SQL数据库连接之前已经执行了ExecuteScalar方法,导致您获得错误。

在ExecuteScalar方法之前打开连接,然后你就完成了。

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\hp\documents\visual studio 2015\Projects\PersonalFinancialSoftware\PersonalFinancialSoftware\Login.mdf;Integrated Security=True");
    conn.Open();
    SqlCommand cmd = new SqlCommand();

    cmd.CommandText = "SELECT COUNT (*) FROM Table where UserID = @userid AND Password = @password";
    cmd.Parameters.AddWithValue("@userid", textBox1.Text);
    cmd.Parameters.AddWithValue("@password", textBox2.Text);
    object result = cmd.ExecuteScalar();

    string useridlogin = Convert.ToString(result);
    conn.Close();

    if (useridlogin != " ")
    {
        Home_Page homepage = new Home_Page();
        homepage.Show();
    }
    else
    {
        MessageBox.Show("Invalid ID or password, please try again!", "Info", MessageBoxButtons.OK);
    }
}

答案 1 :(得分:0)

您在ExecuteScalar之后打开连接即表示您收到错误,您必须打开连接,因为ExecuteScalar尝试此代码

private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\hp\documents\visual studio 2015\Projects\PersonalFinancialSoftware\PersonalFinancialSoftware\Login.mdf;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        conn.Open();
        cmd.CommandText = "SELECT COUNT (*) FROM Table where UserID = @userid AND Password = @password";
        cmd.Parameters.AddWithValue("@userid", textBox1.Text);
        cmd.Parameters.AddWithValue("@password", textBox2.Text);
        object result = cmd.ExecuteScalar();

        string useridlogin = Convert.ToString(result);
        conn.Close();

        if (useridlogin != " ")
        {
            Home_Page homepage = new Home_Page();
            homepage.Show();
        }
        else
        {
            MessageBox.Show("Invalid ID or password, please try again!", "Info", MessageBoxButtons.OK);
        }
    }

在您的代码中查询SQL查询连接。但是你已经在ExecuteScalar之后打开它,所以这给你一个错误。