一个专门实现访问sql server数据库增删改查的操作代码,分享给大家,具体内容如下
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
|
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
using system.data;
using system.data.sqlclient;
namespace windowsformsapplication1
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
//查询
private void button1_click( object sender, eventargs e)
{
string myconn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;trusted_connection=no" ; //定义数据库连接参数
sqlconnection myconnection = new sqlconnection(myconn); //定义一个数据连接实例
sqlcommand mycommand = new sqlcommand( "select * from 图书借阅" , myconnection); //定义一个数据库操作指令
sqldataadapter selectadapter = new sqldataadapter(); //定义一个数据适配器
selectadapter.selectcommand = mycommand; //定义数据适配器的操作指令
dataset mydataset = new dataset(); //定义一个数据集
myconnection.open(); //打开数据库连接
selectadapter.selectcommand.executenonquery(); //执行数据库查询指令
myconnection.close(); //关闭数据库
selectadapter.fill(mydataset); //填充数据集
datagrid1.datasource = mydataset.tables[0];
//datagrid1.databind();//将数据表格用数据集中的数据填充
}
//添加
private void button2_click( object sender, eventargs e)
{
string myconn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;trusted_connection=no" ;
sqlconnection myconnection = new sqlconnection(myconn);
string myinsert = "insert into 图书借阅 (图书编号,读者编号,续借次数) values ('" + convert.tostring(textbox2.text) + "','" +
convert.tostring(textbox3.text)+ "','" +convert.toint32(textbox4.text)+ "')" ;
sqlcommand mycommand = new sqlcommand(myinsert, myconnection);
try //异常处理
{
myconnection.open();
mycommand.executenonquery();
myconnection.close();
}
catch (exception ex)
{
messagebox.show(ex.message);
}
}
//更新
private void button3_click( object sender, eventargs e)
{
string myconn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;trusted_connection=no" ;
sqlconnection myconnection = new sqlconnection(myconn);
string myupdate = "update 图书借阅 set 操作员='" + textbox2.text + "'" + " where 借阅编号=" + "'" + textbox1.text + "'" ;
sqlcommand mycommand = new sqlcommand(myupdate, myconnection);
try
{
myconnection.open();
mycommand.executenonquery();
myconnection.close();
textbox1.text = "" ;
}
catch (exception ex)
{
messagebox.show(ex.message);
}
}
//删除
private void button4_click( object sender, eventargs e)
{
string myconn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;trusted_connection=no" ;
sqlconnection myconnection = new sqlconnection(myconn);
string mydelete = "delete from 图书借阅 where 借阅编号=" + textbox1.text;
sqlcommand mycommand = new sqlcommand(mydelete, myconnection);
try
{
myconnection.open();
mycommand.executenonquery();
myconnection.close();
textbox1.text = "" ;
}
catch (exception ex)
{
messagebox.show(ex.message);
}
}
}
}
|
数据库如下;
winform中查询成功;
插入时,因为借阅编号为自增,不能插入值,会自己生成;
更新,外键冲突;插入的图书编号为000999,无此图书,故出错;
插入成功;
更新操作员为"王老师";
删除借阅编号为31的记录;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。