从存储过程中获取数据

时间:2018-05-21 09:30:52

标签: sql-server vb.net

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

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的新手: - (

3 个答案:

答案 0 :(得分:0)

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

ggplot(aes_string(colnames(df)[x], colnames(df)[y]))

的web.config

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

答案 1 :(得分:0)

您可以在DataGridView中显示查询结果。您需要为要执行的命令建立连接。执行命令之前打开连接。使用...结束使用语句确保关闭对象并在出现错误时处置事件。

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

答案 2 :(得分:0)

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

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()

-KEVIN