这是我的代码
//Probando insercion
OleDbConnection conexionFoxPro = new OleDbConnection();
string rutaFoxPro = @"C:\Users\BigMander\Documents\Proyectos de Visual FoxPro\prueba.dbc";
conexionFoxPro.ConnectionString = String.Format("Provider=VFPOLEDB.1;Data Source={0};Exclusive=Yes;", rutaFoxPro);
bool sePudoEjecutarTodo = true;
try
{
conexionFoxPro.Open();
OleDbCommand comandoFoxPro = new OleDbCommand();
comandoFoxPro.CommandText =
@"INSERT INTO test ([nombre], [telefono], [id]) VALUES (?, ?, ?)";
comandoFoxPro.Parameters.Add("nombre", OleDbType.Char).Value = "bigmander";
comandoFoxPro.Parameters.Add("telefono", OleDbType.Char).Value = "some number";
comandoFoxPro.Parameters.Add("id", OleDbType.Integer).Value = 5;
comandoFoxPro.Connection = conexionFoxPro;
sePudoEjecutarTodo &= (comandoFoxPro.ExecuteNonQuery() > 0);
comandoFoxPro.CommandText =
@"SELECT nombre, telefono FROM test";
OleDbDataReader reader = comandoFoxPro.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("{0}: {1}", reader.GetName(0), reader["nombre"]);
Console.WriteLine("{0}: {1}", reader.GetName(1), reader["telefono"]);
}
reader.Close();
reader.Dispose();
comandoFoxPro.CommandText =
"DELETE FROM test WHERE id = 5";
sePudoEjecutarTodo &= (comandoFoxPro.ExecuteNonQuery() > 0);
comandoFoxPro.CommandText =
"SET EXCLUSIVE ON; PACK test";
sePudoEjecutarTodo &= (comandoFoxPro.ExecuteNonQuery() > 0);
}
catch(OleDbException oleDbE)
{
sePudoEjecutarTodo = false;
Console.WriteLine(oleDbE.Message);
}
finally
{
if (sePudoEjecutarTodo)
Console.WriteLine("Congratulaciones si se armo todo");
else
Console.WriteLine("Pelas");
conexionFoxPro.Close();
Console.ReadKey();
}
我在foxpro 9中有一个数据库,其中一个名为test的测试表,我用普通的sql语句进行测试,一切都很顺利,除了从数据库中物理删除数据的包语句,我发现和示例显示我如何做到这一点,但它与其他类型的驱动器(adodb),但即使我可以使用该代码,我想知道这些东西如何在oledb中工作。
答案 0 :(得分:7)
static void Main(string[] args)
{
Console.WriteLine("Starting program execution...");
string connectionString = @"Provider=VFPOLEDB.1;Data Source=h:\dave\";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand scriptCommand = connection.CreateCommand())
{
connection.Open();
string vfpScript = @"SET EXCLUSIVE ON
DELETE FROM test WHERE id = 5
PACK";
scriptCommand.CommandType = CommandType.StoredProcedure;
scriptCommand.CommandText = "ExecScript";
scriptCommand.Parameters.Add("myScript", OleDbType.Char).Value = vfpScript;
scriptCommand.ExecuteNonQuery();
}
}
Console.WriteLine("End program execution...");
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
答案 1 :(得分:1)
我从来没有注意到VFP的使用;区分BETWEEN语句。 VFP使用;识别语句在下一行继续(与C#和其他语言完全相反)。
所以,我会在最后将命令文本更改为以下
comandoFoxPro.CommandText = "USE TEST EXCLUSIVE \r\n"
"PACK \r\n"
"USE \r\n";
sePudoEjecutarTodo &= (comandoFoxPro.ExecuteNonQuery() > 0);