新手,用SQL2008做了一个登陆界面,注册,登陆没问题,但是登陆时密码输入次数不增加,求解!
//登录按钮
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
int errortimes = sqlHelper.checkErrorTimes(txtUserName.Text.Trim());
if (errortimes >= 3)
{
MessageBox.Show("输入次数超过三次,用户已锁定!");
return;
}
if (sqlHelper.check(txtUserName.Text.Trim(), pbPassWord.Password.Trim()))
{
Comsume comWin = new Comsume();
comWin.Show();
this.Close();
}
else
{
sqlHelper.errorTimes(++errortimes, txtUserName.Text.Trim());
//密码输错后感觉数据库中的ErrorTimes不增加!
MessageBox.Show("登录失败,用户名或密码不正确!");
}
}以下为sqlHelper语句(数据库连接语句均已放到配置文件中,测试连接没问题,登陆也没问题,就是密码输错次数不更新)
//更新用户输入密码错误次数
public static void errorTimes(int i, string name)
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = string.Format("update T_Users set ErrorTimes={0} where UserName='{1}'", i, name);
conn.Open();
conn.Close();
}
}
//检测用户是否输入错误超过三次
public static int checkErrorTimes(string name)
{
using (SqlCommand cmd = new SqlCommand())
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.SelectCommand.Connection = conn;
conn.Open();
cmd.CommandText=string.Format("select * from T_Users where UserName='{0}'", name);
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable tb1 =ds.Tables[0];
DataRow row = tb1.Rows[0];
conn.Close();
int result=Convert.ToInt32(row["ErrorTimes"]);
return result;
}
}SQLnetc#登陆密码
//登录按钮
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
int errortimes = sqlHelper.checkErrorTimes(txtUserName.Text.Trim());
if (errortimes >= 3)
{
MessageBox.Show("输入次数超过三次,用户已锁定!");
return;
}
if (sqlHelper.check(txtUserName.Text.Trim(), pbPassWord.Password.Trim()))
{
Comsume comWin = new Comsume();
comWin.Show();
this.Close();
}
else
{
sqlHelper.errorTimes(++errortimes, txtUserName.Text.Trim());
//密码输错后感觉数据库中的ErrorTimes不增加!
MessageBox.Show("登录失败,用户名或密码不正确!");
}
}以下为sqlHelper语句(数据库连接语句均已放到配置文件中,测试连接没问题,登陆也没问题,就是密码输错次数不更新)
//更新用户输入密码错误次数
public static void errorTimes(int i, string name)
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = string.Format("update T_Users set ErrorTimes={0} where UserName='{1}'", i, name);
conn.Open();
conn.Close();
}
}
//检测用户是否输入错误超过三次
public static int checkErrorTimes(string name)
{
using (SqlCommand cmd = new SqlCommand())
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.SelectCommand.Connection = conn;
conn.Open();
cmd.CommandText=string.Format("select * from T_Users where UserName='{0}'", name);
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable tb1 =ds.Tables[0];
DataRow row = tb1.Rows[0];
conn.Close();
int result=Convert.ToInt32(row["ErrorTimes"]);
return result;
}
}SQLnetc#登陆密码
解决方案 »
- 问一特弱智的问题,谢谢大家。总是出错
- INSERT 语句中列的数目大于 VALUES 子句中指定的值的数目。VALUES 子句中值的数目必须与 INSERT 语句中指定的列的数目匹配。
- 刚看的java社区第一页内容
- 一个关于winform NetworkStream发送和接收文件的问题
- 怎么用代码实现在本机器上开启Guest帐户并添加一个供其他机器访问的Guest帐户啊??
- 以前做的一个web的系统,现在导入后有问题出现!谁帮忙解决下!(回答给分)
- 在winform中如何让groupBox 和panel变成透明
- 我是初学者,请问:DateTable表怎么去释放它所站空间?
- C# 如何使用SFTP 上传文件并支持断点续传
- 读取Excel到DataTable无法获取数字
- 如何用反射获取属性的子属性的值
- 没有用的成员,如何取消警告
//更新用户输入密码错误次数
public static void errorTimes(int i, string name)
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = string.Format("update T_Users set ErrorTimes={0} where UserName='{1}'", i, name);
conn.Open();
conn.Close();
}
}cmd都没执行怎么更新数据库?
cmd执行了,二楼的是正确的,errortimes没有声明为全局变量,不过还是感谢回答~
public static void errorTimes(int i, string name)
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("update T_Users set ErrorTimes={0} where UserName='{1}'", i, name);
conn.Open();
conn.Close();
}
}