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 登陆链接数据库 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) //链接数据库查询 { string uname = textBox1.Text; string upass = textBox2.Text; //第一步 链接数据库 SqlConnection conn = new SqlConnection("server=.;database=yonghu;user=sa;pwd=123"); conn.Open(); //第二步 写执行语句 SqlCommand cmd = conn.CreateCommand();//sqlcommand 帮助写执行语句 cmd.CommandText = "select *from users where uname='"+uname+"'and upass='"+upass+"'"; SqlDataReader dr = cmd.ExecuteReader();//读取执行语句 返回一个结果集 if(dr.Read()) { MessageBox.Show("登陆成功"); } else { MessageBox.Show("登陆失败"); } conn.Close(); } private void button2_Click(object sender, EventArgs e)//链接数据库添加 { string uname = textBox1.Text; string upass = textBox2.Text; //链接数据库 SqlConnection conn = new SqlConnection("server=.;database=yonghu;user=sa;pwd=123"); conn.Open(); SqlCommand cmd = conn.CreateCommand();//通过conn创建sqlcommand对象 cmd.CommandText = "insert into users values('"+uname+"','"+upass+"')"; int count= cmd.ExecuteNonQuery();//增删改 用executenonquery 执行语句返回影响的行数 if (count > 0) { MessageBox.Show("添加成功"); } else { MessageBox.Show("添加失败"); } conn.Close(); } private void button3_Click(object sender, EventArgs e)//链接数据库修改 { string code = textBox3.Text; string uname = textBox1.Text; string upass = textBox2.Text; //链接数据库 SqlConnection conn = new SqlConnection("server=.;database=yonghu;user=sa;pwd=123"); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "update users set uname='" + uname + "',upass='" + upass + "'where code=" + code; int count = cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("修改成功"); } else { MessageBox.Show("修改失败"); } conn.Close(); } private void button4_Click(object sender, EventArgs e)//链接数据库删除 { string code = textBox3.Text; SqlConnection conn = new SqlConnection("server=.;database=yonghu;user=sa;pwd=123"); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "delete from users where code="+code; int count= cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("删除成功"); } else { MessageBox.Show("删除失败"); } conn.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 增删改 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=yonghu;user=sa;pwd=123"); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select *from users"; SqlDataReader dr = cmd.ExecuteReader(); //开始构建listview表头 listView1.Columns.Clear(); listView1.Columns.Add("编号"); listView1.Columns.Add("姓名"); listView1.Columns.Add("密码"); //读取数据库 根据数据库内容往listview里添加行 listView1.Items.Clear(); while (dr.Read()) { ListViewItem vi = new ListViewItem(); vi.Text = dr["code"].ToString(); vi.SubItems.Add(dr["uname"].ToString()); vi.SubItems.Add(dr["upass"].ToString()); listView1.Items.Add(vi); } conn.Close(); } } }