C#(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中添加記錄
http://www.mscto.com
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 http://www.mscto.com
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=/downloads/browse.ASPx?displaylang=en&productID=E794F2FC-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;\";