A C # program was recently implemented to import data from SQL Server into the. DBF data file in Visual Foxpro6.0. An error occurred while updating the value of the Numeric Type field:
System.Data.Odbc.OdbcException:ERROR [22018] [microsoft][odbc Visual FoxPro driver]data type mismatch.
The original program is similar to the following:
//------------------------------------------------------------------------
//到.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");
//----------------------------------------------------------------
When the program runs, it does not make an error when assigning value to row["Num", and executes to Oodbcda.update (DataSet, "Table1");
There is no good solution to the assignment of row["Num".
Later, with the SQL statement test, such as: Update table1 set num=10, execute correctly, want to use SQL statement Insert solution, tested feasible.
The Sql-insert statement is as follows:
Insert into table1 (datefrom, Num) Values ({^2005-09-10},10)
The program is changed to the following:
//------------------------------------------------------------------
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();
//----------------------------------------------------------------