// 代码中引入命名空间的代码极其重要,切勿忘记(using System.Data.SqlClient;)web
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Data.SqlClient; namespace _8_6_无密码链接数据库登陆 { /// <summary> /// Description of MainForm. /// </summary> public partial class MainForm : Form { SqlConnection cn = new SqlConnection("Data Source=DESKTOP-3HVKRJT;Initial Catalog=学生验证和信息系统;Integrated Security=True"); public MainForm() { InitializeComponent(); } void TextBox1TextChanged(object sender, EventArgs e) { if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0) { button1.Enabled = true; } } void TextBox2TextChanged(object sender, EventArgs e) { if (textBox2.Text.Length > 0 && textBox2.Text.Length >0) { button1.Enabled = true; } } void Button1Click(object sender, EventArgs e) { // 1.链接数据库 cn.Open(); // 2.验证输入是否与数据库记录相同 string select1 = "select * from 用户密码表 where 学号 = '"+textBox1.Text+"' and " + "密码 = '"+textBox2.Text+"'"; SqlCommand com = new SqlCommand(select1,cn); // 3.储存查询结果 SqlDataReader dr1 = com.ExecuteReader(); if (dr1.Read()) { MessageBox.Show("登陆成功。"); } else MessageBox.Show("学号或密码输入错误,请重试。"); cn.Close(); } } }