求教 如何实现登录窗体
这是登录窗体代码AdminManager AdminM = new AdminManager();
private void button1_Click(object sender, EventArgs e)
{
if (!Check() || !Checky())
{ return; }
try
{
string name = textBox1.Text.Trim();
string pWord = textBox2.Text.Trim();
if (AdminM.CheckAdmin(name, pWord))
{
MainForm2 main = new MainForm2();
MainForm2.adminName = this.textBox1.Text.Trim();
main.Show();
this.Hide();//本窗体隐藏
}
else
{
MessageBox.Show("用户或密码不存在", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("用户或密码不存在", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// <summary>
/// 验证用户密码
/// </summary>
/// <returns></returns>
public bool Check()
{
if (this.textBox1.Text.Trim().Equals(string.Empty))
{
MessageBox.Show("用户名不能为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.textBox1.Focus();
return false;
}
else if (this.textBox2.Text.Trim().Equals(string.Empty))
{
MessageBox.Show("密码不能为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.textBox2.Focus();
return false;
}
else
{ return true; }
}
/// <summary>
/// 验证验证码的方法
/// </summary>
/// <returns></returns>
public bool Checky()
{
string usercode = this.txtCode.Text.Trim().ToLower();//用户输入的验证码
string systemcode = new ValidateImage().GetValidateCode().ToLower();//系统生成的验证码
if (usercode == null || usercode == "")//判断验证码是否为空
{
MessageBox.Show("请输入验证码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.txtCode.Focus();
return false;
}
else
{
if (usercode.Equals(systemcode))//判断验证码是否正确
{
return true;
}
else
{
MessageBox.Show("验证失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.txtCode.Text = "";
this.txtCode.Focus();
SetImage();
return false;
}
}
}
/// <summary>
/// 用户登录验证方法
/// </summary>
/// <param name="Name">用户名</param>
/// <param name="PWord">用户密码</param>
/// <returns></returns>
public bool CheckAdmin(string Name, string PWord)
{
string sql = "select count(*) from Admin as a where a.adminName=@Name and a.adminPassWord=@PWord";
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter ("@Name",Name),
new SqlParameter ("@PWord",PWord)
};
//创建command命令对象
try
{
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddRange(para);
connection.Open();
//执行查询语句
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
reader.Close();
return true;
}
else
{
reader.Close();
return false;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name ="conn" connectionString="Data Source=.;Initial Catalog=HR_personnel;Persist Security Info=True;User ID=sa;Password=123"/>
</connectionStrings>
</configuration>
哪位大神帮忙看看 我登陆的时候总是失败! --------------------编程问答-------------------- 怎么没人呀
哪位大神帮帮忙呀 --------------------编程问答-------------------- 加断点,调试就知道是哪有问题了 --------------------编程问答-------------------- --------------------编程问答-------------------- 楼上+1,这你加个断点,调试一下,很容易发现问题的。从登录时间开始追踪,一直F10,有问题就看下。你把问题找出来了,再贴上来,比这样直接贴一堆代码给大家看来的效率的多。 --------------------编程问答-------------------- 调试的时候 提示 sa登录失败
但是我配置文件和数据库里的用户名设置的都是 sa
怎么解决啊 --------------------编程问答-------------------- 你打开数据库
用sa 123能登录吗? --------------------编程问答-------------------- 控件判断为空请用 this.textBox1.Text!=""就可以了。
你这种写法是错误的this.textBox2.Text.Trim().Equals(string.Empty) 控件值不会有Empty的所以这些都不会成立。
比较通用的写法是用 string.IsNullOrEmpty来判断空值。 --------------------编程问答-------------------- 数据库用本地连接和 sa 都能连上 --------------------编程问答--------------------
貌似这样可以啊 我的没问题啊 --------------------编程问答-------------------- connection 指向的是conn吗? sa登录失败 肯定是你的连接字符串出问题啊 --------------------编程问答-------------------- 你把配置文件中连接串的Persist Security Info=True;删掉试试
补充:.NET技术 , C#