Search code examples
c#sqlsql-server-cewindows-ce.net-1.0

How can I see the SQL sent to the database after the parameters have replaced their placeholders?


The first MessageBox.Show() below simply shows me the exact same thing as const string SQL_GET_VENDOR_ITEMS, which seems fine to me, but I'm getting, "There was an error parsing the query. [Token line number, Token line offset,, Token in error,,]"

Is there a way to spy on the contents of the SQL after parameters have been added; it should then be something like: "SELECT ItemID, PackSize FROM VendorItems WHERE VendorID = 'TEST' AND VendorItemID = '852963'

Here's the pertinent code:

    const string SQL_GET_VENDOR_ITEMS = "SELECT ItemID, PackSize " + 
        "FROM VendorItems " +
         "WHERE VendorID = @VendorID AND VendorItemID = @VendorItemID";

    string retVal = string.Empty;
    checkConnection();
    SqlCeCommand vendorCMD = objCon.CreateCommand();
    try 
    {
        vendorCMD.CommandText = SQL_GET_VENDOR_ITEMS;
        vendorCMD.Parameters.Add("@VendorID", SqlDbType.NVarChar, 10).Value = VendorID; 
        vendorCMD.Parameters.Add("@VendorItemID", SqlDbType.NVarChar, 19).Value = VendorItemID;

        MessageBox.Show(string.Format("Made it up to vendorCMD.ExecuteReader() with sql {0}", vendorCMD.CommandText));

. . .

        vendorReader.Close();
    } 
    catch (SqlCeException sqlceex)
    {
        MessageBox.Show(string.Format("SqlCeException in GetValsForVendorAndItem == {0}", sqlceex.Message));//TODO: Remove
    }
    finally 
    {
        vendorCMD.Dispose();
    }
    return retVal;

. . .


Solution

  • but I can almost guarantee that won't work in my VS2003/.NET 1.0 world

    ahhh... version - see MSDN:

    The .NET Compact Framework data provider for SQL Server CE does not support named parameters for passing parameters to an SQL statement called by a SqlCeCommand when CommandType is set to Text. You must use the question mark (?) placeholder. For example: SELECT * FROM Customers WHERE CustomerID = ?