VB.Net Tutorial/Database ADO.net/OleDbConnection — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:56, 26 мая 2010
Create OleDbConnection to Access database
Imports System.Data
Module Test
Sub Main()
Dim sConnectionString, sSQL As String
"SQL Connection String
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\yourdatabase.mdb"
sSQL = "SELECT Title FROM yourTable"
Dim conn As New System.Data.OleDb.OleDbConnection(sConnectionString)
Dim cmd As New System.Data.OleDb.OleDbCommand(sSQL, conn)
Dim dr As System.Data.OleDb.OleDbDataReader
conn.Open()
dr = cmd.ExecuteReader()
Do While dr.Read()
System.Console.WriteLine(dr.Item("Title"))
Loop
dr.Close()
conn.Close()
End Sub
End Module
Create OleDbConnection to Sql server
Imports System.Data
Module Test
Sub Main()
Dim sConnectionString, sSQL As String
"SQL Connection String
sConnectionString = "Provider=SQLOLEDB.1;User ID=sa;Initial Catalog=YourDataBase;Data Source=(local)"
sSQL = "SELECT Title FROM yourTable"
Dim conn As New System.Data.OleDb.OleDbConnection(sConnectionString)
Dim cmd As New System.Data.OleDb.OleDbCommand(sSQL, conn)
Dim dr As System.Data.OleDb.OleDbDataReader
conn.Open()
dr = cmd.ExecuteReader()
Do While dr.Read()
System.Console.WriteLine(dr.Item("Title"))
Loop
dr.Close()
conn.Close()
End Sub
End Module
Using sql statement
Option Strict On
Imports System.Data
Imports System.Data.OleDb
Public Module UsingStatement
Public Sub Main()
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=NorthWind.mdb")
Dim cmd As New OleDbCommand
Dim dr As OleDbDataReader
Using conn
conn.Open
cmd.rumandText = "Select * From Customers"
cmd.rumandType = CommandType.Text
cmd.Connection = conn
dr = cmd.ExecuteReader()
If dr.HasRows Then
Do While dr.Read()
" Get first and last name of customer
Console.WriteLine(CStr(dr.Item(1)) & " " & CStr(dr.Item(2)))
Loop
Else
Console.WriteLine("The table has no rows.")
End If
End Using
End Sub
End Module