如何在VB中显示SQL存储过程?

发布于 2024-10-31 19:28:35 字数 2257 浏览 1 评论 0原文

我正在使用 SSMS 2008 和 VB。我是一名 VB 开发新手。我试图在我的 ASPX 页面上显示一个简单存储过程的结果。但我收到以下错误。这是我在 ASPX 页面后面的代码:

>           MsgBox(GlobalFunctions.GlobalF.GetDevSQLServerStoredProcedure())

以及来自 GlobalF 命名空间的代码:

Public Shared Function GetDevSQLServerStoredProcedure()
    Dim conn As SQLConnection
    Dim DSPageData As New System.Data.DataSet

    conn = New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString"))
    Dim cmd As New SQLCommand

    cmd.CommandType = CommandType.StoredProcedure
    cmd.Connection = conn
    cmd.CommandText = "AllTableCount"

    Dim da = New SQLDataAdapter(cmd)
    da.Fill(DSPageData)
    Return DSPageData
End Function

以及我的错误:

参数“提示”无法转换 输入“字符串”

[ArgumentException:参数“提示” 无法转换为“String”类型。] Microsoft.VisualBasic.Interaction.MsgBox(对象 提示、MsgBoxStyle 按钮、对象 标题)+320
ASP.website_complaints_complainttrendinglist3_aspx.Main() 在 e:\inetpub\amdmetrics-d.web.abbott.com\wwwroot\Website\Complaints\ComplaintTrendingList3.aspx:113 ASP.website_complaints_complainttrendinglist3_aspx.Page_Load(对象 发件人、EventArgs E) 中 e:\inetpub\amdmetrics-d.web.abbott.com\wwwroot\Website\Complaints\ComplaintTrendingList3.aspx:60 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp、对象 o、对象 t、EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象 发送者,EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔值 includeStagesAfterAsyncPoint) +627

此存储过程仅返回我的数据库中的记录数。但我想执行其他返回表的存储过程。我如何实现这一点而不会出现错误?

最后我的新 SQL SP:

CREATE proc [dbo].PrintTestMsg AS BEGIN 
PRINT 'HELLO'
PRINT 'WORLD'
PRINT 'PAGE3'
PRINT 'PAGE4'
END

SELECT * FROM SELECT (EXEC PrintTestMsg)

使用上述选择在 SSMS 中出现语法错误。

下面的代码怎么样?我现在可以获得数据集吗?

private string DatasetToString(DataSet ds)
{ 

StringBuilder sb = new StringBuilder();
DataTable dt = ds.Tables(0);

foreach (DataRow dr in dt.Rows) {
sb.Append(“;”);
sb.Append(dt.Rows(1).ToString());
}

return sb.ToString();

}

I am using SSMS 2008 and VB. I'm a novice VB developer. I am trying to display results of a simple stored proc on my ASPX page. But I get the error below. Here is my code behind for the ASPX page:

>           MsgBox(GlobalFunctions.GlobalF.GetDevSQLServerStoredProcedure())

And my code from GlobalF namespace:

Public Shared Function GetDevSQLServerStoredProcedure()
    Dim conn As SQLConnection
    Dim DSPageData As New System.Data.DataSet

    conn = New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString"))
    Dim cmd As New SQLCommand

    cmd.CommandType = CommandType.StoredProcedure
    cmd.Connection = conn
    cmd.CommandText = "AllTableCount"

    Dim da = New SQLDataAdapter(cmd)
    da.Fill(DSPageData)
    Return DSPageData
End Function

And my error:

Argument 'Prompt' cannot be converted
to type 'String'

[ArgumentException: Argument 'Prompt'
cannot be converted to type 'String'.]
Microsoft.VisualBasic.Interaction.MsgBox(Object
Prompt, MsgBoxStyle Buttons, Object
Title) +320
ASP.website_complaints_complainttrendinglist3_aspx.Main()
in
e:\inetpub\amdmetrics-d.web.abbott.com\wwwroot\Website\Complaints\ComplaintTrendingList3.aspx:113
ASP.website_complaints_complainttrendinglist3_aspx.Page_Load(Object
Sender, EventArgs E) in
e:\inetpub\amdmetrics-d.web.abbott.com\wwwroot\Website\Complaints\ComplaintTrendingList3.aspx:60
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr
fp, Object o, Object t, EventArgs e)
+14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object
sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs
e) +99
System.Web.UI.Control.LoadRecursive()
+50 System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) +627

This stored procedure just returns number of records in my database. But I want to perform other stored procedures which return a table. How do I implement this without errors?

Finally my new SQL SP:

CREATE proc [dbo].PrintTestMsg AS BEGIN 
PRINT 'HELLO'
PRINT 'WORLD'
PRINT 'PAGE3'
PRINT 'PAGE4'
END

SELECT * FROM SELECT (EXEC PrintTestMsg)

I get syntax error in SSMS with above select.

What about the code below? Could I get a dataset now?

private string DatasetToString(DataSet ds)
{ 

StringBuilder sb = new StringBuilder();
DataTable dt = ds.Tables(0);

foreach (DataRow dr in dt.Rows) {
sb.Append(“;”);
sb.Append(dt.Rows(1).ToString());
}

return sb.ToString();

}

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

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

