三套路-----增删改
code
1 using system.data.sqlclient;
2
3 sqlconnection conn = new sqlconnection(xxx);
4
5
6 string sql = xxx;
7
8 sqlcommand comm = new sqlcommand(sql, conn);
9
10 conn.open();
11
12 comm.executenonquery();
13
14 conn.close();
三套路-----查(绑定到datagridview
code
1 using system.data.sqlclient;
2
3 sqlconnection conn = new sqlconnection(xxx);
4
5 string sql = xxx;
6
7 sqldataadapter da = new sqldataadapter(sql, conn);
8
9 dataset ds = new dataset();
10
11 da.fill(ds);
12
13 datagridview1.datasource = ds.tables[0];
14
15 //要更新 查询语句要 查询主键列
16
17 sqlcommandbuilder sd = new sqlcommandbuilder(da);
18
19 da.update(ds.tables[0]);
三套路----查(绑定到listview)
代码
using system.data.sqlclient;
sqlconnection conn = new sqlconnection(xxx);
string sql = xx;
sqlcommand comm = new sqlcommand(sql, conn);
conn.open();
sqldatareader read = comm.executereader();
while (read.read())
{
listviewitem lvi = new listviewitem(read[xxx].tostring());
lvi.tag = read[id];
listview1.items.add(lvi);
lvi.subitems.addrange(new string[] { read[xxx].tostring() });
}
read.close();
conn.close();
更多信息请查看IT技术专栏