是通过这么一种方式来往表中插入记录的
1DataTable dt = DataAccess.GetTable("tableName");
2DataRow dr = dt.NewRow();
3dr["col1"] = 1;
4dr["col2"] = "test str";
5dt.Rows.Add(dr);
6DataAccess.UpdateDateTable(dt);
2DataRow dr = dt.NewRow();
3dr["col1"] = 1;
4dr["col2"] = "test str";
5dt.Rows.Add(dr);
6DataAccess.UpdateDateTable(dt);
DataAccess.GetTable 和 UpdateDataTable 是这么实现的。
1public 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
15public 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 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
15public 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}
大家看看,有感想吗?