【发布时间】: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#