1、 用The SQL Server .NET Data Provider連接數(shù)據(jù)庫
成都創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、銀海網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為銀海等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
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)要從工作效率方面來考慮。
以上回答你滿意么?
你可以將連接到數(shù)據(jù)庫進(jìn)行的操作分成子任務(wù),然后設(shè)定進(jìn)度,例如要做10件事,每做完一件演示一個(gè)動(dòng)畫。至于動(dòng)畫,你可以用Flash做一組圖片然后用圖片框根據(jù)進(jìn)度動(dòng)態(tài)顯示。希望能幫到你。采納請(qǐng)插旗。
VB大全視頻23 SQL在VB中的應(yīng)用,
VB大全視頻23 SQL在VB中的應(yīng)用,
美河提供.上海交大.VB.NET26 - 專輯:數(shù)據(jù)庫基礎(chǔ)視頻教程 ,
在百度搜,視頻 vb sql,能找到不少。
參考一下下面這段代碼就可以了。
Imports System.Data
'引入數(shù)據(jù)庫操作類命名空間
Imports System.Data.OleDb
'引入ADO.NET操作命名空間
Public Class FrmModifystInfo
Inherits System.Windows.Forms.Form
Public ADOcmd As OleDbDataAdapter
Public ds As DataSet = New DataSet()
'建立DataSet對(duì)象
Public mytable As Data.DataTable
'建立表單對(duì)象
Public myrow As Data.DataRow
'建立數(shù)據(jù)行對(duì)象
Public rownumber As Integer
'定義一個(gè)整型變量來存放當(dāng)前行數(shù)
Public SearchSQL As String
Public cmd As OleDbCommandBuilder
'======================================================
#Region " Windows 窗體設(shè)計(jì)器生成的代碼 "
#End Region
'======================================================
Private Sub FrmModifystInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'窗體的載入
TxtSID.Enabled = False
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False '設(shè)置信息為只讀
Dim tablename As String = "student_Info "
SearchSQL = "select * from student_Info "
ExecuteSQL(SearchSQL, tablename) '打開數(shù)據(jù)庫
ShowData() '顯示記錄
End Sub
Private Sub ShowData()
'在窗口中的textbox中顯示數(shù)據(jù)
myrow = mytable.Rows.Item(rownumber)
TxtSID.Text = myrow.Item(0).ToString
TxtName.Text = myrow.Item(1).ToString
ComboSex.Text = myrow.Item(2).ToString
TxtBornDate.Text = Format(myrow.Item(3), "yyyy-MM-dd ")
TxtClassno.Text = myrow.Item(4).ToString
TxtTel.Text = myrow.Item(5).ToString
TxtRuDate.Text = Format(CDate(myrow.Item(6)), "yyyy-MM-dd ")
TxtAddress.Text = myrow.Item(7).ToString
TxtComment.Text = myrow.Item(8).ToString
End Sub
Private Sub BtFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtFirst.Click
'指向第一條數(shù)據(jù)
rownumber = 0
ShowData()
End Sub
Private Sub BtPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtPrev.Click
'指向上一條數(shù)據(jù)
BtNext.Enabled = True
rownumber = rownumber - 1
If rownumber 0 Then
rownumber = 0 '如果到達(dá)記錄的首部,行號(hào)設(shè)為零
BtPrev.Enabled = False
End If
ShowData()
End Sub
Private Sub BtNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtNext.Click
'指向上一條數(shù)據(jù)
BtPrev.Enabled = True
rownumber = rownumber + 1
If rownumber mytable.Rows.Count - 1 Then
rownumber = mytable.Rows.Count - 1 '判斷是否到達(dá)最后一條數(shù)據(jù)
BtNext.Enabled = False
End If
ShowData()
End Sub
Private Sub BtLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtLast.Click
'指向最后一條數(shù)據(jù)
rownumber = mytable.Rows.Count - 1
ShowData()
End Sub
Private Sub BtDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtDelete.Click
mytable.Rows.Item(rownumber).Delete() '刪除記錄
If MsgBox( "確定要?jiǎng)h除改記錄嗎? ", MsgBoxStyle.OKCancel + vbExclamation, "警告 ") = MsgBoxResult.OK Then
cmd = New OleDbCommandBuilder(ADOcmd)
'使用自動(dòng)生成的SQL語句
ADOcmd.Update(ds, "student_Info ")
BtNext.PerformClick()
End If
End Sub
Private Sub BtModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtModify.Click
TxtSID.Enabled = False '關(guān)鍵字段只讀
TxtName.Enabled = True '可讀寫
ComboSex.Enabled = True
TxtBornDate.Enabled = True
TxtClassno.Enabled = True
TxtRuDate.Enabled = True
TxtTel.Enabled = True
TxtAddress.Enabled = True
TxtComment.Enabled = True
End Sub
Private Sub BtUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtUpdate.Click
If Not Testtxt(TxtName.Text) Then
MsgBox( "請(qǐng)輸入姓名! ", vbOKOnly + vbExclamation, "警告 ")
TxtName.Focus()
Exit Sub
End If
If Not Testtxt(ComboSex.Text) Then
MsgBox( "請(qǐng)選擇性別! ", vbOKOnly + vbExclamation, "警告 ")
ComboSex.Focus()
Exit Sub
End If
If Not Testtxt(TxtClassno.Text) Then
MsgBox( "請(qǐng)選擇班號(hào)! ", vbOKOnly + vbExclamation, "警告 ")
TxtClassno.Focus()
Exit Sub
End If
If Not Testtxt(TxtTel.Text) Then
MsgBox( "請(qǐng)輸入聯(lián)系電話! ", vbOKOnly + vbExclamation, "警告 ")
TxtTel.Focus()
Exit Sub
End If
If Not Testtxt(TxtAddress.Text) Then
MsgBox( "請(qǐng)輸入家庭住址! ", vbOKOnly + vbExclamation, "警告 ")
TxtAddress.Focus()
Exit Sub
End If
If Not IsNumeric(Trim(TxtSID.Text)) Then
MsgBox( "請(qǐng)輸入數(shù)字學(xué)號(hào)! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtSID.Focus()
End If
If Not IsDate(TxtBornDate.Text) Then
MsgBox( "出生時(shí)間應(yīng)輸入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtBornDate.Focus()
End If
If Not IsDate(TxtRuDate.Text) Then
MsgBox( "入校時(shí)間應(yīng)輸入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
TxtRuDate.Focus()
Exit Sub
End If
myrow.Item(0) = Trim(TxtSID.Text)
myrow.Item(1) = Trim(TxtName.Text)
myrow.Item(2) = Trim(ComboSex.Text)
myrow.Item(3) = Trim(TxtBornDate.Text)
myrow.Item(4) = Trim(TxtClassno.Text)
myrow.Item(5) = Trim(TxtTel.Text)
myrow.Item(6) = Trim(TxtRuDate.Text)
myrow.Item(7) = Trim(TxtAddress.Text)
myrow.Item(8) = Trim(TxtComment.Text)
mytable.GetChanges()
cmd = New OleDbCommandBuilder(ADOcmd)
'使用自動(dòng)生成的SQL語句
ADOcmd.Update(ds, "student_Info ")
'對(duì)數(shù)據(jù)庫進(jìn)行更新
MsgBox( "修改學(xué)籍信息成功! ", vbOKOnly + vbExclamation, "警告 ")
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False '重新設(shè)置信息為只讀
End Sub
Private Sub BtCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtCancel.Click
TxtSID.Enabled = False
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False
End Sub
Public Function ExecuteSQL(ByVal SQL As String, ByVal table As String)
Try
'建立ADODataSetCommand對(duì)象
'數(shù)據(jù)庫查詢函數(shù)
ADOcmd = New OleDbDataAdapter(SQL, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\student.mdb ")
'建立ADODataSetCommand對(duì)象
ADOcmd.Fill(ds, table) '取得表單
mytable = ds.Tables.Item(0) '取得名為table的表
rownumber = 0 '設(shè)置為第一行
myrow = mytable.Rows.Item(rownumber)
'取得第一行數(shù)據(jù)
Catch
MsgBox(Err.Description)
End Try
End Function
End Class
分享題目:包含vbnet數(shù)據(jù)庫視頻的詞條
分享路徑:http://jinyejixie.com/article38/dosjopp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、靜態(tài)網(wǎng)站、ChatGPT、網(wǎng)站導(dǎo)航、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)