If you add missing quotes it would work for 'some cases' coincidentally (precisely when string length is not over 255).
Create OleDbParameter objects for your insert in the order they appear and assign your string to the corresponding parameter.
This sample was previously posted on UniversalThread ( I'm a C#er but it'd be easy for you to understand): using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
class Test
{
static void Main()
{
string strCon = @"Provider=VFPOLEDB;
Data source=C:\Program Files\Microsoft Visual Studio\MSDN\2001OCT\1033\SAMPLES\VFP98\data\Testdata.dbc";
string strSQL = "insert into employee (emp_id,first_name,last_name,notes) values ( , , , )";
OleDbConnection cn = new OleDbConnection(strCon) ;
cn.Open();
// First send "set null off" command to VFP so it wouldn't error for missing fields
// but instead fill them with empty values if default is not specified
OleDbCommand cmdInit = new OleDbCommand("set null off",cn);
cmdInit.ExecuteNonQuery();
// Prepare command and parameters
OleDbCommand cmd = new OleDbCommand(strSQL,cn);
OleDbParameter empID = cmd.Parameters.Add("empID",OleDbType.Char);
OleDbParameter firstName = cmd.Parameters.Add("firstName",OleDbType.Char);
OleDbParameter lastName = cmd.Parameters.Add("lastName",OleDbType.Char);
OleDbParameter notes = cmd.Parameters.Add("notes",OleDbType.Char);
// might be VarChar, LongVarChar etc
// Important
// Note that unlike MSSQL, VFP parameters are not named but positional.
// Parameter name only helps to developer. It's their position that provides mapping
// parameter to its related field. IOW:
// (emp_id,first_name,last_name,notes) values ( , , , )
// first parameter added (empID) is for emp_id, 2nd (firstName) is for first_Name and so on.
empID.Value = "C#2VFP";
firstName.Value = "TestName";
lastName.Value = "TestLast";
// Get an existing text file to put into VFP memo field
string file2read = @"C:\Program Files\Microsoft Visual Studio\Vfp98\VFP6FAQ.txt";
string memoContent;
using (StreamReader sr = new StreamReader(file2read))
{
memoContent = sr.ReadToEnd();
}
// insert the record
cmd.ExecuteNonQuery();
cn.Close();
}
}
|