发布评论

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

评论(1

心是晴朗的。 2024-11-07 19:28:35

不用担心将数据集转换为字符串。这里真正的问题是您根本无法使用 asp.net 中的正常 MsbBox() 函数。真的。如果您尝试,它会在 Web 服务器上隐藏的私人桌面上显示该消息。它不会在用户的机器上弹出该消息。不仅如此,它还会阻塞,直到单击为止,并且由于它位于隐藏桌面上,因此永远不会发生这种情况。结果是您的用户的页面永远不会加载。

您想要做的是将 GetDevSQLServerStoredProcedure() 方法的结果绑定到 GridView、DataGrid 或 Repeater 控件。您的代码中还存在其他问题,因此我将对每个部分进行快速清理,以向您展示它应该是什么样子:

''#Always list the type returned at the end of the method name
Public Shared Function GetDevSQLServerStoredProcedure() As DataSet
    Dim DSPageData As New System.Data.DataSet

    ''# A Using block will ensure the .Dispose() method is called for these variables, even if an exception is thrown
    ''# This is IMPORTANT - not disposing your connections properly can result in an unrespsonsive database
    Using conn As New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString")), _
          cmd As New SQLCommand("PrintTestMsg", conn), _
          da As New SQLDataAdapter(cmd)

        cmd.CommandType = CommandType.StoredProcedure

        da.Fill(DSPageData)
    End Using

    Return DSPageData
End Function

现在,您的存储过程。这不会返回任何内容,因此我添加了代码来将打印的数据返回到您的页面:

CREATE proc [dbo].PrintTestMsg AS BEGIN 
    PRINT 'HELLO'
    PRINT 'WORLD'
    PRINT 'PAGE3'
    PRINT 'PAGE4'

    SELECT 'HELLO' AS Msg
    UNION
    SELECT 'WORLD'
    UNION
    SELECT 'PAGE3'
    UNION 
    SELECT 'PAGE4'
END

您需要将其添加到您的 aspx 标记中:

<asp:GridView ID="PrintMessageGrid" runat="server" AutoGenerateColumns="True" />

最后,将您的 MsgBox() 函数调用替换为这段代码:

PrintMessageGrid.DataSource = GetDevSQLServerStoredProcedure() 
PrintMessageGrid.DataBind()

请注意,这足以让事情在基本意义上工作,以便您可以继续探索 ASP.Net。我刚刚发布的一些技术我永远不会在生产网站中使用。示例包括 GridView 中的 AutoGenerateColumns 甚至数据集(datareader 几乎总是更好的选择)。这只是对原始代码的最小更改,因此您可以开始将所有内容组合在一起,并随着更多练习而学习更好的技术。

Don't worry about converting the Dataset to a string. Your real problem here is that you can't use the normal MsbBox() function in asp.net at all. Really. If you try, it will display the message on a hidden private desktop on the web server. It will not pop up the message on the user's machine. Not only that, but it will block until clicked on, and since it's on a hidden desktop this can never happen. The result is that your user's page will never load.

What you want to do instead is bind the result of GetDevSQLServerStoredProcedure() method to a GridView, DataGrid, or Repeater control. There are other problems in your code as well, so I'll do a quick clean-up for each part to show you what it should look like:

''#Always list the type returned at the end of the method name
Public Shared Function GetDevSQLServerStoredProcedure() As DataSet
    Dim DSPageData As New System.Data.DataSet

    ''# A Using block will ensure the .Dispose() method is called for these variables, even if an exception is thrown
    ''# This is IMPORTANT - not disposing your connections properly can result in an unrespsonsive database
    Using conn As New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString")), _
          cmd As New SQLCommand("PrintTestMsg", conn), _
          da As New SQLDataAdapter(cmd)

        cmd.CommandType = CommandType.StoredProcedure

        da.Fill(DSPageData)
    End Using

    Return DSPageData
End Function

Now, your stored procedure. This doesn't return anything, so I added code to also return the printed data to your page:

CREATE proc [dbo].PrintTestMsg AS BEGIN 
    PRINT 'HELLO'
    PRINT 'WORLD'
    PRINT 'PAGE3'
    PRINT 'PAGE4'

    SELECT 'HELLO' AS Msg
    UNION
    SELECT 'WORLD'
    UNION
    SELECT 'PAGE3'
    UNION 
    SELECT 'PAGE4'
END

You need to add this to your aspx markup:

<asp:GridView ID="PrintMessageGrid" runat="server" AutoGenerateColumns="True" />

And, finally, replace your MsgBox() function call with this code:

PrintMessageGrid.DataSource = GetDevSQLServerStoredProcedure() 
PrintMessageGrid.DataBind()

Note that this is just enough to get things working in a basic sense, so that you can continue to explore ASP.Net. Some of the techniques I just posted I would never use in a production web site. Examples include AutoGenerateColumns in the GridView and even datasets (datareader is almost always a better option). This is just the bare minimum change to your original code so you can start piecing things together and learn better techniques as you get more practice.

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