Winform 连接数据库 增删改查基本操作 新建文件夹 App_Code 添加两个类: UserDA.cs(封装表中字段) User.Data.cs(书写连接数据库,增删改查的操作方法)
using WindowsFormsApplication7.App_Code; namespace WindowsFormsApplication7 { public partial class Form4 : Form { public Form4() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Insert f = new Insert(); f.Owner = this; f.Show(); } private void Form4_Load(object sender, EventArgs e) { List<UserData> list = new UserDA().Select(); Binddata(list); } private void Binddata(List<UserData> list) { //第一步:构建listview"‘ listView1.Columns.Clear(); listView1.Columns.Add("编号"); listView1.Columns.Add("用户名"); listView1.Columns.Add("密码"); listView1.Items.Clear(); //读取数据,通过数据构建listview中的行 foreach (UserData data in list) { ListViewItem lt = new ListViewItem(); lt.Text = data.Code.ToString(); lt.SubItems.Add(data.Uname); lt.SubItems.Add(data.UPass); listView1.Items.Add(lt); } } private void button4_Click(object sender, EventArgs e) { List<UserData> listtiaojian = new UserDA().Select(textBox1.Text); Binddata(listtiaojian); } private void button3_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { string code = listView1.SelectedItems[0].Text; new UserDA().Delete(code); //重新绑定数据 List<UserData> list = new UserDA().Select(); Binddata(list); } else { MessageBox.Show("请选择,再删除"); } } private void button2_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { UserData data = new UserData(); data.Code = int.Parse(listView1.SelectedItems[0].SubItems[0].Text); data.Uname = listView1.SelectedItems[0].SubItems[1].Text; data.UPass = listView1.SelectedItems[0].SubItems[2].Text; Update f = new Update(data); f.Show(); } else { MessageBox.Show("请选择,再修改"); } } } }
App-code userdata.cs
namespace WindowsFormsApplication7.App_Code { public class UserData { private int _Code; public int Code { get { return _Code; } set { _Code = value; } } private string _Uname; public string Uname { get { return _Uname; } set { _Uname = value; } } private string _UPass; public string UPass { get { return _UPass; } set { _UPass = value; } } } }
App_code UserDA().cs
using System.Data.SqlClient; namespace WindowsFormsApplication7.App_Code { public class UserDA { private SqlConnection _Conn; private SqlCommand _Cmd; public UserDA() { _Conn = new SqlConnection("server=.;database=data1220;user=sa;pwd="); } //添加方法 public void Insert(UserData data) { _Conn.Open(); _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "insert into users values(@uname,@upass)"; _Cmd.Parameters.Clear(); _Cmd.Parameters.Add("@uname",data.Uname); _Cmd.Parameters.Add("@upass", data.UPass); _Cmd.ExecuteNonQuery(); _Conn.Close(); } //查询方法 public List<UserData> Select() { List<UserData> list = new List<UserData>(); _Conn.Open(); _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "select *from users"; SqlDataReader dr = _Cmd.ExecuteReader(); while (dr.Read()) { UserData data = new UserData(); data.Code = int.Parse(dr["code"].ToString()); data.Uname = dr["uname"].ToString(); data.UPass = dr["upass"].ToString(); list.Add(data); } return list; } /// <summary> /// 根据用户名查 /// </summary> /// <returns></returns> public List<UserData> Select(string uname) { List<UserData> list = new List<UserData>(); _Conn.Open(); _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "select *from users where uname like ‘%"+uname+"%‘"; SqlDataReader dr = _Cmd.ExecuteReader(); while (dr.Read()) { UserData data = new UserData(); data.Code = int.Parse(dr["code"].ToString()); data.Uname = dr["uname"].ToString(); data.UPass = dr["upass"].ToString(); list.Add(data); } return list; } public void Delete(string code) { _Conn.Open(); _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "delete from users where code="+code; _Cmd.ExecuteNonQuery(); _Conn.Close(); } public void update(UserData data) { _Conn.Open(); _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "update users set [email protected],[email protected] where [email protected]"; _Cmd.Parameters.Clear(); _Cmd.Parameters.Add("@code",data.Code); _Cmd.Parameters.Add("@uname",data.Uname); _Cmd.Parameters.Add("@upass",data.UPass); _Cmd.ExecuteNonQuery(); _Conn.Close(); } } }
时间: 2024-10-12 03:59:36