VB.Net/Database ADO.net/SqlCommand Select — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:44, 26 мая 2010
Содержание
Execute two queries together
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")
"Sql Query 1
Dim sql1 As String ="SELECT FirstName FROM Employee; "
"Sql Query 2
Dim sql2 As String ="SELECT LastName FROM Employee;"
"Combine queries
Dim sql As String = sql1 & sql2
"Create Command object
Dim thisCommand As New SqlCommand _
(sql, thisConnection)
Try
" Open Connection
thisConnection.Open()
Console.WriteLine("Connection Opened")
" Execute Query
Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
Catch ex As SqlException
" Display error
Console.WriteLine("Error: " & ex.ToString())
Finally
" Close Connection
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try
End Sub
End Class
Get data from "Select" command
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
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")
" Create the command object
Dim str As String = "SELECT ID, FirstName, LastName FROM Employee"
Dim cmd As New SqlCommand(str, con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "Employee")
Dim dc As DataColumn
For Each dc In ds.Tables(0).Columns
Console.Write("{0,15}", dc.ColumnName)
Next
Console.Write(vbCrLf)
Dim dr As DataRow
For Each dr In ds.Tables(0).Rows
Dim i As Integer
For i = 1 To ds.Tables(0).Columns.Count
Console.Write("{0,15}", dr(i - 1))
Next i
Console.Write(vbCrLf)
Next
End Sub
End Class
Output data from "Select" command in a table format
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
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")
" Create the command object
Dim str As String = "SELECT ID, FirstName, LastName FROM Employee"
Dim cmd As New SqlCommand(str, con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "Employee")
Dim dc As DataColumn
For Each dc In ds.Tables(0).Columns
Console.Write("{0,15}", dc.ColumnName)
Next
Console.Write(vbCrLf)
Dim dr As DataRow
For Each dr In ds.Tables(0).Rows
Dim i As Integer
For i = 1 To ds.Tables(0).Columns.Count
Console.Write("{0,15}", dr(i - 1))
Next i
Console.Write(vbCrLf)
Next
End Sub
End Class
Read the result sets from two "select" command
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")
"Sql Query 1
Dim sql1 As String ="SELECT FirstName, LastName FROM Employee; "
"Sql Query 2
Dim sql2 As String ="SELECT LastName, FirstName FROM Employee;"
"Combine queries
Dim sql As String = sql1 & sql2
"Create Command object
Dim thisCommand As New SqlCommand _
(sql, thisConnection)
Try
" Open Connection
thisConnection.Open()
Console.WriteLine("Connection Opened")
" Execute Query
Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
" Loop through result sets
Do
" Fetch Data
While (thisReader.Read())
Console.WriteLine("{0} | {1}", _
thisReader(0), _
thisReader(1))
End While
Console.WriteLine("".PadLeft(60, "="))
Loop While (thisReader.NextResult())
"Close DataReader
thisReader.Close()
Catch ex As SqlException
" Display error
Console.WriteLine("Error: " & ex.ToString())
Finally
" Close Connection
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try
End Sub
End Class
SqlDataAdapter: "select" command and the connection string
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Data
Public Class MainClass
Shared Sub Main()
Dim da As New SqlDataAdapter( _
"SELECT ID, FirstName, LastName FROM Employee", _
"Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
Dim ds As New DataSet("Employee")
da.FillSchema(ds, SchemaType.Source)
ds.ReadXml("Employee.xml")
Dim Table As DataTable = ds.Tables(0)
Dim numCols As Integer = Table.Columns.Count
Dim Row As DataRow
For Each Row In Table.Rows
Dim i As Integer
For i = 0 To numCols - 1
Console.WriteLine(Table.Columns(i).ColumnName & " = " & Row(i))
Next
Console.WriteLine()
Next
End Sub
End Class
Use SqlDataReader to read result set from "select" command
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")
"Create Command object
Dim thisCommand As New SqlCommand _
("SELECT FirstName, LastName FROM Employee",thisConnection)
Try
" Open Connection
thisConnection.Open()
Console.WriteLine("Connection Opened")
" Execute Query
Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
While (thisReader.Read())
Console.WriteLine("Employee: {0} {1}", _
thisReader.GetValue(0), thisReader.GetValue(1))
End While
Catch ex As SqlException
" Display error
Console.WriteLine("Error: " & ex.ToString())
Finally
" Close Connection
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try
End Sub
End Class