using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MySchool { public partial class AdminFrom : Form { public string user = string.Empty; public AdminFrom() { InitializeComponent(); } private void AdminFrom_Load(object sender, EventArgs e) { textName.Text = user; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using System.Data; namespace MySchool { class DBHelper { private string strConn = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; private SqlConnection conn; public SqlConnection conntection { get { if (conn == null) { conn = new SqlConnection(strConn); } return conn; } } public void OpenConnection() { if (conntection.State == ConnectionState.Closed) { conntection.Open(); } else if(conntection.State == ConnectionState.Broken) { conntection.Close(); conntection.Open(); } } public void CloseConnection() { if (conntection.State == ConnectionState.Open || conntection.State == ConnectionState.Broken) { conntection.Close(); } } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace MySchool { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("确定取消登录码", "操作显示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { this.Close(); } } private void Form1_Click(object sender, EventArgs e) { if(this.BackColor==Color.Red) { this.BackColor = Color.Yellow; } else if (this.BackColor == Color.Yellow) { this.BackColor = Color.Green; } else { this.BackColor = Color.Red; } } public const string CAPTION = "输入提示"; public const string ANDMIN= "管理员"; private void button2_Click(object sender, EventArgs e) { if (CheckInput()) { string message = string.Empty; if (CheckUser(ref message)) { if (this.cboLoginType.Text.Equals(ANDMIN)) { AdminFrom a = new AdminFrom(); a.user = textUserName.Text.Trim(); a.Show(); } this.Hide(); } } } public bool CheckInput() { if (this.textUserName.Text.Trim().Equals(string.Empty)) { MessageBox.Show("请输入用户名", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information); this.textUserName.Focus(); return false; } else if (this.textPwd.Text.Trim().Equals(string.Empty)) { MessageBox.Show("请输入密码", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information); this.textPwd.Focus(); return false; } else if (this.cboLoginType.Text.Trim().Equals(string.Empty)) { MessageBox.Show("请选择数据类型", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information); this.cboLoginType.Focus(); return false; } else { return true; } } public bool CheckUser(ref string message) { bool i = false; string userName = textUserName.Text.Trim(); string userPwd = textPwd.Text.Trim(); StringBuilder sb = new StringBuilder(); if (cboLoginType.Text.Equals(ANDMIN)) { 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.conntection); db.OpenConnection(); count = (int)comm.ExecuteScalar(); if (count > 0) { i = true; } else { MessageBox.Show( "用户或密码不正确"); i = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show( "系统发生错误,请稍后重试"); i = false; } finally { db.CloseConnection(); } return i; }
C#开发数据库第一章上机题
最新推荐文章于 2022-09-13 23:09:10 发布