VB.Net by API/System.Data.SqlClient/SqlDataReader — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:50, 26 мая 2010
Содержание
SqlDataReader.GetOrdinal
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class MainClass
Shared Dim WithEvents con As SqlConnection
Shared Sub Main()
con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
Dim cmd As New SqlCommand()
cmd.rumandText = "SELECT * FROM Employee"
cmd.Connection = con
Try
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim OrderIDIdx As Integer = reader.GetOrdinal("ID")
"Dim OrderDateIdx As Integer = reader.GetOrdinal("FirstName")
"Dim OrderFreightIdx As Integer = reader.GetOrdinal("LastName")
Do
While reader.Read()
Dim OrderID As Integer = reader.GetInt32(OrderIDIdx)
"Dim OrderDate As DateTime = reader.GetDateTime(OrderDateIdx)
"Dim OrderFreight As Decimal = reader.GetDecimal(OrderFreightIdx)
"Console.WriteLine(OrderID & vbTab & OrderDate & vbTab & OrderFreight)
Console.WriteLine(OrderID & vbTab )
End While
Loop While reader.NextResult()
reader.Close()
Finally
con.Close()
End Try
End Sub
End Class
SqlDataReader.GetSchemaTable()
Imports System
Imports System.Data
Imports System.Data.SqlClient
public class MainClass
Shared Sub Main()
Dim thisConnection As New SqlConnection("server=(local)\SQLEXPRESS;" & _
"integrated security=sspi;database=MyDatabase")
Dim sql As String = "SELECT * FROM Employee"
Dim thisCommand As New SqlCommand(sql, thisConnection)
Try
thisConnection.Open()
Console.WriteLine("Connection Opened")
Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
Dim schema As DataTable = thisReader.GetSchemaTable()
For Each row As DataRow In schema.Rows
For Each col As DataColumn In schema.Columns
Console.WriteLine(col.ColumnName & " = " & row(col).ToString())
Next
Next
thisReader.Close()
Catch ex As SqlException
Console.WriteLine("Error: " & ex.ToString())
Finally
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try
End Sub
End Class
SqlDataReader.GetString
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class MainClass
Shared Dim WithEvents con As SqlConnection
Shared Sub Main()
con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
Dim cmd As New SqlCommand()
cmd.rumandText = "SELECT FirstName, LastName FROM Employee"
cmd.Connection = con
Try
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
While reader.Read()
Console.WriteLine("{0} - {1}", _
reader.GetString(0), reader.GetString(1))
End While
reader.Close()
Finally
con.Close()
End Try
End Sub
End Class
SqlDataReader.NextResult()
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class MainClass
Shared Dim WithEvents con As SqlConnection
Shared Sub Main()
con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
Dim cmd As New SqlCommand()
cmd.rumandText = "SELECT * FROM Employee; SELECT * FROM Employee"
cmd.Connection = con
Try
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
Do
While reader.Read()
Console.WriteLine(reader(0) & vbTab & reader(1))
End While
Loop While reader.NextResult()
reader.Close()
Finally
con.Close()
End Try
End Sub
End Class
SqlDataReader.Read()
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class MainClass
Shared Dim WithEvents con As SqlConnection
Shared Sub Main()
con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
Dim cmd As New SqlCommand()
cmd.rumandText = "SELECT FirstName, LastName FROM Employee"
cmd.Connection = con
Try
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
While reader.Read()
Console.WriteLine("{0} - {1}", _
reader.GetString(0), reader.GetString(1))
End While
reader.Close()
Finally
con.Close()
End Try
End Sub
End Class