c#基础在winform操作数据库,实现增删改查
1.数据库操作类代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Configuration; using System.Data.SqlClient; using System.Security.Cryptography; namespace Data { class SqlDesigner { private static string connStr = ConfigurationManager.ConnectionStrings[ "data" ].ConnectionString; /// <summary> /// 返回受影响的数据行数 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int ExecuteNoQuery( string sql) { using (SqlConnection conn= new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd=conn.CreateCommand()) { cmd.CommandText = sql; return cmd.ExecuteNonQuery(); } } } /// <summary> /// 返回一个数据集 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataSet ExecuteDataSet( string sql) { using (SqlConnection xonn= new SqlConnection(connStr)) { xonn.Open(); using (SqlCommand cmd = xonn.CreateCommand()) { cmd.CommandText = sql; SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet dataset = new DataSet(); adapter.Fill(dataset); return dataset; } } } public static object ExecuteScalar( string sql) { using (SqlConnection conn= new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd=conn.CreateCommand()) { cmd.CommandText = sql; return cmd.ExecuteScalar(); } } } /// <summary> /// md5加密 /// </summary> /// <param name="strPwd"></param> /// <returns></returns> public static string GetMD5( string strPwd) { string pwd = "" ; //实例化一个md5对象 MD5 md5 = MD5.Create(); // 加密后是一个字节类型的数组 byte [] s = md5.ComputeHash(Encoding.UTF8.GetBytes(strPwd)); //翻转生成的MD5码 s.Reverse(); //通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得 //只取MD5码的一部分,这样恶意访问者无法知道取的是哪几位 for ( int i = 3; i < s.Length - 1; i++) { //将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 //进一步对生成的MD5码做一些改造 pwd = pwd + (s[i] < 198 ? s[i] + 28 : s[i]).ToString( "X" ); } return pwd; } } } |
2.程序代码 :

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; namespace Data { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataSet ds = new DataSet(); DataTable dt = new DataTable(); private void TextBoxNull() { textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; } private void 用户ToolStripMenuItem_Click(object sender, EventArgs e) { TextBoxNull(); ds = SqlDesigner.ExecuteDataSet("select * from dtuser"); dt = ds.Tables[0]; dataGridView1.DataSource = dt; labelshow(); } private void 角色ToolStripMenuItem_Click(object sender, EventArgs e) { TextBoxNull(); ds = SqlDesigner.ExecuteDataSet("select *from dtrole"); dt = ds.Tables[0]; dataGridView1.DataSource = dt; label4.Text = "None"; textBox4.Text = "None"; labelshow(); } private void 对象ToolStripMenuItem_Click(object sender, EventArgs e) { TextBoxNull(); ds = SqlDesigner.ExecuteDataSet("select * from dtfunction"); dt = ds.Tables[0]; dataGridView1.DataSource = dt; labelshow(); } private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e) { TextBoxNull(); ds = SqlDesigner.ExecuteDataSet("select * from help"); dt = ds.Tables[0]; dataGridView1.DataSource = dt; dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; } //双击dataGridView1 private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { string index = dataGridView1.CurrentRow.Cells[0].Value.ToString(); if (label1.Text == "uid") { ds = SqlDesigner.ExecuteDataSet("select *from dtuser where uid='" + index + "'"); dt = ds.Tables[0]; DataRow row = dt.Rows[0]; textBox1.Text = row["uid"].ToString(); textBox2.Text = row["uname"].ToString(); textBox3.Text = row["pwd"].ToString(); textBox4.Text = row["uflag"].ToString(); } if (label1.Text == "rid") { ds = SqlDesigner.ExecuteDataSet("select *from dtrole where rid='" + index + "'"); dt = ds.Tables[0]; DataRow row = dt.Rows[0]; textBox1.Text = row["rid"].ToString(); textBox2.Text = row["rname"].ToString(); textBox3.Text = row["flag"].ToString(); textBox4.Text = "None"; } if (label1.Text == "fid") { ds = SqlDesigner.ExecuteDataSet("select *from dtfunction where fid='" + index + "'"); dt = ds.Tables[0]; DataRow row = dt.Rows[0]; textBox1.Text = row["fid"].ToString(); textBox2.Text = row["fname"].ToString(); textBox3.Text = row["flag"].ToString(); textBox4.Text = row["uflag"].ToString(); } } private void labelshow() { label1.Text = dataGridView1.Columns[0].HeaderText; label2.Text = dataGridView1.Columns[1].HeaderText; label3.Text = dataGridView1.Columns[2].HeaderText; try { label4.Text = dataGridView1.Columns[3].HeaderText; } catch (Exception) { label4.Text = "None"; } } private void btn_add_Click(object sender, EventArgs e) { int i = 0; if (label1.Text=="uid") { string str = SqlDesigner.GetMD5(textBox3.Text.Trim()); i = SqlDesigner.ExecuteNoQuery("insert into dtuser(uid,uname,pwd,uflag)values('" + textBox1.Text + "','" + textBox2.Text + "','" + str + "','" + textBox4.Text + "')"); } else if (label1.Text == "rid") { i = SqlDesigner.ExecuteNoQuery("insert into dtrole(rid,rname,flag)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')"); } else { try { i = SqlDesigner.ExecuteNoQuery("insert into dtfunction(fid,rid,uid,uflag)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')"); } catch (Exception) { MessageBox.Show("添加失败"); } } if (i > 0) { MessageBox.Show("添加成功"); } else { MessageBox.Show("添加失败"); } } private void btn_del_Click(object sender, EventArgs e) { int i = 0; string currentIndex = dataGridView1.CurrentRow.Cells[0].Value.ToString(); if (label1.Text=="uid") { i = SqlDesigner.ExecuteNoQuery("delete from dtuser where uid='" + currentIndex + "'"); } else if (label1.Text=="fid") { i = SqlDesigner.ExecuteNoQuery("delete from dtfunction where fid='" + currentIndex + "'"); } else { i = SqlDesigner.ExecuteNoQuery("delete from dtrole where rid='" + currentIndex + "'"); } if (i > 0) { MessageBox.Show("删除成功"); } else { MessageBox.Show("删除失败"); } } private void btn_update_Click(object sender, EventArgs e) { int i = 0; if (label1.Text == "rid") { i = SqlDesigner.ExecuteNoQuery("update dtrole set rname='" + textBox2.Text + "',flag='" + textBox3.Text + "'where rid='" + textBox1.Text + "'"); } if (label1.Text == "uid") { i = SqlDesigner.ExecuteNoQuery("update dtuser set uname='" + textBox2.Text + "',pwd='" + textBox3.Text + "',uflag='" + textBox4.Text + "'where uid='" + textBox1.Text + "'"); } if (label1.Text=="fid") { i = SqlDesigner.ExecuteNoQuery("update dtfunction set rid='" + textBox2.Text + "',uid='" + textBox3.Text + "',uflag='" + textBox4.Text + "'where fid='" + textBox1.Text + "'"); } if (i > 0) { MessageBox.Show("Succeed!"); } else { MessageBox.Show("Failed!"); } } } } View All Code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)