vb 错误:“字符串”类型的值无法转换为“System.Data.SqlClient.SqlCommand”

发布于 2024-11-01 03:23:06 字数 1028 浏览 2 评论 0原文

我是一名新手 VB 程序员,我确信这个解决方案很简单,但是,当我尝试在 SQL Server 中显示不需要任何参数的存储过程之一的结果时,我收到上述错误。我该如何解决这个问题?

我的代码摘录:

Dim SQLCmd as SQLCommand = new SQLCommand()
SQLCmd.CommandText = "Exec AllTableCount" 
SQLCmd.CommandType = CommandType.StoredProcedure 
SQLCmd.Connection = GlobalFunctions.GlobalF.GetDevSQLServerStoredProcedure(SQLCmd.CommandType)             
SQLCmd.ExecuteNonQuery()
MsgBox(SQLCmd.ExecuteNonQuery)

其中 GetDevSQLServerStoredProcedure 在不同的文件中定义为:

Public Shared Function GetDevSQLServerStoredProcedure(ByVal SQL As String)
   Dim DBConn As SQLConnection
   Dim DBCommand As SQLDataAdapter
   Dim DSPageData As New System.Data.DataSet
   DBConn = New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString"))
   DBCommand = New SQLDataAdapter(SQL) 'This is the line it errors on'
   DBCommand.Fill(DSPageData, "Exceptions")
   Return DSPageData
End Function

我可以在服务器资源管理器中看到来自 VS 2008 的此 SP。所以问题似乎是我不知道如何将数据适配器连接到 SP。只是一个字符串查询。

I'm a novice VB programmer and I'm sure this solution is trivial, however, I am getting the above error when I try to display the results of one of my Stored Procs in SQL Server which doesn't need any parameters. How can I fix this?

My code excerpt:

Dim SQLCmd as SQLCommand = new SQLCommand()
SQLCmd.CommandText = "Exec AllTableCount" 
SQLCmd.CommandType = CommandType.StoredProcedure 
SQLCmd.Connection = GlobalFunctions.GlobalF.GetDevSQLServerStoredProcedure(SQLCmd.CommandType)             
SQLCmd.ExecuteNonQuery()
MsgBox(SQLCmd.ExecuteNonQuery)

Where GetDevSQLServerStoredProcedure is defined in a different file as:

Public Shared Function GetDevSQLServerStoredProcedure(ByVal SQL As String)
   Dim DBConn As SQLConnection
   Dim DBCommand As SQLDataAdapter
   Dim DSPageData As New System.Data.DataSet
   DBConn = New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString"))
   DBCommand = New SQLDataAdapter(SQL) 'This is the line it errors on'
   DBCommand.Fill(DSPageData, "Exceptions")
   Return DSPageData
End Function

I can see this SP from VS 2008 in the Server Explorer. So the problem seems to be that I don't know how to connect a data adapter to an SP. Just a string query.

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

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

发布评论

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

评论(1

静若繁花 2024-11-08 03:23:06

正如蒂姆提到的,命令文本应该只是存储过程的名称。

看来您现在遇到的问题是您将 CommandType 传递给方法 GetDevSQLServerStoredProcedure 而不是字符串。

Command text should just be the name of the stored procedure as Tim mentioned.

It looks like the problem you are having now is that you are passinging the CommandType to your method GetDevSQLServerStoredProcedure instead of a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文