C# 导出Visual FoxPro (dbf文件) 问题
最近一个项目中由于客户需求需要将查询信息导出.dbf文件并且严格要求了字段类型、宽度、小数点位数等。
现在功能倒是已经写好了。
但由于我是通过sql语句进行的一系列操作。
其中在创建表指定数字型字段宽度和小数点位数的时候总是不生效。
我明明指定的是宽度17,小数位2
但是打开导出的.dbf文件 查看表结构居然是宽度20 小数位5
我晕倒。不知道究竟是什么问题。
现贴出关键代码,求大神帮解脱。
#region Creat file
/// <summary>
/// 创建数据文件
/// </summary>
/// <param name="dt">需要创建的数据</param>
/// <param name="errMsg"></param>
/// <returns></returns>
public int CreatFile(DataTable dt, out string errCode, out string errMsg,string filePath)
{
errCode = null; errMsg = null; int rtn = 0;
try
{
//需创建的文件名称
string fileName = dt.TableName + ".dbf";
//判断文件是否存在
DirectoryInfo dir = new DirectoryInfo(filePath);
if (dir != null)
{
if (dir.GetFiles(fileName).Length > 0)
{
//已经存在则删除原有数据
rtn = this.ExcCommandText("drop table " + dt.TableName, out errCode, out errMsg);
if (rtn != 0)
{
return rtn;
}
}
}
//构建创建文件数据结构语句
string sqlCreat = "create table " + dt.TableName + "({0})";
string columns = "";
foreach (DataColumn col in dt.Columns)
{
columns += col.ColumnName;
if (col.DataType == System.Type.GetType("System.String"))
{
columns += " varchar(" + col.MaxLength + "),";
}
else
{
columns += " numeric(17,2),";
}
}
columns = columns.Remove(columns.Length - 1, 1);
sqlCreat = string.Format(sqlCreat, columns);
rtn = ExcCommandText(sqlCreat, out errCode, out errMsg);
if (rtn != 0)
{
return rtn;
}
//导入数据
foreach (DataRow row in dt.Rows)
{
string sqlInsert = "insert into " + dt.TableName + " values({0})";
string invalues = "";
foreach (DataColumn col in dt.Columns)
{
invalues += "'" + row[col].ToString() + "',";
}
invalues = invalues.Remove(invalues.Length - 1, 1);
sqlInsert = string.Format(sqlInsert, invalues);
rtn = ExcCommandText(sqlInsert, out errCode, out errMsg);
if (rtn != 0)
{
return rtn;
}
}
return rtn;
}
catch (System.Exception e)
{
errMsg = e.Message + "|" + e.StackTrace;
return -1;
}
}
#endregion C# SQL --------------------编程问答-------------------- 两个星期了 没人理我T.T
补充:.NET技术 , C#