C#编程向VFP数据库中插入Numeric型的值
最近做一个C#程序,实现将SQLServer中的数据导入到Visual Foxpro6.0的.dbf数据文件中。更新Numeric类型字段的值时出现错误:
System.Data.Odbc.OdbcException:ERROR [22018] [Microsoft][ODBC Visual FoxPro Driver]Data type mismatch.
原程序类似如下:
//------------------------------------------------------------------------
//到.dbf数据库文件的ODBC连接字符串
string strOdbcConn = @" DRIVER=Microsoft Visual FoxProDriver;UID=;Collate=Machine;BackgroundFetch=Yes; Exclusive=No;SourceType=DBF;SourceDB="+ strFilePath +";";
//获取DataTable
string strSQL = "Select * From table1 ;
DataSet dataSet = new DataSet();
OdbcDataAdapter odbcDA = new OdbcDataAdapter(strSQL,strOdbcConn);
odbcDA.Fill(dataSet,"table1");
DataTable table = dataSet.Tables["table1"];
//向DataTable中添加记录
DataRow row = table.NewRow();
row["DateFrom"] = Convert.ToDateTime("2005-09-10");//日期型字段
row["Num"] = Convert.ToDecimal(10);//Numric(16,0)型字段
table.Rows.Add(row);
//更新到数据库中
OdbcCommandBuilder builder = new OdbcCommandBuilder(odbcDA);
odbcDA.InsertCommand = builder.GetInsertCommand();
odbcDA.Update(dataSet,"table1");
//----------------------------------------------------------------
程序运行时,在对row["Num"]赋值时并不出错,执行到oodbcDA.Update(dataSet,"table1");时出错,根源就在于
对row["Num"]的赋值,实在找不到好的解决办法。
后来,用SQL语句测试,如:update table1 set Num=10;执行正确,就想用SQL语句insert解决,经测试可行。
SQL-Insert语句如下:
Insert Into table1(DateFrom, Num) Values({^2005-09-10},10)
程序相应的改成如下的了:
//------------------------------------------------------------------
string strOdbcConn = @" DRIVER=Microsoft Visual FoxProDriver;UID=;Collate=Machine;BackgroundFetch=Yes; Exclusive=No;SourceType=DBF;SourceDB="+ strFilePath +";";
OdbcConnection odbcConn = new OdbcConnection(strOdbcConn);
string sqlInsert = "Insert Into table1(DateFrom, Num) Values({^2005-09-10},10)";
OdbcCommand odbcComm = new OdbcCommand(sqlInsert,odbcConn);
odbcComm.Connection.Open();
odbcComm.ExecuteNonQuery();
odbcConn.Close();
//----------------------------------------------------------------
========================================================
其它关于VFP的信息:
1.VFP-SQL语句
----插入日期值:
insert into 1able1(日期字段) values({^2005-09-10})
----不支持如下语句(insert-select):
insert into table1
select * from table2
2.MS OLE DB Provider for VFP :
微软网站:http://www.microsoft.com/downloads/details.aspx?FamilyID=e1a87d8f-2d58-491f-a0fa-95a3289c5fd4&DisplayLang=en提供了Microsoft OLE DB Provider for Visual FoxPro 9.0,会对编写访问VFP数据的程序有帮助。
3.更多微软Visual FoxPro资源:
http://www.microsoft.com/downloads/info.aspx?na=13&p=1&SrcDisplayLang=en&SrcCategoryId=&SrcFamilyId=&u=%2fdownloads%2fbrowse.aspx%3fdisplaylang%3den%26productID%3dE794F2FC-0425-40AD-A292-39490679FA65
4.记住这个连接字符串:
string strOdbcConn = @" DRIVER=Microsoft Visual FoxProDriver;UID=;Collate=Machine;BackgroundFetch=Yes; Exclusive=No;SourceType=DBF;SourceDB=D:\DataDBF\table1.dbf;";
=======================
夏春涛的blog: http://bluesky521.cnblogs.com/
email: xchuntao@163.com 或 xchuntao@gmail.com
QQ:23106676
msn:xchuntao@163.com