4

this is my code

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

i have a database in foxpro 9 with one testing table named test, and i tested with normal sql sentences, and everything is going fine except for the pack statement whom delete data physically from the database, i found and example that shows me how to do it but its with other kinda drive (adodb), but even if i can do it with that code i want to know how could this stuff works in oledb.

bigmander
  • 133
  • 3
  • 9

2 Answers2

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(); 
    }
DaveB
  • 9,470
  • 4
  • 39
  • 66
1

I've never noticed VFP to use ; to differentiate BETWEEN statements. VFP uses ; to identify statement continues on next line (exact opposite of C# and other languages).

So, I would change your command text at the end to the following

comandoFoxPro.CommandText = "USE TEST EXCLUSIVE \r\n"
                            "PACK \r\n"
                            "USE \r\n";

sePudoEjecutarTodo &= (comandoFoxPro.ExecuteNonQuery() > 0);  
DRapp
  • 47,638
  • 12
  • 72
  • 142
  • +1 This is a nice solution to sending multiple FoxPro commands through the driver. Has this solution been tested? – DaveB Dec 09 '10 at 16:57
  • @DaveB, I have not tested, but its similar to others I've done in the past, such as SQL-Select into a cursor, then copy type foxplus or XLS export type with no problems. @Bigmander is obviously new with reputation points and might not know about checking off an answer if it helped solve his issue. That might be coming if it so did work for him. – DRapp Dec 09 '10 at 17:20