是通过这么一种方式来往表中插入记录的
1
DataTable dt = DataAccess.GetTable("tableName");
2
DataRow dr = dt.NewRow();
3
dr["col1"] = 1;
4
dr["col2"] = "test str";
5
dt.Rows.Add(dr);
6
DataAccess.UpdateDateTable(dt);

2

3

4

5

6

DataAccess.GetTable 和 UpdateDataTable 是这么实现的。
1
public static DataTable GetTable(string tablename)
2
{
3
SqlConnection conn = GetConn(); //取得数据库链接
4
SqlCommand cmd = new SqlCommand("select top 1 * from " + tablename, conn);
5
DataTable dt = new DataTable();
6
conn.Open();
7
SqlDataAdapter da = new SqlDataAdapter();
8
da.SelectCommand = cmd;
9
da.FillSchema(dt, SchemaType.Mapped);
10
dt.TableName = tablename;
11
conn.Close();
12
return dt;
13
}
14
15
public static int UpdateDataTable(DataTable dt)
16
{
17
SqlConnection conn = GetConn();
18
SqlCommand cmd = new SqlCommand("select top 1 * from " + dt.TableName, conn);
19
SqlCommandBuilder scb = new SqlCommandBuilder(da);
20
return da.Update(dt);
21
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

大家看看,有感想吗?