DBF文件初步了解(二)——DBF数据导出代码实现

    上篇博客中主要记录一些关于DBF数据文件的概念性知识。包括DBF的数据结构和用处。在这里记录一下在C#中导出DBF文件的实现方式。
    DBF文件也是一种数据库文件,我们导出DBF文件也就是将数据库中的数据导出到DBF文件中。所以最主要的就是讲DataTable转换成DBF,包括数据和数据类型。
  
<span style="font-size:24px;">private bool WriteDBFfile(string filepath, DataTable dt, List<string> columnNames, string tableName, Dictionary<string, char> nametype = null)
        {
            #region ***************************************写t**************************************
            nametype = ConvertColumnType(dt, tableName); ;
            FileStream fs = null;
            BinaryWriter bw = null;
            try
            {
                int rowCount = dt.Rows.Count;
                int columnCount = dt.Columns.Count;
                //if (rowCount > 0 && columnCount > 0)
                //{
                fs = new FileStream(filepath, FileMode.Create, FileAccess.Write);
                bw = new BinaryWriter(fs);
                byte tempByte;
                byte[] tempBytes;
                int tempInt;
                int[] fieldLength = new int[columnCount];
                int tempMax = 0;
                for (int i = 0; i < columnCount; i++)
                {
                    tempMax = 0;
                    for (int j = 0; j < rowCount; j++)
                    {
                        if (dt.Rows[j][i].ToString() != null && dt.Rows[j][i].ToString() != "")
                        {
                            if (dt.Rows[j][i].ToString().Length > tempMax)
                            {
                                tempMax = dt.Rows[j][i].ToString().Length;
                            }
                        }
                    }
                    char dbfType = 'C';
                    if (nametype.ContainsKey(columnNames[i].ToLower()))
                    {
                        dbfType = nametype[columnNames[i].ToLower()];
                    }
                    if (tableName.StartsWith("F") && dbfType == 'N')
                    {
                        tempMax = 8;
                    }
                    else if (dbfType == 'N')
                    {
                        tempMax = 1;
                    }
                    //数êy字×?类àà型Dí长3¤度è至á少éù13,£?此′?处′|直±接ó设éè置?所ù有óD列áD最×?小D?长3¤度è为a13
                    fieldLength[i] = tempMax * 2 < 13 ? 13 : tempMax * 2;
                }
                /
                tempByte = 0x03;
                bw.Write(tempByte);
                tempBytes = new byte[3];

                tempInt = DateTime.Now.Year - 1900;
                tempBytes[0] = System.Convert.ToByte(tempInt);

                tempBytes[1] = System.Convert.ToByte(DateTime.Now.Month);

                tempBytes[2] = (byte)(DateTime.Now.Day);
                bw.Write(tempBytes);

                bw.Write(rowCount);

                tempInt = 33 + columnCount * 32;
                bw.Write((Int16)tempInt);

                tempInt = 1;
                for (int i = 0; i < columnCount; i++)
                {
                    tempInt += fieldLength[i];
                }
                bw.Write((Int16)tempInt);

                tempBytes = new byte[20];
                bw.Write(tempBytes);
                string tempColumnName;

                for (int i = 0; i < columnCount; i++)
                {
                    tempColumnName = columnNames[i];
                    tempBytes = ConvertStringToBytes(tempColumnName, 11);
                    bw.Write(tempBytes);
            
                    char dbfType = 'C';
                    if (nametype.ContainsKey(tempColumnName.ToLower()))
                    {
                        dbfType = nametype[tempColumnName.ToLower()];
                    }
                    tempByte = (byte)dbfType;//数êy据Y类àà型Dí为a字×?符·?串′?
                    bw.Write(tempByte);
                    //第μú12-15为a保±£留á?字×?节ú
                    tempBytes = new byte[4];
                    bw.Write(tempBytes);
                    //第μú16个?字×?节ú为a字×?段?长3¤度è
                    tempMax = fieldLength[i];
                    tempByte = (byte)tempMax;
                    bw.Write(tempByte);
                    //接ó着×?第μú17-31都?为a保±£留á?字×?节ú
                    tempBytes = new byte[15];
                    if (tableName.StartsWith("F") && dbfType == 'N' && tempColumnName.StartsWith("z"))
                    {
                        tempBytes[0] = (byte)2;/
                    }
                    bw.Write(tempBytes);
                }

                tempByte = 0x0D;
                bw.Write(tempByte);
                object tempValue;
                for (int i = 0; i < rowCount; i++)
                {
                    //每?一ò?行DD第μú一ò?个?字×?节ú默?认è?为a20
                    tempByte = 0x20;
                    bw.Write(tempByte);
                    for (int j = 0; j < columnCount; j++)
                    {
                        tempValue = dt.Rows[i][j];
                        if (tempValue != null)
                        {
                            double val;
                            if (double.TryParse(tempValue.ToString(), out val) && Equals(val, DbfsjdcViewModel_2015.FXB_DEF))
                            {
                                tempValue = "";
                            }
                            tempBytes = ConvertStringToBytes(tempValue.ToString(), fieldLength[j], true);
                            bw.Write(tempBytes);
                        }
                        else
                        {
                            tempBytes = ConvertStringToBytes("", fieldLength[j], true);
                            bw.Write(tempBytes);
                        }
                    }
                }
            }
            catch (Exception except)
            {
                MessageBox.Show(except.Message);
            }
            finally
            {
                if (bw != null)
                {
                    bw.Close();
                    bw.Dispose();
                }
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
                nametype.Clear();
            }
            return System.IO.File.Exists(filepath);
            #endregion  ***************************************写D′入è?DBF文?件t**************************************
        }
      
     类型转换将String类型转换为Bytes类型方法:
        private byte[] ConvertStringToBytes(string tempStr, int limitLength, bool needAppendWhiteSpace = false)
        {
            try
            {
                 //originaldqdm,sourcelsgxmc,sourcelsgxdm,
                //org_dqdm,src_lsgxmc,src_lsgxdm
                switch (tempStr)
                {
                    case "originaldqdm":
                        tempStr = "org_dqdm";
                        break;
                    case "sourcelsgxmc":
                        tempStr = "src_lsgxmc";
                        break;
                    case "sourcelsgxdm":
                        tempStr = "src_lsgxdm";
                        break;
                }
                byte[] tempBytes = UTF8Encoding.GetEncoding("gb2312").GetBytes(tempStr);
                byte[] result = null;
                if (tempBytes.Length == limitLength)
                {
                    result = tempBytes;
                }
                else if (tempBytes.Length > limitLength)
                {
                    result = new byte[limitLength];
                    for (int i = 0; i < limitLength; i++)
                    {
                        result[i] = tempBytes[i];
                    }
                }
                else if (tempBytes.Length < limitLength)
                {
                    result = new byte[limitLength];
                    for (int i = 0; i < tempBytes.Length; i++)
                    {
                        result[i] = tempBytes[i];
                    }
                    if (needAppendWhiteSpace)
                    {
                        for (int i = tempBytes.Length; i < limitLength; i++)
                        {
                            result[i] = (byte)32;
                        }
                    }
                }
                return result;
            }
            catch (Exception except)
            {
                MessageBox.Show(except.Message);
                return null;
            }
        }
        提取DataTable 列名,并转化为 List
        private Dictionary<string, char> ConvertColumnType(DataTable dt, string tableName)
        {
            
            Dictionary<string, char> lstType = new Dictionary<string, char>();
            foreach (DataColumn dc in dt.Columns)
            {
                string colName = dc.ColumnName;  
                string colType = dc.DataType.ToString();    
                char dbfType = 'C';
                switch (colType)
                {
                    case "System.Int32":
                    case "System.Int64":
                    case "System.Double":
                    case "System.Decimal":
                    case "System.UInt16":
                    case "System.UInt32":
                    case "System.UInt64":
                    case "System.Byte":
                    case "System.SByte":
                        dbfType = 'N';
                        break;
                    case "System.DateTime":
                        dbfType = 'D';
                        break;
                    case "System.Int16":
                    case "System.String":
                        dbfType = 'C';
                        break;
                    default:
                        dbfType = 'C';
                        break;
                }
           
                if (!lstType.ContainsKey(colName.ToLower()))
                {
                    lstType.Add(colName.ToLower(), dbfType);
                }
            }
            return lstType;
        }</span>

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
c# DBF数据库导入导出实例 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.Odbc; using System.Data.SqlClient; namespace DbfExample { public partial class Form1 : Form { System.Data.Odbc.OdbcConnection conn; public Form1() { InitializeComponent(); } //导出数据 private void btnOut_Click(object sender, EventArgs e) { string connect = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=c:\\; "; OdbcConnection myconn = new OdbcConnection(connect); string sqlt ="CREATE TABLE aa.DBF (cc int(10))"; myconn.Open(); OdbcCommand olec = new OdbcCommand(sqlt, myconn); try { int i = olec.ExecuteNonQuery(); MessageBox.Show("'" + i + "'success"); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { olec.Dispose(); myconn.Close(); } //string ole_connstring = @"Provider=microsoft.jet.oledb.5.0;Data Source=D:\;"; //System.Data.OleDb.OleDbConnection ole_conn = new System.Data.OleDb.OleDbConnection(ole_connstring); //try //{ // ole_conn.Open(); // System.Data.OleDb.OleDbCommand cmd1 = new System.Data.OleDb.OleDbCommand // ("Create Table TestTable (Field1 int, Field2 char(10),Field float(10,2))", // ole_conn); // System.Data.OleDb.OleDbCommand cmd2 = new System.Data.OleDb.OleDbCommand // ("Insert Into TestTable values (1,'Hello3',520.20)", ole_conn); // System.Data.OleDb.OleDbCommand cmd3 = new System.Data.OleDb.OleDbCommand // ("Insert Into TestTable values (2,'Hello4',18076.60)", ole_conn); // cmd1.ExecuteNonQuery(); // cmd2.ExecuteNonQuery(); // cmd3.ExecuteNonQuery(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} //finally //{ // ole_conn.Close(); //} } //导入数据 private void btnIn_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { Bind(); } private void Bind() { try { conn = new System.Data.Odbc.OdbcConnection(); string table = @"C:\测试例子\Dbf\prepur.dbf"; string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); string sql = @"select * from " + table; OdbcDataAdapter da = new OdbcDataAdapter(sql, conn); DataTable dt = new DataTable(); da.Fill(dt); this.dataGridView1.DataSource = dt.DefaultView; //MessageBox.Show(dt.Rows[0][0].ToString()); } catch { } finally { conn.Close(); } } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { try { conn = new System.Data.Odbc.OdbcConnection(); string table = @"C:\测试例子\Dbf\table1.dbf"; string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); string sql = @"select * from " + table + " where id='" + id + "'"; OdbcDataAdapter da = new OdbcDataAdapter(sql, conn); DataTable dt = new DataTable(); da.Fill(dt); txtId.Text = id; txtName.Text = dt.Rows[0]["name"].ToString(); txtAddress.Text = dt.Rows[0]["address"].ToString(); } catch { } finally { conn.Close(); } } private void Add() { conn = new System.Data.Odbc.OdbcConnection(); string table = @"C:\temp\Dbf\table1.dbf"; string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); OdbcCommand cmd = new OdbcCommand(); cmd.Connection = conn; string sql = "insert into " + table + " values('" + txtId.Text + "','" + txtName.Text + "','" + txtAddress.Text + "')"; cmd.CommandText = sql; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); Bind(); } private void btnTOSQL_Click(object sender, EventArgs e) { try { string sql = "Insert Into dbftosql select * From openrowset('MSDASQL','Driver=Microsoft Visual FoxPro Driver;SourceType=DBF;SourceDB=C:\\temp\\Dbf','select * from table1.dbf')"; SqlConnection con = new SqlConnection("server=.;database=labelprint;uid=sa;pwd=sa"); con.Open(); SqlCommand cmd = new SqlCommand(sql, con); cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值