调用存储过程并返回结果

时间:2012-01-04 02:12:03

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

嘿所有这是我第一次通过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个列的数据,我想将该数据添加到列表视图中。我不确定我是否有正确的sytax这样做,但这是我在以前的sql代码中使用的(运行查询,而不是存储过程)。

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

MS SQL mangement studio代码用于存储过程:

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

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

任何帮助都会很棒!

大卫

1 个答案:

答案 0 :(得分:3)

你非常接近。

几个笔记:

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

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

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

  4. 另一种方法是在添加参数的.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
    
相关问题