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(); } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异