0

Here is an example of using OleDbCommand and ExecuteNonQuery(I took it from a manual on msdn.microsoft.com). I would like to check out the query that was generated before executing it.

private static void OleDbCommandPrepare(string connectionString)
{
    using (OleDbConnection connection = new
               OleDbConnection(connectionString))
    {
        connection.Open();

        // Create the Command.
        OleDbCommand command = new OleDbCommand();

        // Set the Connection, CommandText and Parameters.
        command.Connection = connection;
        command.CommandText =
            "INSERT INTO dbo.Region (RegionID, RegionDescription) VALUES (?, ?)";
        command.Parameters.Add("RegionID", OleDbType.Integer, 4);
        command.Parameters.Add("RegionDescription", OleDbType.VarWChar, 50);
        command.Parameters[0].Value = 20;
        command.Parameters[1].Value = "First Region";

        // Call  Prepare and ExecuteNonQuery.
        command.Prepare();

find out the query that is being executed here

        command.ExecuteNonQuery();

        // Change parameter values and call ExecuteNonQuery.
        command.Parameters[0].Value = 21;
        command.Parameters[1].Value = "SecondRegion";
        command.ExecuteNonQuery();
    }
}

Is there any possible way to do it? I'm using PgSqlCommand - which is PostgreSQL equivalent of OleDbCommand and getting undescribed exception. I was able to build that query and find what was the error by cycling Command.Parameters and replacing question symbols in Command.CommandText with paremeter's values, but I am hoping that there is a built-in method to get that query.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
plavozont
  • 807
  • 9
  • 17
  • Related : http://stackoverflow.com/questions/178857/oledb-ado-net-get-the-commands-text-with-all-parameters-replaced –  Jun 30 '16 at 04:46
  • A way to fix a query: http://stackoverflow.com/questions/37670358/get-oledbcommandbuilder-s-generated-sql-commands/38118349#38118349 – plavozont Jul 01 '16 at 02:03

1 Answers1

1

There is no direct way to check Generated query , but alternative is you can make use of string format instead of command.

var query= String.Format(
            "INSERT INTO dbo.Region (RegionID, RegionDescription)
              VALUES ({0}, {1})", 20,"First Region" );
command.CommandText = query;## Heading ##

Other way to check query is make use of Profiler i.e. in case of sql server SQL Profiler shows which query is fired on database.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263