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;";
}