Dim
創(chuàng)新互聯(lián)專注于青白江網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供青白江營銷型網(wǎng)站建設(shè),青白江網(wǎng)站制作、青白江網(wǎng)頁設(shè)計(jì)、青白江網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造青白江網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供青白江網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
sqlConnection1
As
SqlClient.SqlConnection
Dim
strConnect
As
String=”data
source=服務(wù)器名;initial
catalog=數(shù)據(jù)庫名;user
id=sa;password=;”
sqlConnection1=New
System.Data.SqlClient.SqlConnection(strConnect)
sqlConnection1.open
‘打開數(shù)據(jù)庫
sqlConnection1.close
‘關(guān)閉連接,釋放資源
1)連接Sql
Server數(shù)據(jù)庫
Dim
odbcDbConnetion1
As
Microsoft.Data.OdbcConnection
Dim
strConnect
As
Sting=”Driver={SQL
Server};Server=服務(wù)器名;Uid=sa;pwd=;Database=
數(shù)據(jù)庫名;”
odbcDbConnetion1=New
Microsoft.Data.OdbcConnection(strConnect)
1·綁定數(shù)據(jù)源來進(jìn)行連接
2.用代碼連接
先到數(shù)據(jù)庫建立一個(gè)數(shù)據(jù)庫和相應(yīng)的表
連接數(shù)據(jù)庫的代碼:
Dim str As String = "Data Source=服務(wù)器名;Initial Catalog=數(shù)據(jù)庫名;Persist Security Info=True;User ID=;Password="
dim conn As SqlClient.SqlConnection
try
conn = New SqlClient.SqlConnection
conn.ConnectionString = str
conn.Open()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
登錄代碼:Dim str As String = "Data Source=服務(wù)器名;Initial Catalog=數(shù)據(jù)庫名;Persist Security Info=True;User ID=;Password="
dim conn As SqlClient.SqlConnection
conn = New SqlClient.SqlConnection
conn.ConnectionString = str
conn.Open()
sqlstr = "Select * From Amd Where AmdName='" TextBox1.Text "' And AmdPwd = '" TextBox2.Text "'"
Dim sqlcmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sqlstr, conn)
Dim dr As SqlClient.SqlDataReader
dr = sqlcmd.ExecuteReader
If dr.Read = True Then '判斷一條記錄為真
kf.Show() '顯示下個(gè)窗體
Me.Hide() ’隱藏當(dāng)前窗體
Else
MessageBox.Show("輸入信息有誤!", "提示")
TextBox1.Text = ""
TextBox2.Text = ""
End If
你說這么一大篇全是業(yè)務(wù)邏輯,沒說明你遇到的技術(shù)性問題在哪
.net自帶的config文件來配置連接字符串,比ini好N倍,如果你非要用ini那么自己解決問題
檢測(cè)是否能連接用Connection.Open加try catch就可以
1、 用The SQL Server .NET Data Provider連接數(shù)據(jù)庫
The SQL Server .NET Data Provider是利用SqlConnection類來連接SQL Server7.0或更高版本的數(shù)據(jù)庫,
SqlConnection類位于名稱空間System.Data.SqlClient下。
連接代碼:
Dim sqlConnection1 As SqlClient.SqlConnection
Dim strConnect As String=”data source=服務(wù)器名;initial catalog=數(shù)據(jù)庫名;user id=sa;password=;”
sqlConnection1=New System.Data.SqlClient.SqlConnection(strConnect)
sqlConnection1.open ‘打開數(shù)據(jù)庫
sqlConnection1.close ‘關(guān)閉連接,釋放資源
2、 用The OLE DB .NET Data Provider連接數(shù)據(jù)庫
上面已經(jīng)說過,利用The OLE DB .NET Data Provider可以訪問Access、Oracle和SQL Server等種數(shù)據(jù)
庫,那么,它是怎樣訪問這些數(shù)據(jù)庫的呢?The OLE DB .NET Data Provider是通過位于名稱空間Sy
stem.Data.OleDb類庫下的OleDbConnection類來連接這三種不同類型的數(shù)據(jù)庫的。下面舉例說明:
1)連接SQL Server數(shù)據(jù)庫
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”Provider=SQLOLEDB;Persist Security Info=False;Data Source=服務(wù)器名;Initial Catalog=數(shù)據(jù)庫名;User ID=sa;Password=;”
oleDbConnection1=New System.Data.OleDb.OleDbConnection(strConnect)
2)連接Access數(shù)據(jù)庫
假設(shè)要連接的Access數(shù)據(jù)庫名為“Example.mdb”,存放在d:\Data\目錄下。
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\Data\ Example.mdb”
oleDbConnection1= New System.Data.OleDb.OleDbConnection(strConnect)
3)連接Oracle數(shù)據(jù)庫
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”Provider=MSDAORA;Data Source=服務(wù)器名;User ID=用戶ID;Password=密碼;”
oleDbConnection1= New System.Data.OleDb.OleDbConnection(strConnect)
3、 用The ODBC .NET Data Provider連接數(shù)據(jù)庫
The ODBC .NET Data Provider連接數(shù)據(jù)庫是通過OdbcConnection類來實(shí)現(xiàn)的,這個(gè)類位于名稱空間
Microsoft.Data.Odbc下,而名稱空間Microsoft.Data.Odbc是封裝在Microsoft.Data.Odbc.dll文件下的。
由于篇幅有限,這里就只介紹連接Sql Server和Oracle數(shù)據(jù)庫的方法,其他數(shù)據(jù)庫的連接方法基本類
似,我就不再多講了。
1)連接Sql Server數(shù)據(jù)庫
Dim odbcDbConnetion1 As Microsoft.Data.OdbcConnection
Dim strConnect As Sting=”Driver={SQL Server};Server=服務(wù)器名;Uid=sa;pwd=;Database= 數(shù)據(jù)庫名;”
odbcDbConnetion1=New Microsoft.Data.OdbcConnection(strConnect)
2)連接Oracle數(shù)據(jù)庫
Dim odbcDbConnetion1 As Microsoft.Data.OdbcConnection
Dim strConnect As Sting=”Driver={Microsoft ODBC for Oracle};Server=服務(wù)器名;Uid=sa;pwd=;”
odbcDbConnetion1=New Microsoft.Data.OdbcConnection(strConnect)
四、總結(jié)
通過本文的介紹,讀者基本掌握了在Visual Basic.NET中用ADO.NET和ODBC.NET連接各種數(shù)據(jù)庫的方法
。以上三種驅(qū)動(dòng)針對(duì)不同的數(shù)據(jù)庫,它們的性能方面也有很大的不同:The SQL Server .NET Data Provider
的效率最高;The OLE DB .NET Data Provider的效率比較底;The ODBC .NET Data Provider的效率最慢。
具體連接哪一種數(shù)據(jù)庫選用哪一種數(shù)據(jù)驅(qū)動(dòng)要從工作效率方面來考慮。
以上回答你滿意么?
VB.NET中有控件可以真接接點(diǎn)擊屬性進(jìn)行連接配置.
SqlConnect數(shù)據(jù)連接控件.點(diǎn)擊ConnectionString屬性進(jìn)行配置就可以了.
VB.NET中沒有RecordSet了.取而代之的是DataSet.
在VB.net中可以先用SqlConnection對(duì)像來連接數(shù)據(jù)庫再用SqlDataAdapter對(duì)像來讀取數(shù)據(jù)并填充到DataSet里.然后就可以進(jìn)行數(shù)據(jù)綁定了.
例:
dim
conn
as
SqlConnection
dim
da
as
SqlDataAdapter
dim
ds
as
Dataset
set
conn
=
new
SqlConnection
conn.ConnectionString="server=serverIP;database=databasename;uid=userid;pwd=password";
conn.open
da
=
new
SqlDataAdapter
(,conn)
da.fill(ds)
Imports System.Data
Imports System.Data.SqlClient
Module 連接數(shù)據(jù)庫
Public Server1 As String
Public DataBase1 As String
Public Function GetTable(ByVal sqlstr$) As DataTable
Dim SqlConn As SqlConnection = New SqlConnection("server=" Server1 ";database=" DataBase1 ";integrated security=true") '聲明這些SQL的類
Dim facDataAdapter As SqlDataAdapter = New SqlDataAdapter
Dim SqlComm As SqlCommand = New SqlCommand(sqlstr, SqlConn)
facDataAdapter.SelectCommand = SqlComm
Dim dt As DataTable = New DataTable
facDataAdapter.Fill(dt)
Return dt
End Function
End Module