2012-01-04 36 views
0

嘿,这一切都是我第一次通过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个colums价值的数据,我想这些数据添加到列表视图。我不确定我是否有正确的联系,但这是我在以前的sql代码中运行的查询(而不是存储过程)。

此外,我不知道如何去获取数据的文本框上面的@xxx名称?我如何将这些值传递给用户文本框中的@xxx名称?

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

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

传递的可变因素中的两个可以为NULL。只有一个需要填写以便它返回数据。

任何帮助将是伟大的!

大卫

回答

3

你是非常接近。

夫妇的注意事项:

  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 
+0

Awe一些CT!它很适合你的小小调整!谢谢! :O) – StealthRT 2012-01-04 02:34:57

相关问题