.NET读写DBF

    C# 读写DBF分为两种模式,一种为OLEDB驱动,需要安装一个文件“VFPOLEDBSetup.msi”; 一种为Odbc模式,这种几乎上不需要安装Odbc驱动

我这边用的是第一种。

         /// <summary>
        /// 读DBF文件到DataTable
        /// </summary>
        /// <param name="dbname">DBF实际绝对路径带文件名</param>
        /// <returns></returns>
        public static DataTable DbfToDataTable(string dbname)
        {
            try
            {
                var conn = new OleDbConnection(string.Format(@"provider=VFPOLEDB.1;Source Type=DBF;data source={0};user id=admin;password=", dbname));
                string table = dbname.Substring(dbname.LastIndexOf("\\") + 1);
                var da = new OleDbDataAdapter(string.Format("select * from {0}", table), conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close(); 
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 写DataTable文件到DBF文件中
        /// </summary>
        /// <param name="dt">DataTable数据表</param>
        /// <param name="filepath">DBF保存的绝对路径</param>
        /// <param name="savefilename">DBF文件名</param>
        /// <returns></returns>
public static void WriteToDbf(DataTable dt, string filePath, string savefilename)
        { 
           // 连接字符串
            string sConn =
                "Provider=Microsoft.Jet.OLEDB.4.0; " +
                "Data Source=" + filePath + "; " +
                "Extended Properties=dBASE IV;";
            OleDbConnection conn = new OleDbConnection(sConn);
            conn.Open();
            dt.TableName=DateTime.Now.ToString("yyyyMMddHHmmssff");
            try
            {
                //如果存在同名文件则先删除
                if (File.Exists(dt.TableName + ".dbf"))
                { 
                    File.Delete(dt.TableName + ".dbf");
                } 
                OleDbCommand cmd;
                 
                //建立新表
                StringBuilder sbCreate = new StringBuilder();
                sbCreate.Append("CREATE TABLE " + dt.TableName + ".dbf (");
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    sbCreate.Append(dt.Columns[i].ColumnName);
                    sbCreate.Append(" char(25)");
                    if (i != dt.Columns.Count - 1)
                    {
                        sbCreate.Append(", ");
                    }
                    else
                    {
                        sbCreate.Append(')');
                    }
                } 
                cmd = new OleDbCommand(sbCreate.ToString(), conn);
                cmd.ExecuteNonQuery();

                //插入各行
                StringBuilder sbInsert = new StringBuilder();
                foreach (DataRow dr in dt.Rows)
                {
                    sbInsert.Clear();
                    sbInsert.Append("INSERT INTO " + dt.TableName + ".dbf (");
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        sbInsert.Append(dt.Columns[i].ColumnName);
                        if (i != dt.Columns.Count - 1)
                        {
                            sbInsert.Append(", ");
                        }
                    }
                    sbInsert.Append(") VALUES (");
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        sbInsert.Append("'" + dr[i].ToString() + "'");
                        if (i != dt.Columns.Count - 1)
                        {
                            sbInsert.Append(", ");
                        }
                    }
                    sbInsert.Append(')'); 
                    cmd = new OleDbCommand(sbInsert.ToString(), conn);
                    cmd.ExecuteNonQuery();
                }

                //替换文件名
                if (File.Exists(filePath + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".DBF"))
                {
                    File.Move(filePath + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".DBF", filePath + "\\" + savefilename + ".DBF");
                    
                }


                MessageBox.Show("抄表文件下载完毕!", "【温馨提示】", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            } 
            conn.Close();
        }

替换文件名的原因是因为保存DBF文件系统会自动生成文件名为yyyymmdd.DBF的DBF文件,改名为了实现自定义文件名。

posted @ 2019-08-01 14:39  我本梁人  阅读(1016)  评论(0编辑  收藏  举报