【发布时间】:2015-12-16 11:07:16
【问题描述】:
我不断收到此错误消息,说"ExecuteScalar has not been initialized" 我是 C# 新手,但通过 google 和教程查看了一下,仍然看不到问题所在。这可能是一个非常愚蠢的错误,但如果有人可以提供帮助。谢谢:)
// open connection
myConnection.Open();
// sql command
string Account_Num = txt_acc.Text;
string Pin_num = txt_pin.Text;
SqlCommand check_details = new SqlCommand("select Account_num, Pin_num from Cust_details where Account_num='" + txt_acc.Text + "'and Pin_num ='" + txt_pin.Text + "'");
check_details.Parameters.AddWithValue("@Account_num", txt_acc.Text);
check_details.Parameters.AddWithValue("@Pin_num", txt_pin.Text);
int result = Convert.ToInt32(check_details.ExecuteScalar());
if (result > 0)
{
Console.WriteLine("user exists");
}
else
{
Console.WriteLine("error");
}
}
【问题讨论】:
-
如果您在网上搜索过实际异常,您可能会找到duplicate。 :)
-
您需要为您的命令提供连接名称。还有为什么在参数中添加 acco_num 和 pin_num 时要在查询中添加它们?
标签: c#