c# - 在参数替换了它们的占位符后,如何查看发送到数据库的 SQL?

标签 c# sql sql-server-ce windows-ce .net-1.0

下面的第一个 MessageBox.Show() 只是向我展示了与 const string SQL_GET_VENDOR_ITEMS 完全相同的东西,这对我来说似乎很好,但我得到的是,“解析查询时出错。[ token 行number, Token line offset,, Token in error,,]"

有没有办法在添加参数后监视 SQL 的内容?它应该是这样的:“SELECT ItemID, PackSize FROM VendorItems WHERE VendorID = 'TEST' AND VendorItemID = '852963'

相关代码如下:

    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;

. . .

最佳答案

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

啊...版本 - 请参阅 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 = ?

关于c# - 在参数替换了它们的占位符后,如何查看发送到数据库的 SQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15798207/

相关文章:

c# - 使用正则表达式在字符串中查找以某个索引开头的索引

sql - 将多列转换为一列

sql - 为多个用户使用 SDF 数据库文件

c# - 如何使用 C# 确定列是否存在于 SQL Server CE 表中?

c# - 在 Windows Phone 上使用 Linq to SQL 时是否可以提高批量删除的性能?

sql-server-ce - Windows 窗体应用程序中的连接字符串 EF 4.1 code first SQL compact

c# - WPF ViewModel 和绑定(bind)

c# - 如何借助 Linq 找出整数列表中特定模式的存在。 C#

c# - 将 int 转换为 0-1 float

mysql - 表的 2 列和相同查询的联合快捷方式?