C# DataSet DataTable To/转换为 DBF 格式

        public string ExportDataIntoDBF(DataSet xmlDataSet, string path, string fileName, DataSet dataSet)
        {

            ArrayList list = new ArrayList();//用集合保存需导出的字段

            if (File.Exists(path + fileName))
            {

                File.Delete(path + fileName);

            }

            string createSql = "create table " + fileName + " (";

            //for (int i = 0; i < xmlDataSet.Tables[0].Rows.Count; i++)//根据需导出的字段新创建一个表
            //{

            //    string fieldName = xmlDataSet.Tables[0].Rows[i]["fieldName"].ToString();

            //    string type = xmlDataSet.Tables[0].Rows[i]["type"].ToString();

            //    createSql = createSql + fieldName + " " + type + ",";

            //    list.Add(fieldName);

            //}

            foreach (DataColumn dc in dataSet.Tables[0].Columns)
            {
                string fieldName = dc.ColumnName;

                string type = dc.DataType.ToString();

                switch (type)
                {
                    case "System.String":
                        type = "varchar(50)";
                        break;
                    case "System.Boolean":
                        type = "varchar(10)";
                        break;
                    case "System.Int32":
                        type = "int";
                        break;
                }

                createSql = createSql + fieldName + " " + type + ",";

                list.Add(fieldName);
            }

            createSql = createSql.Substring(0, createSql.Length - 1) + ")";


            OleDbConnection con = new OleDbConnection(GetConnection(path));

            OleDbCommand cmd = new OleDbCommand();

            try
            {

                cmd.Connection = con;

                con.Open();

                cmd.CommandText = createSql;

                cmd.ExecuteNonQuery();//创建新表


                foreach (DataRow row in dataSet.Tables[0].Rows)//将数据导出到DBF文件中
                {

                    string insertSql = "insert into " + fileName + " values(";

                    for (int i = 0; i < list.Count; i++)
                    {

                        insertSql = insertSql + "'" + row[list[i].ToString()].ToString() + "',";

                    }

                    insertSql = insertSql.Substring(0, insertSql.Length - 1) + ")";

                    cmd.CommandText = insertSql;

                    cmd.ExecuteNonQuery();

                }

                return "导出成功!";

            }

            catch (Exception ex)
            {

                return ex.Message;

            }

            finally
            {

                con.Close();

            }

        }


        private string GetConnection(string path)
        {

            return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=dBASE IV;";

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值