当前位置:首页 > 通信资讯 > 正文

winform窗体增删改查(c#增删改查怎么实现)

winform中利用ado.net实现对单表的增删改查的详细例子,具体如下:

1.前言:

运行环境:vs2013+sql2008+windows10

程序界面预览:
winform窗体增删改查(c#增删改查怎么实现)

使用的主要控件:datagridview和menustrip等。

2.功能具体介绍:

1.首先,我们要先实现基本的数据操作,增删改查这几个操作。

(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)运用建立的公共类,进行数据库的操作:

a.数据查询:

?
1 2 3 ds = sqldesigner.executedataset("select * from dtuser"); dt = ds.tables[0]; datagridview1.datasource = dt;

b.数据添加

?
1 i = sqldesigner.executenoquery("insert into dtuser(uid,uname,pwd,uflag)values('" + textbox1.text + "','" + textbox2.text + "','" +textbox3.text+ "','" + textbox4.text + "')");

c.数据删除

?
1 2 string currentindex = datagridview1.currentrow.cells[0].value.tostring(); i = sqldesigner.executenoquery("delete from dtuser where uid='" + currentindex + "'");

d.数据修改

?
1 i = sqldesigner.executenoquery("update dtrole set rname='" + textbox2.text + "',flag='" + textbox3.text + "'where rid='" + textbox1.text + "'");

e.一些细节

这里,我们修改一下添加数据,让添加的数据变成字符串的形式,也就是加密操作:

?
1 2 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控件:

?
1 2 3 4 //绑定数据源 datagridview1.datasource = dt; //自动适应列宽 datagridview1.columns[1].autosizemode = datagridviewautosizecolumnmode.allcells;

3.代码仅供参考:

?
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 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!"); } } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/zhiboday/p/6107057.html

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。