【问题标题】:Calling stored procedure and getting the results back调用存储过程并返回结果
【发布时间】:2012-01-04 02:12:03
【问题描述】:

嘿,这是我第一次通过 vb.net 调用存储过程,我想在执行之前确保一切正确。

这是我的代码:

Dim connectionString As String = GetConnectionString()
Dim intCount As Integer = 0

Using connection As New SqlConnection(connectionString)
   Dim command As SqlCommand = connection.CreateCommand()

   Try
      connection.Open()
      command.CommandType = CommandType.StoredProcedure
      command.CommandText = "Complete_S_H"
      command.Parameters.Add("@J_ID", SqlDbType.Int)
      command.Parameters.Add("@O_Nbr", SqlDbType.VarChar)
      command.Parameters.Add("@R_Nbr", SqlDbType.VarChar)
      command.Parameters("@theOutput").Direction = ParameterDirection.Output

      Dim dataReader As SqlDataReader = command.ExecuteReader()
      Do While dataReader.Read()
         ListView1.Items.Add(Trim(dataReader(0)))
         ListView1.Items(CInt(intCount)).SubItems.Add(Trim(dataReader(1)))
         ListView1.Items(CInt(intCount)).SubItems.Add(Trim(dataReader(2)))
         ListView1.Items(CInt(intCount)).SubItems.Add(Trim(dataReader(3)))
         ListView1.Items(CInt(intCount)).SubItems.Add(Trim(dataReader(4)))
         ListView1.Items(CInt(intCount)).SubItems.Add(Trim(dataReader(5)))
         intCount = intCount + 1
      Loop

      dataReader.Close()
      connection.Close()
   Catch ex As Exception
      Console.WriteLine(ex.Message)
   End Try
End Using

存储过程返回 6 列的数据,我想将该数据添加到列表视图中。我不确定我是否有正确的语法来执行此操作,但这是我在以前的 sql 代码中使用的(运行查询,而不是存储过程)。

另外,我不确定如何从文本框中获取上述@xxx 名称的数据?如何将值从用户文本框中传递给 @xxx 名称?

存储过程的MS SQL管理工作室代码是这样的:

 EXEC [dbo].[Complete_S_H]
@J_ID = 208660,
@O_Nbr = NULL,
@R_Nbr = NULL

两个传递的变量可以为 NULL。只需填写一项即可返回数据。

任何帮助都会很棒!

大卫

【问题讨论】:

    标签: sql-server vb.net sql-server-2008 stored-procedures


    【解决方案1】:

    你已经很接近了。

    几个笔记:

    1. 您可以获得对已添加列表项的本地引用,从而加快并清理代码

    2. 除非您知道 DB 值永远不会为空,否则您应该始终在使用它们之前对其进行 DbNull 测试。

    3. 要使用文本框中的值,您可以使用Parameters.AddWithValue。我已经修改了代码以显示如何。

    另一种方法是在添加参数后设置 .Value 属性:

    command.Parameters.Add("@J_ID", SqlDbType.Int).Value = CInt(TextBox1.Text)
    

    command.Parameters("@J_ID").Value = CInt(TextBox1.Text)
    

    以下是对这些想法的重写和用于设置子项的奖励循环(非必需):

        Dim connectionString As String = GetConnectionString()
    
        Using connection As New SqlConnection(connectionString)
            Dim command As SqlCommand = connection.CreateCommand()
    
            Try
                connection.Open()
                command.CommandType = CommandType.StoredProcedure
                command.CommandText = "Complete_S_H"
                command.Parameters.AddWithValue("@J_ID", CInt(TextBox1.Text))
                command.Parameters.AddWithValue("@O_Nbr", TextBox2.Text)
                command.Parameters.AddWithValue("@R_Nbr", TextBox3.Text)
                command.Parameters("@theOutput").Direction = ParameterDirection.Output
                'command.ExecuteNonQuery()
    
                Dim dataReader As SqlDataReader = command.ExecuteReader()
                Do While dataReader.Read()
                    Dim oItem As ListViewItem
    
                    oItem = ListView1.Items.Add(Trim(dataReader(0)))
    
                    For nI As Integer = 1 To dataReader.FieldCount - 1
                        If Not dataReader.IsDBNull(nI) Then
                            oItem.SubItems.Add(Trim(dataReader(nI)))
                        Else
                            oItem.SubItems.Add(String.Empty)
                        End If
                    Next
                Loop
    
                dataReader.Close()
                connection.Close()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using
    

    【讨论】:

    • 真棒CT!你的小调整效果很好!谢谢! :o)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2015-06-03
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多