C # Programming inserts the numeric value into the VFP database

Source: Internet
Author: User

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();

//----------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.