C# 将DataTable存储到DBF文件中
作者:网络转载 发布时间:[ 2016/4/21 11:39:44 ] 推荐标签:测试开发技术 .NET
(准备)生成一个DataTable
1 /// <summary>
2 /// 生成一个数据表
3 /// </summary>
4 /// <returns></returns>
5 static DataTable GetTable()
6 {
7 try
8 {
9 DataTable dt = new DataTable("Characters");
10
11 dt.Columns.Add("Column0");
12 dt.Columns.Add("Column1");
13 dt.Columns.Add("Column2");
14 dt.Columns.Add("Column3");
15
16 dt.Rows.Add("abc", "def", "ghi", "jkl");
17 dt.Rows.Add("def", "ghi", "jkl", "mno");
18 dt.Rows.Add("ghi", "jkl", "mno", "pqr");
19 dt.Rows.Add("jkl", "mno", "pqr", "stu");
20
21 Console.WriteLine("Set DataTable: " + dt.TableName);
22 foreach (DataRow dr in dt.Rows)
23 {
24 foreach (object obj in dr.ItemArray)
25 {
26 Console.Write(obj.ToString() + " ");
27 }
28 Console.WriteLine();
29 }
30 Console.WriteLine("Mission complete!");
31
32 return dt;
33 }
34 catch
35 {
36 return null;
37 }
38 }
函数:将DataTable的内容输出到DBF文件
1 /// <summary>
2 /// 将数据表写入到DBF文件中
3 /// </summary>
4 /// <param name="dt"></param>
5 static void WriteToDbf(DataTable dt)
6 {
7 Console.WriteLine("Writing to: " + dt.TableName + ".dbf ...");
8
9 //连接字符串
10 string sConn =
11 "Provider=Microsoft.Jet.OLEDB.4.0; " +
12 "Data Source=" + System.IO.Directory.GetCurrentDirectory() + "; " +
13 "Extended Properties=dBASE IV;";
14 OleDbConnection conn = new OleDbConnection(sConn);
15 conn.Open();
16
17 try
18 {
19 //如果存在同名文件则先删除
20 if (File.Exists(dt.TableName + ".dbf"))
21 {
22 Console.WriteLine("Delete file: " + dt.TableName + ".dbf ...");
23 File.Delete(dt.TableName + ".dbf");
24 }
25
26 OleDbCommand cmd;
27
28 //建立新表
29 StringBuilder sbCreate = new StringBuilder();
30 sbCreate.Append("CREATE TABLE " + dt.TableName + ".dbf (");
31 for (int i = 0; i < dt.Columns.Count; i++)
32 {
33 sbCreate.Append(dt.Columns[i].ColumnName);
34 sbCreate.Append(" char(25)");
35 if (i != dt.Columns.Count - 1)
36 {
37 sbCreate.Append(", ");
38 }
39 else
40 {
41 sbCreate.Append(')');
42 }
43 }
44
45 Console.WriteLine("
Creating Table ...");
46 Console.WriteLine(sbCreate.ToString());
47 cmd = new OleDbCommand(sbCreate.ToString(), conn);
48 cmd.ExecuteNonQuery();
49
50 //插入各行
51 StringBuilder sbInsert = new StringBuilder();
52 foreach (DataRow dr in dt.Rows)
53 {
54 sbInsert.Clear();
55 sbInsert.Append("INSERT INTO " + dt.TableName + ".dbf (");
56 for (int i = 0; i < dt.Columns.Count; i++)
57 {
58 sbInsert.Append(dt.Columns[i].ColumnName);
59 if (i != dt.Columns.Count - 1)
60 {
61 sbInsert.Append(", ");
62 }
63 }
64 sbInsert.Append(") VALUES (");
65 for (int i = 0; i < dt.Columns.Count; i++)
66 {
67 sbInsert.Append("'" + dr[i].ToString() + "'");
68 if (i != dt.Columns.Count - 1)
69 {
70 sbInsert.Append(", ");
71 }
72 }
73 sbInsert.Append(')');
74
75 Console.WriteLine("
Inserting lines ...");
76 Console.WriteLine(sbInsert.ToString());
77 cmd = new OleDbCommand(sbInsert.ToString(), conn);
78 cmd.ExecuteNonQuery();
79 }
80 }
81 catch (Exception ex)
82 {
83 Console.WriteLine(ex.Message);
84 }
85
86 conn.Close();
87 }
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11