插入参数后检索SqlCommand/OleDbCommand 查询?
有什么方法可以访问发送到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我从未找到使用参数功能记录查询的方法。我最终恢复为在字符串中创建查询,然后使用所述字符串:
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.
我需要类似的东西。我已经构建了一个可以做到这一点的方法,它并不完美,但它确实有帮助:
VB .NET:
这给出了类似的内容:
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:
This gives something like:
我尝试搜索了一段时间,但找不到使用该库来获取返回的已插入参数的 sql 语句。我能想到的最好的办法是 sql server 中的 SQL Server Profiler。如果您将其打开并选中 TSQL 菜单下的选项
SQL:StmtCompleted
和存储过程菜单下的SP:StmtCompleted
,您将看到如下所示的事件文本数据列:SQL:StmtCompleted
SP:Completed
由于您正在寻找一种方法来记录查询及其各自的输入参数,这可能很有用 - 但是,如果
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 andSP:StmtCompleted
under the Stored Procedures menu you will see event textdata columns like this:SQL:StmtCompleted
SP:Completed
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.