C#在winform中实现数据增删改查等功能
程序员文章站
2022-06-22 16:05:38
winform中利用ado.net实现对单表的增删改查的详细例子,具体如下:
1.前言:
运行环境:vs2013+sql2008+windows10
程序界面...
winform中利用ado.net实现对单表的增删改查的详细例子,具体如下:
1.前言:
运行环境:vs2013+sql2008+windows10
程序界面预览:
使用的主要控件:datagridview和menustrip等。
2.功能具体介绍:
1.首先,我们要先实现基本的数据操作,增删改查这几个操作。
(1)先定义一个数据库操作的公共类:
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)运用建立的公共类,进行数据库的操作:
a.数据查询:
ds = sqldesigner.executedataset("select * from dtuser"); dt = ds.tables[0]; datagridview1.datasource = dt;
b.数据添加
i = sqldesigner.executenoquery("insert into dtuser(uid,uname,pwd,uflag)values('" + textbox1.text + "','" + textbox2.text + "','" +textbox3.text+ "','" + textbox4.text + "')");
c.数据删除
string currentindex = datagridview1.currentrow.cells[0].value.tostring(); i = sqldesigner.executenoquery("delete from dtuser where uid='" + currentindex + "'");
d.数据修改
i = sqldesigner.executenoquery("update dtrole set rname='" + textbox2.text + "',flag='" + textbox3.text + "'where rid='" + textbox1.text + "'");
e.一些细节
这里,我们修改一下添加数据,让添加的数据变成字符串的形式,也就是加密操作:
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 + "')");
(3)datagridview控件:
//绑定数据源 datagridview1.datasource = dt; //自动适应列宽 datagridview1.columns[1].autosizemode = datagridviewautosizecolumnmode.allcells;
3.代码仅供参考:
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!"); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: C#给Excel添加水印实例详解
下一篇: 详解MongoDB for C#基础入门