本文整理匯總了VB.NET中System.Data.IDbConnection接口的典型用法代碼示例。如果您正苦於以下問題:VB.NET IDbConnection接口的具體用法?VB.NET IDbConnection怎麽用?VB.NET IDbConnection使用的例子?那麽, 這裏精選的接口代碼示例或許可以為您提供幫助。
在下文中一共展示了IDbConnection接口的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Program
' 導入命名空間
Imports System.Data
Class Program
Public Shared Sub Main(args As String())
Dim connection As IDbConnection
' First use a SqlClient connection
connection = New System.Data.SqlClient.SqlConnection("Server=(localdb)\V11.0")
Console.WriteLine("SqlClient" & vbCr & vbLf & "{0}", GetServerVersion(connection))
connection = New System.Data.SqlClient.SqlConnection("Server=(local);Integrated Security=true")
Console.WriteLine("SqlClient" & vbCr & vbLf & "{0}", GetServerVersion(connection))
' Call the same method using ODBC
' NOTE: LocalDB requires the SQL Server 2012 Native Client ODBC driver
connection = New System.Data.Odbc.OdbcConnection("Driver={SQL Server Native Client 11.0};Server=(localdb)\v11.0")
Console.WriteLine("ODBC" & vbCr & vbLf & "{0}", GetServerVersion(connection))
connection = New System.Data.Odbc.OdbcConnection("Driver={SQL Server Native Client 11.0};Server=(local);Trusted_Connection=yes")
Console.WriteLine("ODBC" & vbCr & vbLf & "{0}", GetServerVersion(connection))
' Call the same method using OLE DB
connection = New System.Data.OleDb.OleDbConnection("Provider=SQLNCLI11;Server=(localdb)\v11.0;Trusted_Connection=yes;")
Console.WriteLine("OLE DB" & vbCr & vbLf & "{0}", GetServerVersion(connection))
connection = New System.Data.OleDb.OleDbConnection("Provider=SQLNCLI11;Server=(local);Trusted_Connection=yes;")
Console.WriteLine("OLE DB" & vbCr & vbLf & "{0}", GetServerVersion(connection))
End Sub
Public Shared Function GetServerVersion(connection As IDbConnection) As String
' Ensure that the connection is opened (otherwise executing the command will fail)
Dim originalState As ConnectionState = connection.State
If originalState <> ConnectionState.Open Then
connection.Open()
End If
Try
' Create a command to get the server version
' NOTE: The query's syntax is SQL Server specific
Dim command As IDbCommand = connection.CreateCommand()
command.CommandText = "SELECT @@version"
Return DirectCast(command.ExecuteScalar(), String)
Finally
' Close the connection if that's how we got it
If originalState = ConnectionState.Closed Then
connection.Close()
End If
End Try
End Function
End Class