如果樓主熟悉VB6,可以直接在項目中添加ADODB的Com引用,這樣你就可以像VB6那樣操作數(shù)據(jù)庫了!
創(chuàng)新互聯(lián)建站是一家專業(yè)提供蘭坪企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、H5建站、小程序制作等業(yè)務(wù)。10年已為蘭坪眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。
另外
.NET Framework中連接數(shù)據(jù)庫要用到ADO.NET。如果要操作Access數(shù)據(jù)庫,要用到System.Data.OleDb命名空間下的許多類。
比如按樓主所說,“我想在textbox1中顯示表一中【一些數(shù)據(jù)】字段下的第一個內(nèi)容”:
'首先導(dǎo)入命名空間
Imports System.Data
Imports System.Data.OleDb
'然后在某一個事件處理程序中寫:
Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=數(shù)據(jù)庫.accdb;Jet OLEDB:Database Password=MyDbPassword")
Dim command As New OleDbCommand("Select * From 數(shù)據(jù)表", conn)
conn.Open() '打開數(shù)據(jù)庫連接
Dim reader As OleDbDataReader = command.ExecuteReader() '執(zhí)行SQL語句,返回OleDbDataReader 對象
Do While reader.Read() '讀取一條數(shù)據(jù)
textbox1.Text += reader("一些數(shù)據(jù)") VbCrLf
Loop
reader.Close() '關(guān)閉OleDbDataReader
conn.Close() '關(guān)閉連接
Dim?myconn?As?New?OleDb.OleDbConnection
Dim?mycommand?As?New?OleDb.OleDbCommand
myconn.ConnectionString?=?"Provider=Microsoft.ace.OLEDB.12.0;Data?Source=C:\Users\Administrator\Documents\賬號密碼.accdb?"
myconn.Open()
mycommand.Connection?=?myconn
Dim?sql5?As?String?=?"delete?from?表3?where?userid='"??(TextBox1.Text)??"'"
mycommand.CommandText?=?sql5
mycommand.CommandType?=?CommandType.Text
mycommand.ExecuteNonQuery()
Dim?sql3?As?String?=?"update?表2?set?Balance=Balance-2?where?UserID='"??(TextBox1.Text)??"'"
mycommand.CommandText?=?sql3
mycommand.CommandType?=?CommandType.Text
mycommand.ExecuteNonQuery()
myconn.Close()
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類來實現(xiàn)的,這個類位于名稱空間
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ū)動針對不同的數(shù)據(jù)庫,它們的性能方面也有很大的不同:The SQL Server .NET Data Provider
的效率最高;The OLE DB .NET Data Provider的效率比較底;The ODBC .NET Data Provider的效率最慢。
具體連接哪一種數(shù)據(jù)庫選用哪一種數(shù)據(jù)驅(qū)動要從工作效率方面來考慮。
以上回答你滿意么?
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)閉連接,釋放資源
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ù)源來進行連接
2.用代碼連接
先到數(shù)據(jù)庫建立一個數(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() '顯示下個窗體
Me.Hide() ’隱藏當(dāng)前窗體
Else
MessageBox.Show("輸入信息有誤!", "提示")
TextBox1.Text = ""
TextBox2.Text = ""
End If
首先vb.net里大漠插件的findstr命令是不能用的,因為vb.net自身的問題。intx和inty值只會返回0,0. 我們要用findstrE這個命令 pos = findstr。。。。。。
然后用split function給出x和y值