如果你能将你的问题描述清除一点的话,我想会有人愿意帮助你~~
面是我最近几天刚弄好的代码,它能够判断 是否输入了用户名和密码 以及 输入的是否真确!!
当然还能加入“连续登录错误 N 次 系统自动关闭”以及其他的一些功能,谁想加入的可以互相讨论啊
现在共享一下:
private void button1_Click(object sender, EventArgs e)//button1的Text是:登录
{
if (this.textBox1.Text == "")
{
MessageBox.Show("用户名不能为空!", "错误");
}
else if (this.textBox2.Text == "")
{
MessageBox.Show("密码不能为空!", "错误");
}
else
{
if (IsUser(this.textBox1.Text))
{
if (this.textBox2.Text == LoginUser(this.textBox1.Text))
{
Form2 frm2 = new Form2();
// //frm2.showdialog();
frm2.Show();
Close();
}
else
{
MessageBox.Show("密码错误!");
}
}
else
{
MessageBox.Show("用户名错误!");
}
}
}
private string LoginUser(string uName) //检测用户是否能够登录
{
DataSet ds = new DataSet();
string u = uName;
OleDbConnection conn = new OleDbConnection("Data Source=F:\\c#.net\\学生信息管理\\denglu.mdb;provider=Microsoft.Jet.OLEDB.4.0");//连接数据库,我用的是Access
OleDbDataAdapter daAuthors = new OleDbDataAdapter("Select * From denglubiao where userName='" + uName + "'", conn);
conn.Open();
daAuthors.Fill(ds, "denglubiao");
return ds.Tables[0].Rows[0]["userPassWD"].ToString();
//SqlParameter parName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
// parName.Value = User;
OleDbParameter parName = new OleDbParameter("@Name", OleDbType.VarChar, 50);
conn.Close();
}
private bool IsUser(string uName) //检测用户输入的用户名是否存在
{
DataSet ds = new DataSet();
OleDbConnection conn = new OleDbConnection("Data Source=F:\\c#.net\\学生信息管理\\denglu.mdb;provider=Microsoft.Jet.OLEDB.4.0");//连接数据库
OleDbDataAdapter daAuthors = new OleDbDataAdapter("Select userPassWD From denglubiao where userName='" + uName + "'", conn);
conn.Open();
daAuthors.Fill(ds, "denglubiao");
int n;
n = ds.Tables[0].Rows.Count;
if (n > 0)
return true;//存在就返回true
else
return false;
conn.Close();
}
private void button2_Click(object sender, EventArgs e)//button2的 Text属性是:取消登录
{
Application.Exit();//用户取消登录,退出系统
}
转别人的
private void btn_ok_Click(object sender, System.EventArgs e)
{
string strUser,strPassword,strDept_id;
strUser=this.tbnusername.Text.ToString().Trim();
strPassword=this.tbnpassword.Text.ToString().Trim();
strDept_id=this.tbndept_id.Text.ToString().Trim();
if(strUser=="")
{
MessageBox.Show( "用户名不能为空!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
if(strPassword=="")
{
MessageBox.Show( "密码不能为空!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
if(strDept_id=="")
{
MessageBox.Show( "部门号不能为空!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
// string strselect="SELECT username,password,dept_id FROM companydb.login WHERE username='"+strUser+"'";
// string source="Server=(LOCAL)\\NetSDK;Integrated security=SSPI;Database=companydb";
// string source="Server=127.0.0.1;uid=sa;pwd=123;database=companydb";
// string mysource="Data Source=172.16.5.37;Initial Catalog=companydb;Integrated Security=SSPI";
string mysource="Data Source=HOBBY;Initial Catalog=companydb;Integrated Security=SSPI";
SqlConnection conn=new SqlConnection(mysource);
try
{
conn.Open();
}
catch
{
MessageBox.Show("连接数据库失败","提示",MessageBoxButtons.OK ,MessageBoxIcon.Stop );
this.Cursor = Cursors.Default ;
return;
}
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText="Select * from login where username='"+strUser+"'";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (dr["password"].ToString().Trim() ==strPassword && dr["dept_id"].ToString().Trim()==strDept_id )
{
// Form Form5=new Form();
// Form5.Show();
MessageBox.Show("系统登陆成功!");
Form4 Form_4=new Form4();
Form_4.Show();
// MessageBox.Show("Form5");
this.Hide();
}
else
{
MessageBox.Show("您输入的用户信息或密码不正确!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("没有此用户");
}
dr.Close();
conn.Close();
return;
}