【问题标题】:"ExecuteScalar: Connection property has not been initialized."“ExecuteScalar:连接属性尚未初始化。”
【发布时间】:2017-11-25 14:26:58
【问题描述】:

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

这是我的项目的登录页面。我创建了一个基于服务的数据库,我想连接到数据库中表“表”中的用户名和密码数据。 但是,我遇到了“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();

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

【问题讨论】:

    标签: c#


    【解决方案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);
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-03
        • 2011-07-22
        • 2012-08-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多