ExecuteNonQuery: Connection 属性尚未初始化.
privatevoidResetErrorTimes(){using(SqlConnectionconn=newSqlConnection(@"DataSource=4T...
private void ResetErrorTimes()
{
using (SqlConnection conn = new SqlConnection(@"Data Source=4TX4MZYM9MMIQGB;Initial Catalog=mydb1;Integrated Security=True"))
{
conn.Open();
using (SqlCommand updateCmd = new SqlCommand())
{
updateCmd.CommandText = "update t_user1 set errortimes=0 where username=@username";
updateCmd.Parameters.Add(new SqlParameter("username", txtUserName.Text));
updateCmd.ExecuteNonQuery();
}
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(@"Data Source=4TX4MZYM9MMIQGB;Initial Catalog=mydb1;Integrated Security=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from t_user1 where username=@username";
cmd.Parameters.Add(new SqlParameter("username",txtUserName.Text));
using(SqlDataReader reader=cmd.ExecuteReader())
{
if (reader.Read())
{
int errortimes=reader.GetInt32(reader.GetOrdinal("errortimes"));
if (errortimes>3)
{
MessageBox.Show("登陆错误次数过多,禁止登陆");
return;
}
string dbpassword = reader.GetString(reader.GetOrdinal("password"));
if (dbpassword == txtPassWord.Text)
{
MessageBox.Show("登陆成功");
ResetErrorTimes();
}
else
{
IncErrorTimes();
MessageBox.Show("密码错误,登陆失败");
}
}
else
{
MessageBox.Show("用户名不存在");
}
}
}
} 展开
{
using (SqlConnection conn = new SqlConnection(@"Data Source=4TX4MZYM9MMIQGB;Initial Catalog=mydb1;Integrated Security=True"))
{
conn.Open();
using (SqlCommand updateCmd = new SqlCommand())
{
updateCmd.CommandText = "update t_user1 set errortimes=0 where username=@username";
updateCmd.Parameters.Add(new SqlParameter("username", txtUserName.Text));
updateCmd.ExecuteNonQuery();
}
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(@"Data Source=4TX4MZYM9MMIQGB;Initial Catalog=mydb1;Integrated Security=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from t_user1 where username=@username";
cmd.Parameters.Add(new SqlParameter("username",txtUserName.Text));
using(SqlDataReader reader=cmd.ExecuteReader())
{
if (reader.Read())
{
int errortimes=reader.GetInt32(reader.GetOrdinal("errortimes"));
if (errortimes>3)
{
MessageBox.Show("登陆错误次数过多,禁止登陆");
return;
}
string dbpassword = reader.GetString(reader.GetOrdinal("password"));
if (dbpassword == txtPassWord.Text)
{
MessageBox.Show("登陆成功");
ResetErrorTimes();
}
else
{
IncErrorTimes();
MessageBox.Show("密码错误,登陆失败");
}
}
else
{
MessageBox.Show("用户名不存在");
}
}
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询