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

99封情书 提交于 2019-12-12 16:18:20

问题


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;

. . .


回答1:


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 = ?




回答2:


Since you are on CE, your options are limited, but there are some suggestions for how to peek into the database: Profiler for Sql CE

If you were on normal SQL Server, you might consider using SQL Profiler. You'd be able to see what is getting executed against the database.



来源:https://stackoverflow.com/questions/15798207/how-can-i-see-the-sql-sent-to-the-database-after-the-parameters-have-replaced-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!