【问题标题】:Getting data from a stored procedure从存储过程中获取数据
【发布时间】:2018-05-21 09:30:52
【问题描述】:

我有一个这样的存储过程:

Select name, surname from student

我无法使用 VB.Net 获取数据。 我的代码是:

Dim reader As SqlDataReader
With dbCmd
    .CommandType = CommandType.StoredProcedure
    .CommandText = "sp_myPersonalSP"
End With
reader = dbCmd.ExecuteReader()

但 Visual Studio 在尝试“reader = dbCmd.ExecuteReader()”时向我发送异常:

过程 sp_myPersonalSP 没有提供参数和参数。

谢谢!我是 VB.Net 的新手 :-(

【问题讨论】:

  • 过程 sp_myPersonalSP_Ruoli 没有参数并且提供了参数
  • 您遇到了哪个错误?
  • 嗨@Emdad,我的错误是:“过程 sp_myPersonalSP 没有提供参数和参数。”我的存储过程是这样的:“创建 myPersonalSP AS 选择姓名,学生的姓氏”在我的 sp 中没有其他文本。
  • 错误消息表明您已将参数添加到命令中,但我在您的 VB.NET 代码 sn-p 中没有看到任何参数。另外'您不应使用 sp_ 前缀来命名存储过程,因为它用于表示系统过程。
  • 您之前是否在代码中使用了相同的dbCmd 实例并为其添加了参数?如果是这样,您可以在较早的实例上调用.Dispose(),然后实例化一个新实例,或者使用dbCmd.Parameters.Clear()。

标签: sql-server vb.net


【解决方案1】:

从执行存储过程的 Sql Server 返回数据表的函数:

Public Function GetApplicationType() As DataTable
    Dim MyDataTable As DataTable = New DataTable()
    ' The connection string information is in the web.config file - see below
    Dim con = ConfigurationManager.ConnectionStrings("MyConnectionString").ToString()
    Dim MyDataAdapter As SqlDataAdapter = New SqlDataAdapter("GetSomeData", con)
    MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
    ' add the parameters in the same order and type as what the stored procedure expects, they must match the names in  the stored procedure and are case sensitive.

    MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@ParameterName", SqlDbType.VarChar, 10));
    MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@Parametername2", SqlDbType.VarChar, 40));

    MyDataAdapter.SelectCommand.Parameters["@ParameterName"].Value = somedata1;
    MyDataAdapter.SelectCommand.Parameters["@ParameterName2"].Value = somedata2;

    MyDataAdapter.Fill(MyDataTable)
    Return MyDataTable
End Function

web.config

  <connectionStrings>
    <add name="MyConnectionString" connectionString="server=192.168.11.11;database=Test;uid=someusername; pwd=somepassword" providerName="System.Data.SqlClient" />
  </connectionStrings>

【讨论】:

  • 我认为没有必要按照与S​​QL Sever相同的顺序添加参数。在 Access 中是必需的。
【解决方案2】:

您可以在 DataGridView 中显示查询结果。您需要有一个连接才能执行命令。在执行命令之前打开连接。 Using...End Using 语句确保您的对象在出现错误时关闭并释放事件。

Private Sub GetData()
            Using cn As New SqlConnection("Your Connection String")
                Using dbCmd As New SqlCommand
                    With dbCmd
                        .CommandType = CommandType.StoredProcedure
                        .CommandText = "sp_myPersonalSP"
                        .Connection = cn
                    End With
                    cn.Open()
                    Using reader As SqlDataReader = dbCmd.ExecuteReader()
                        'You can view the result of your query in a DataGridView
                        Dim dt As New DataTable
                        dt.Load(reader)
                        DataGridView1.DataSource = dt
                    End Using
                End Using
            End Using
    End Sub

【讨论】:

    【解决方案3】:

    要从存储过程中检索数据,只需像这样调用您的存储过程名称。

    Dim stringquery = "CALL YOURSTOREDPROCNAME()"

    试试我的代码:

            Dim dt as new Datatable
            con.Open()
            Dim query = "Call StoredProcedureName()"
            command = New SqlCommand(query, con)
            adapter.SelectCommand = command
            dt.Clear()
            adapter.Fill(dt)
            con.Close()
    

    -凯文

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 2016-10-21
      • 2016-08-14
      • 2017-01-01
      • 1970-01-01
      • 2013-08-01
      • 2016-08-04
      相关资源
      最近更新 更多