Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “ExecuteScalar:尚未初始化连接属性。”_C# - Fatal编程技术网

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

C# “ExecuteScalar:尚未初始化连接属性。”,c#,C#,我是c语言的新手,目前我在没有调试的情况下运行这个程序时遇到了一些问题 这是我的项目的登录页面。我已经创建了一个基于服务的数据库,我想连接到数据库中“table”表中的用户名和密码数据。 但是,我遇到了一个问题,即ExecuteScalar:Connection属性尚未初始化。当我运行这个代码时 有人能帮我吗 private void button1_Click(object sender, EventArgs e) { SqlConnection conn =

我是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);
        }
    }

我可以看到,在打开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);
    }
}

您在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之后打开了它,所以这给了你一个错误