class DBHelper
{
private string connString = @"Data Source=LAPTOP-ODTAIH4A;Initial Catalog=MySchool;Integrated Security=True";
private SqlConnection connection;
public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connString);
}
return connection;
}
}
public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
public void CloseConnection()
{
if (Connection.State == ConnectionState.Open || connection.State == ConnectionState.Broken)
{
connection.Close();
}
}
}
}
public partial class Form1 : Form
{
public const string ADMIN = "系统管理员";
public const string CAPTION = "输入提示";
public Form1()
{
InitializeComponent();
}
public bool denglu(ref string message)
{
bool a = false;
string userName = yonghuming.Text.Trim();
string userPwd = mima.Text.Trim();
StringBuilder sb = new StringBuilder();
if (this.leixing.Text.Equals(ADMIN))
{
sb.AppendFormat("select count(*) from admin" + " where loginId='{0}' and loginpwd='{1}'", userName, userPwd);
}
int count = 0;
DBHelper db = new DBHelper();
try
{
SqlCommand comm = new SqlCommand(sb.ToString(),db.Connection);
db.OpenConnection();
count = (int)comm.ExecuteScalar();
if (count > 0)
{
a = true;
}
else
{
message = "用户名或密码不存在";
a = false;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
a = false;
}
finally
{
db.CloseConnection();
}
return a;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public bool CheckInput()
{
if (this.yonghuming.Text.Trim().Equals(string.Empty))
{
MessageBox.Show("请输入用户名", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.yonghuming.Focus();
return false;
}
else if (this.mima.Text.Trim().Equals(string.Empty))
{
MessageBox.Show("请输入密码", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.yonghuming.Focus();
return false;
}
else if (this.leixing.Text.Trim().Equals(string.Empty))
{
MessageBox.Show("请输入登陆类型", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.yonghuming.Focus();
return false;
}
else
{
return true;
}
}
private void btndenglu_Click(object sender, EventArgs e)
{
string message = string.Empty;
if (CheckInput() && denglu(ref message))
{
if (this.leixing.Text.Equals(ADMIN))
{
FrmAdminMain fraAdmin = new FrmAdminMain();
MessageBox.Show("登陆成功", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
fraAdmin.userId = "管理员主窗体";
fraAdmin.Show();
}
}
else {
MessageBox.Show("登陆失败", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnquxiao_Click(object sender, EventArgs e)
{
DialogResult r= MessageBox.Show("确认退出?","取消",MessageBoxButtons.YesNo,MessageBoxIcon.Hand);
if (r == DialogResult.Yes)
{
this.Close();
}
else {
return;
}
}
public partial class FrmAdminMain : Form
{
public string userId=string.Empty;
public FrmAdminMain()
{
InitializeComponent();
}
private void FrmAdminMain_Load(object sender, EventArgs e)
{
this.textBox1.Text = userId;
}
}