C# 数据库链接
private void button_queding_Click(object sender, EventArgs e)
{ string zhanghao= textBox_zhanghao.Text, mima= textBox_mima.Text; //创建数据库连接类的对象 SqlConnection con = new SqlConnection(@"Data Source=CFF-PC;Initial Catalog=data1;Integrated Security=True"); //将连接打开 con.Open(); //执行con对象的函数,返回一个SqlCommand类型的对象 SqlCommand cmd = con.CreateCommand(); //把输入的数据拼接成sql语句,并交给cmd对象 cmd.CommandText = "select * from users where username='" + zhanghao + "'and password='" + mima + "'"; //用cmd的函数执行语句,返回SqlDataReader对象dr,dr就是返回的结果集(也就是数据库中查询到的表数据) SqlDataReader dr = cmd.ExecuteReader(); //用dr的read函数,每执行一次,返回一个包含下一行数据的集合dr,在执行read函数之前,dr并不是集合 if (dr.Read()) { //dr[]里面可以填列名或者索引,显示获得的数据 MessageBox.Show(dr[1].ToString()); } //用完后关闭连接,以免影响其他程序访问 con.Close(); }

浙公网安备 33010602011771号