winform窗体应用管理员登录验证
首先创建一个管理员类,声明字段 并封装 生成构造方法 如图:

第二添加DBhelper类的封装 如下:
namespace Wvidin { //数据库相关操作 验证登录 public class DBhelper { //数据库连接 public static string sqlcutn= "Data Source=.;Initial Catalog=vicin;Integrated Security=True"; //创建一个返回值为管理员类的方法 并传入账户和密码两个参数 public static vicin_sing Vincin(string number,string pwd) { //创建vicin_sing类的对象 使其等于空 vicin_sing vicin_s = null; //sql语句 string sql = string.Format("select*form vicin_sing where number='{0}' and pwd='{1}'", number, pwd); //创建连接对象 SqlConnection con = new SqlConnection(sqlcutn); //打开连接 con.Open(); //创建执行对象 SqlCommand cmd = new SqlCommand(sql, con); //执行操作 SqlDataReader reader = cmd.ExecuteReader(); //判断是否查到数据 if (reader.Read())//存在数据 { vicin_s = new vicin_sing(number ,pwd); //将数据记录封装到Administrvicin对象中 vicin_s.Id = int.Parse(reader["id"].ToString()); vicin_s.Number = reader["number"].ToString(); vicin_s.Pwd = reader["pwd"].ToString(); } //关闭连接 reader.Close(); con.Close(); return vicin_s; } } }
在窗体登录验证界面中

namespace Wvidin { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //获取文本框的内容 string number = this.textBox1.Text; string pwd = this.textBox2.Text; //用管理员类的对象调用DBhelper类 vicin_sing vicin_ = DBhelper.Vincin(number, pwd); //判断 if (vicin_==null)//如果等于空则DBhelper查询不到 { MessageBox.Show("账户或密码错误"); } else { //窗体跳转 Form2 form2 = new Form2(); form2.Show(); this.Hide(); } } } }

浙公网安备 33010602011771号