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