当尝试通过 LocalDB 在我的程序中注册一个新帐户时,我收到一条错误消息:
ExecuteNonQuery 需要一个打开且可用的连接。连接的当前状态是关闭的
但如您所见,我已经打开了连接
我已经尝试在多个地方设置连接字符串但无济于事
// The methods whh will be called if the user tries to log in or register
private void LoginRegisterClick(object sender, EventArgs e)
{
// The following lines of code will execute if the user tries to register
if (lblView.Text == "Register")
{
// If all the textboxes have passed validation, the details from the textboxes are retrieved and stored
if (ucRegister1.AllFieldsValidated() == true)
{
string[] details = ucRegister1.ReturnRegisterDetails();
// (cmdCountUsernames) Checks to see if new username is unique
Helper.ConnectionToDB().Open();
SqlCommand cmdCU = new SqlCommand(@"SELECT COUNT(*) FROM LoginsTable WHERE Login = '" + details[6] + "'", Helper.ConnectionToDB());
try
{
int count = (int)cmdCU.ExecuteScalar();
//int count = Convert.ToInt32(cmdCU.ExecuteScalar());
// If the new username is unique, the record is added into MainParentTable
if (count == 0)
{
try
{
// Order of the details:
// details[(0)Firstname, (1)Surname, (2)DOB, (3)HomeTel, (4)MobileTel, (5)Address, (6)Username, (7)Password, (8)Email]
}
catch (Exception msg2)
{
MessageBox.Show("Error Message 2: " + msg2);
}
}
}
应根据查询结果将计数设置为 0 或 1
RISEBY
相关分类