一个解决方案,三个项目,分别了 "LOGIN" "采购部" "仓储部"
会员表如下:
uid username password kind
1 111111 111111 仓储
2 222222 222222 采购
下边源码是 LOGIN 项目中 Login.cs 登录窗口 检测输入的用户名和密码后,如正确匹配,取出 Kind 字段值
根据 kind 的值 登录 "采购部"项目主窗口 Main.cs 或 "仓储部"项目 的 主窗口 Main.cs
namespace LOGIN
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
this.btnOK.Enabled = false; //初始化登录按钮不可用
this.txtUsername.Tag = false; //初始用户名控件属性
this.txtPassword.Tag = false; //初始密码控件属性
}
string mKind = "";
private void Login_Load(object sender, EventArgs e)
{
}
private void btnOK_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Class1.connectString); //创建链接
string sql = "SELECT * FROM [会员表] WHERE (username ='" + txtUsername.Text.Trim().Replace("'", "_") + "')"; //定义SQL语句
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
//判断用户是否存在
if (!reader.HasRows)
{
MessageBox.Show("该账号不存在!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUsername.Text = string.Empty;
txtPassword.Text = string.Empty;
txtUsername.Focus();
return;
}
while (reader.Read())
{
//判断此用户的密码是否与数据库中的匹配
if (reader["password"].ToString().Trim() != txtPassword.Text.Trim().Replace("'", "_"))
{
MessageBox.Show("难道你忘记了密码了吗?", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPassword.Text = string.Empty;
txtPassword.Focus();
return;
}
else
{
//匹配时,取出Kind字段值
mKind = reader["kind"].ToString().Trim();
}
}
switch (mKind)
{
case "仓储":
.................
.................
break;
case "采购":
.................
.................
break;
default:
MessageBox.Show("您不是本单位成员!!!", "登录超出界限!", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
reader.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, "提示::", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
//关闭数据库连接
con.Close();
}
}
#region 控件验证
//---------------------获取下一个焦点前验证-------------------------
private void txtUsername_Validating(object sender, CancelEventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.Length < 6)
{
tb.BackColor = Color.Yellow;
tb.Tag = false;
}
else
{
tb.BackColor = System.Drawing.SystemColors.Window;
tb.Tag = true;
}
ValidateOK();
}
//----------------控件更改Text属性值时引发------------------------
private void txtUsername_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.Length < 6)
{
tb.Tag = false;
tb.BackColor = Color.Yellow;
}
else
{
tb.Tag = true;
tb.BackColor = SystemColors.Window;
}
ValidateOK();
}
//----------------根据控件Tag属性来启用btnOK按钮------------------------
private void ValidateOK()
{
this.btnOK.Enabled = ((bool)(this.txtUsername.Tag) && (bool)(this.txtPassword.Tag));
}
#endregion
}
}
求红色省略号处代码
[此贴子已经被作者于2007-7-2 14:51:08编辑过]