【问题标题】:How to display a SQL stored procedure in VB?如何在VB中显示SQL存储过程?
【发布时间】:2011-04-12 14:44:46
【问题描述】:

我正在使用 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: 参数“提示” 无法转换为“字符串”类型。] 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();

}

【问题讨论】:

  • 您正在返回一个数据集。您需要对该数据集进行一些解析以提取您希望 MsgBox 显示的字符串
  • 哦,我明白你在说什么了。让我试试!
  • Prescott,我明白为什么我需要为 MsgBox 返回一个字符串。但是,我试图从 SSMS 中的存储过程中仅返回第一行,但出现语法错误。如何修改此查询以仅返回第一行?见上面的编辑
  • SELECT TOP 1 * FROM SELECT (EXEC PrintTestMsg)
  • 另外,你可能想稍微改变你的代码,'foreach(DataRow dr in Dt.Rows) { sb.Append(";"); sb.Append(dr(0).ToString()); {'

标签: asp.net sql vb.net stored-procedures namespaces


【解决方案1】:

不用担心将数据集转换为字符串。您真正的问题是您根本无法在 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 几乎总是更好的选择)。这只是对原始代码的最低限度的更改,因此您可以开始拼凑起来,并在进行更多练习时学习更好的技术。

【讨论】:

  • 谢谢一百万,乔尔!但是,当我尝试这样做时,我得到了新错误:未声明名称“GetDevSQLServerStoredProcedure”。你知道这应该声明为什么类型吗?
  • 这里的类型很好 - 问题在于你把它放在哪里。确保它在您的页面类中。否则,您还必须使用它的类名来调用它。
  • 我该如何感谢你?!它现在返回结果!我非常兴奋!接下来我要弄清楚如何执行一个有参数的存储过程。
  • 我很好奇……上面代码行末尾的“_”是什么?我需要那个吗?
  • @salvationishere 这是 VB 行继续符。您可以删除它,但是您必须将这些其他变量放在同一行。我觉得这样更易读。
猜你喜欢
  • 2019-07-15
  • 1970-01-01
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
  • 2020-12-28
相关资源
最近更新 更多