插入参数后检索SqlCommand/OleDbCommand 查询?

发布于 2024-08-14 10:08:12 字数 1062 浏览 9 评论 0原文

有什么方法可以访问发送到 SQL Server 的准备好的语句吗?

            Dim params(1) As OleDb.OleDbParameter
            params(0) = New OleDb.OleDbParameter("@ID", OleDb.OleDbType.VarChar, 50)
            params(0).Value = guiItemsGridView.Rows(e.RowIndex).Cells("ID").Value
            params(1) = New OleDb.OleDbParameter("@SavedValue", OleDb.OleDbType.VarChar, 50)
            params(1).Value = guiItemsGridView.Rows(e.RowIndex).Cells("Saved Value").Value

            Dim QueryString As String = "UPDATE Items SET [Saved Value]=@SavedValue WHERE ID=@ID"
            myDatabase.objAdapter.UpdateCommand = New OleDb.OleDbCommand(QueryString, DatabaseConnection)

            myDatabase.objAdapter.UpdateCommand.Parameters.AddRange(params)
            myDatabase.objAdapter.UpdateCommand.Prepare()

            Debug.Print(myDatabase.objAdapter.UpdateCommand.CommandText)

我正在寻找一种在添加参数后访问准备好的查询的方法。准备命令后,即使参数已传递,CommandText 仍包含基本查询 UPDATE Items SET [Saved Value]=@SavedValue WHERE ID=@ID

我发现编写由这段代码执行的查询日志对于调试该应用程序的其他区域很有用。

Is there any way to access the prepared statement as sent to the SQL server?

            Dim params(1) As OleDb.OleDbParameter
            params(0) = New OleDb.OleDbParameter("@ID", OleDb.OleDbType.VarChar, 50)
            params(0).Value = guiItemsGridView.Rows(e.RowIndex).Cells("ID").Value
            params(1) = New OleDb.OleDbParameter("@SavedValue", OleDb.OleDbType.VarChar, 50)
            params(1).Value = guiItemsGridView.Rows(e.RowIndex).Cells("Saved Value").Value

            Dim QueryString As String = "UPDATE Items SET [Saved Value]=@SavedValue WHERE ID=@ID"
            myDatabase.objAdapter.UpdateCommand = New OleDb.OleDbCommand(QueryString, DatabaseConnection)

            myDatabase.objAdapter.UpdateCommand.Parameters.AddRange(params)
            myDatabase.objAdapter.UpdateCommand.Prepare()

            Debug.Print(myDatabase.objAdapter.UpdateCommand.CommandText)

I'm looking for a way to access the prepared query after the parameters have been added. After preparing the command, CommandText still contains the base query UPDATE Items SET [Saved Value]=@SavedValue WHERE ID=@ID even though the parameters have been passed.

I would find it useful to write a log of queries preformed by this chunk of code for debugging other areas of this application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妖妓 2024-08-21 10:08:12

我从未找到使用参数功能记录查询的方法。我最终恢复为在字符串中创建查询,然后使用所述字符串:

QueryString = String.Format("UPDATE Items SET [Saved Value]={0} WHERE ID={1}", SavedValue, ID)

然后,我将该字符串用于查询和日志记录功能。

I never found a way to log the query using the parameters functionality. I ended up reverting to creating the query in a string, then consuming said string:

QueryString = String.Format("UPDATE Items SET [Saved Value]={0} WHERE ID={1}", SavedValue, ID)

I then consumed that string for both the query and logging functions.

萌能量女王 2024-08-21 10:08:12

我需要类似的东西。我已经构建了一个可以做到这一点的方法,它并不完美,但它确实有帮助:

VB .NET:

Public Sub LogSQL(ByVal cmd As OleDb.OleDbCommand)
    Dim pars As String = ""
    If cmd.Parameters.Count > 0 Then
        pars = " ["
        Dim a As Integer
        For a = 0 To cmd.Parameters.Count - 1
            pars = pars + cmd.Parameters(a).Value.ToString + ","
        Next
        pars = Left(pars, Len(pars) - 1) + "]"
    End If
    Log("SQL=" + cmd.CommandText + pars, LogNivel.INFO)
End Sub

这给出了类似的内容:

SQL=SELECT f1,f2 FROM table WHERE fx = ? AND fy = ? [20,35]

I needed something like that. I've built a method that can do the trick, it's not perfect, but it does help:

VB .NET:

Public Sub LogSQL(ByVal cmd As OleDb.OleDbCommand)
    Dim pars As String = ""
    If cmd.Parameters.Count > 0 Then
        pars = " ["
        Dim a As Integer
        For a = 0 To cmd.Parameters.Count - 1
            pars = pars + cmd.Parameters(a).Value.ToString + ","
        Next
        pars = Left(pars, Len(pars) - 1) + "]"
    End If
    Log("SQL=" + cmd.CommandText + pars, LogNivel.INFO)
End Sub

This gives something like:

SQL=SELECT f1,f2 FROM table WHERE fx = ? AND fy = ? [20,35]
何必那么矫情 2024-08-21 10:08:12

我尝试搜索了一段时间,但找不到使用该库来获取返回的已插入参数的 sql 语句。我能想到的最好的办法是 sql server 中的 SQL Server Profiler。如果您将其打开并选中 TSQL 菜单下的选项 SQL:StmtCompleted 和存储过程菜单下的 SP:StmtCompleted,您将看到如下所示的事件文本数据列:

SQL:StmtCompleted

EXEC    @return_value = [dbo].[ap_test]
        @User_ID = N'testuser'

SP:Completed

SELECT *
FROM Table1
WHERE User_ID = @User_ID

由于您正在寻找一种方法来记录查询及其各自的输入参数,这可能很有用 - 但是,如果 UpdateCommand.CommandText 捕获插入的参数肯定会更好。我将继续看看是否有解决方案,因为我自己也多次想要这个功能。

I tried to search around for a while and couldn't find to use that library to get a sql statement returned that had the parameters inserted. The best I was able to come up with was the SQL Server Profiler in sql server. If you turn it on and check the option SQL:StmtCompleted under the TSQL menu and SP:StmtCompleted under the Stored Procedures menu you will see event textdata columns like this:

SQL:StmtCompleted

EXEC    @return_value = [dbo].[ap_test]
        @User_ID = N'testuser'

SP:Completed

SELECT *
FROM Table1
WHERE User_ID = @User_ID

Since you were looking for a way to log your queries and their respective input parameters this might be useful- however, it would definitely be better if UpdateCommand.CommandText captured the inserted parameters. I will continue to see if a there is a solution out there as i have wanted this functionality several times myself.

旧瑾黎汐 2024-08-21 10:08:12
string commandString = UpdateCommand.CommandText;
foreach (OleDbParameter parameter in UpdateCommand.Parameters)
    commandString = commandString.Replace("@" + parameter.ParameterName.ToString(), parameter.Value.ToString());
string commandString = UpdateCommand.CommandText;
foreach (OleDbParameter parameter in UpdateCommand.Parameters)
    commandString = commandString.Replace("@" + parameter.ParameterName.ToString(), parameter.Value.ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文