Ejecutar una sentencia Select

2041 vistas

Usaremos los objetos Command y DataReader.

Vemos un ejemplo con SQL Server



vbnet
  1. Trace.Listeners.Add(New TextWriterTraceListener(Console.Out))
  2. Dim connectString As String = "database=test;server=SRV01;User ID=samfisher;pwd=echelon"
  3. Dim connection As SqlConnection = New SqlConnection(connectString)
  4. connection.Open()
  5. Dim command As SqlCommand = New SqlCommand("SELECT * FROM usr_contract", connection)
  6. Dim reader As SqlDataReader = command.ExecuteReader
  7. Dim row As Object() = Nothing
  8. While reader.Read
  9.     If row Is Nothing Then
  10.         row = New Object(reader.FieldCount) {}
  11.     End If
  12.     reader.GetValues(row)
  13.     Dim i As Integer = 0
  14.     While i < row.GetLength(0)
  15.         If Not row(i) Is Nothing AndAlso Not (row(i) Is DBNull.Value) Then
  16.             Trace.Write(row(i).ToString())
  17.         Else
  18.             Trace.Write("NULL")
  19.         End If
  20.         If i < row.GetUpperBound(0) Then
  21.             Trace.Write(" | ")
  22.         End If
  23.         i = i + 1
  24.     End While
  25.     Trace.Write(vbCrLf)
  26. End While
  27. reader.Close()
  28. connection.Close()