❶ VB與各資料庫的幾種連接方式
-、用DAO控制項連接資料庫1.與Access2000資料庫連接Private Sub Command1_Click()'也可直接在控制項屬性中設置以下各項但在控制項屬性中不能寫入密碼'只有在數據數沒有密碼的情況下可以省略Data1.RefreshData1.Connect = "Access 2000;"Data1.DatabaseName = App. Path + "/chncmadb.mdb"'資料庫沒有密碼此句可省Data1.Connect = ";pwd=123456"'Data1.RecordSource = "耕地資源管理單元屬性數據表2004" Data1.RecordSource = "select * from耕地資源管理單元屬性數據表2004"Data1.Refresh』move後才能正確顯示記錄個數End Sub2.與沒有密碼的DBF文件資料庫連接Private Sub Command2_Click()Data1.Connect = "dBASE III;"Data1.DatabaseName = App. Path' Data1.RecordSource ="DBF"Data1.RecordSource = "select * from dbf"Data1.Refresh』move後才能正確顯示記錄個數End Sub3.與沒有密碼的Excel文件資料庫連接Private Sub Command3_Click()Data1.Connect = "Excel 8.0;"Data1.DatabaseName = App.Path & "/EXcel.xls"Data1.RecordSource = "select * from [EXcel.xls]"Data1.Refresh』move後才能正確顯示記錄個End Sub 二、用DAO代碼連接資料庫'在使用DAO對象前應選定Visual Basic菜單下的[工程]中的引用了菜單中的[Microsoft DAO 3.6 Object Library]選項,或其它版本1.DAO代碼與Access資料庫連接Private Sub Command1_Click()Dim Db As DatabaseDim Rs As Recordset'以共享、讀寫方式打開'如果無密碼最後一個參數可以不要Set Db= OpenDatabase(App.Path & "/chncmadb.mdb", False, False, ";pwd=123456")'不需要move來更新記錄個數 'Set Rs = Db.OpenRecordset("耕地資源管理單元屬性數據表2004") '需要move來更新記錄個數Set Rs = Db.OpenRecordset("select * from [耕地資源管理單元屬性數據表2004]")If Rs.RecordCount > 0 Then Rs.MoveLast Rs.MoveFirstEnd IfEnd Sub 2.DAO代碼與沒有密碼的DBF文件資料庫連接Private Sub Command2_Click()Dim Db As DatabaseDim Rs As Recordset'以共享、讀寫方式打開Set Db = OpenDatabase(App.Path, False, False, "dbase III;") '不需要move來更新記錄個數'Set Rs = Db.OpenRecordset("DBF")』需要move來更新記錄個數Set Rs = Db.OpenRecordset("select * from [DBF]") If Rs.RecordCount > 0 Then Rs.MoveLast Rs.MoveFirstEnd IfEnd sub 3. 'DAO代碼與沒有密碼的Excel文件資料庫連接Private Sub Command3_Click()Dim Db As DatabaseDim Rs As Recordset'以共享、讀寫方式打開'如果無密碼最後一個參數可以不要Set Db = OpenDatabase(App.Path & "/EXcel.xls", False, False, "Excel 8.0;")'不需要move來更新記錄個數 ' Set Rs = Db.OpenRecordset("EXcel.xls") '表格中的工作目錄sheet '需要move來更新記錄個數Set Rs = Db.OpenRecordset("select * from [EXcel.xls]") '表格中的工作目錄sheet'If Rs.RecordCount > 0 Then Rs.MoveLast Rs.MoveFirstEnd IfEnd Sub 三、用ADO控制項連接資料庫'也可直接在控制項屬性中設置以下各項1.ADO控制項與Access2000資料庫連接Private Sub Command1_Click() '連接有密碼的Access資料庫 'Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "/chncmadb1.mdb;Jet OLEDB:DataBase PASSWORD=123456" '連接沒有密碼的Access資料庫 Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "/chncmadb.mdb;Persist Security Info=False" 'Adodc1.RecordSource = "[耕地資源管理單元屬性數據表2004]" Adodc1.RecordSource = "select * from [耕地資源管理單元屬性數據表2004]" Adodc1.Refresh Set DataGrid1.DataSource = Adodc1 DataGrid1.RefreshEnd Sub 2.'ADO控制項與DBF表連接Private Sub Command2_Click() 'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=dBASE Files;DBQ=" & App.Path & ";SourceType=DBF;" 'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DSN=Visual FoxPro Tables;UID=;SourceDB=」& app.path &」;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes;" 'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DSN=dBASE Files;DBQ=」& app.path &」;;DefaultDir=」& app.path &」;DriverId=533;MaxBufferSize=2048;PageTimeout=5;" '能使表名長度不受限制 Adodc1.ConnectionString = "Provider=MSDASQL.1;Driver=Microsoft Visual Foxpro Driver;SourceDB=" & App.Path & ";SourceType=DBF;Locale Identifier=2052" 'Adodc1.RecordSource = "[DBF1]" Adodc1.RecordSource = "select * from DBF1" Adodc1.Refresh Set DataGrid1.DataSource = Adodc1 DataGrid1.RefreshEnd Sub 3.'ADO控制項與Excel表連接Private Sub Command3_Click() '下面一句測試未能通過 'Adodc1.ConnectionString = "Data Provider=MSDASQL.1;driver=Microsoft Excel Driver *.xls);DBQ=" & App.Path & "/EXcel.xls" 'Adodc1.ConnectionString="Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DSN=Excel Files;DBQ=" & App.Path & "/EXcel.xls;DefaultDir=」&app.path &」;DriverId=790;MaxBufferSize=2048;PageTimeout=5;" Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "/EXcel.xls;Extended Properties='Excel 8.0;HDR=Yes'" 'Adodc1.RecordSource = "[EXcel.xls]" Adodc1.RecordSource = "select * from [EXcel.xls]" Adodc1.Refresh Set DataGrid1.DataSource = Adodc1 DataGrid1.RefreshEnd Sub 4.'ADO控制項與Oracle資料庫連接Private Sub Command4_Click() 'Adodc1.ConnectionString = "Provider=MSDAORA.1;Password=chncmadb;User ID=chncmadb;Data Source=towebserver;Persist Security Info=True"Adodc1.ConnectionString="Provider=OraOLEDB.Oracle.1;Password=chncmadb;Persist Security Info=True;User ID=chncmadb;Data Source=towebserver" 'Adodc1.RecordSource = "T320481TR012004" '表名不能加方括弧 Adodc1.RecordSource = "select * from T320481TR012004" Adodc1.Refresh Set DataGrid1.DataSource = Adodc1 DataGrid1.RefreshEnd Sub 5.'ADO控制項與SQLserver資料庫連接'未測試Private Sub Command5_Click() Adodc1.ConnectionString = "Provider=SQLOLEDB.1;Password=111;Persist Security Info=True;User ID=111;Initial Catalog=111;Data Source=111" 'Adodc1.RecordSource = "T320481TR012004" Adodc1.RecordSource = "select * from T320481TR012004" Adodc1.Refresh Set DataGrid1.DataSource = Adodc1 DataGrid1.RefreshEnd Sub 四、用ADO代碼連接資料庫'在使用ADO對象前應選定Visual Basic菜單下的[工程]中的引用了菜單中的[Microsoft.ActiveX Data Object 2.5 Library]選項,或其它版本1.'ADO代碼與Access2000資料庫連接Private Sub Command1_Click() Dim AdoCnn As ADODB.Connection Dim AdoRs As ADODB.Recordset Set AdoCnn = New ADODB.Connection Set AdoRs = New ADODB.Recordset AdoCnn.CursorLocation = adUseClient '.open後面的字元串可以參考ADO控制項連接.ConnectionString後面的的字元串 AdoCnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "/chncmadb1.mdb;Jet OLEDB:DataBase PASSWORD=123456"AdoRs.Open "select * from [耕地資源管理單元屬性數據表2004]", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText Set DataGrid1.DataSource = AdoRs Set AdoRs = Nothing Set AdoCnn = NothingEnd Sub 2.'ADO代碼與DBF表連接Private Sub Command2_Click() Dim AdoCnn As ADODB.Connection Dim AdoRs As ADODB.Recordset Set AdoCnn = New ADODB.Connection Set AdoRs = New ADODB.Recordset AdoCnn.CursorLocation = adUseClient '.open後面的字元串可以參考ADO控制項連接.ConnectionString後面的的字元串 AdoCnn.Open "Provider=MSDASQL.1;Driver=Microsoft Visual Foxpro Driver;SourceDB=" & App.Path & ";SourceType=DBF;Locale Identifier=2052"AdoRs.Open "select * from [DBF1]", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText Set DataGrid1.DataSource = AdoRs Set AdoRs = Nothing Set AdoCnn = NothingEnd Sub3.'ADO代碼與Excel表連接Private Sub Command3_Click() Dim AdoCnn As ADODB.Connection Dim AdoRs As ADODB.Recordset Set AdoCnn = New ADODB.Connection Set AdoRs = New ADODB.Recordset AdoCnn.CursorLocation = adUseClient '.open後面的字元串可以參考ADO控制項連接.ConnectionString後面的的字元串 AdoCnn.Open"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "/EXcel.xls;Extended Properties='Excel 8.0;HDR=Yes'"AdoRs.Open "select * from [EXcel.xls]", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText Set DataGrid1.DataSource = AdoRs Set AdoRs = Nothing Set AdoCnn = NothingEnd Sub 4.'ADO代碼與Oracle資料庫連接Private Sub Command4_Click() Dim AdoCnn As ADODB.Connection Dim AdoRs As ADODB.Recordset Set AdoCnn = New ADODB.Connection Set AdoRs = New ADODB.Recordset AdoCnn.CursorLocation = adUseClient '.open後面的字元串可以參考ADO控制項連接.ConnectionString後面的的字元串 AdoCnn.Open "Provider=OraOLEDB.Oracle.1;Password=chncmadb;Persist Security Info=True;User ID=chncmadb;Data Source=towebserver"AdoRs.Open "select * from T320481TR012004", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText Set DataGrid1.DataSource = AdoRs Set AdoRs = Nothing Set AdoCnn = NothingEnd Sub 5.'ADO代碼與SQLserver資料庫連接'未測試Private Sub Command5_Click() Dim AdoCnn As ADODB.Connection Dim AdoRs As ADODB.Recordset Set AdoCnn = New ADODB.Connection Set AdoRs = New ADODB.Recordset AdoCnn.CursorLocation = adUseClient '.open後面的字元串可以參考ADO控制項連接.ConnectionString後面的的字元串 AdoCnn.Open "Provider=SQLOLEDB.1;Password=111;Persist Security Info=True;User ID=111;Initial Catalog=111;Data Source=111"AdoRs.Open "select * from T320481TR012004", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText Set DataGrid1.DataSource = AdoRs Set AdoRs = Nothing Set AdoCnn = NothingEnd Sub
❷ 求VB用winsock連接mysql的方法!
用winsock 連接太麻煩了吧。
還不如用adoc
❸ 在VB中,如何連接MYSQL
樓上講的是VB2005的連接方法,不知道你用的是什麼版本的VB,我給你一個VB6.0的連接方法吧
先在本機上安裝最新的ODBC CONNECTER/mysql
無需設置ODBC,就可以使用.
注意MYSQL的帳戶登錄是根據主機的,設置好MYSQL的帳戶後再做測試.
測試代碼:
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Dim sql As String
'connect to MySQL server using MySQL ODBC 3.51 Driver
Set conn = New ADODB.Connection
conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
& "SERVER=192.168.0.1;" _
& " DATABASE=db_name;" _
& "UID=username;PWD=password; OPTION=3"
conn.Open
'create table
conn.Execute "DROP TABLE IF EXISTS my_ado"
conn.Execute "CREATE TABLE my_ado(id int not null primary key, name varchar(20)," _
& "txt text, dt date, tm time, ts timestamp)"
'direct insert
conn.Execute "INSERT INTO my_ado(id,name,txt) values(1,100,'venu')"
conn.Execute "INSERT INTO my_ado(id,name,txt) values(2,200,'MySQL')"
conn.Execute "INSERT INTO my_ado(id,name,txt) values(3,300,'Delete')"
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseServer
'fetch the initial table ..
rs.Open "SELECT * FROM my_ado", conn
Debug.Print rs.RecordCount
rs.MoveFirst
Debug.Print String(50, "-") & "Initial my_ado Result Set " & String(50, "-")
For Each fld In rs.Fields
Debug.Print fld.name,
Next
Debug.Print
Do Until rs.EOF
For Each fld In rs.Fields
Debug.Print fld.value,
Next
rs.MoveNext
Debug.Print
Loop
rs.Close
conn.Close
❹ vb連接mysql資料庫
ODBC載入.
vb中添加一個Adodc的控制項.
Public Function conn() As String
conn = "Provider=SQLOLEDB.1;Password=資料庫管理密碼;Persist Security Info=True;User ID=資料庫管理帳號;Initial Catalog=資料庫名;Data Source=計算機名"
End Function
Dim rs As New ADODB.Recordset
Dim con As New ADODB.Connection
Dim sql As String
Set con = CreateObject("ADODB.Connection")
con.Open conn
sql = "select * from Admin where username='" & username.Text & "' and password='" & password.Text & "'"
rs.Open sql, con, 1, 1
❺ vb如何連接操作MYSQL資料庫啊
必須安裝mysql的驅動才行,不管是vb還是c++都需要這個驅動。
代碼如下:
public
function
exemysql(byval
sql
as
string)
as
adodb.recordset
sql
=
trim$(sql)
set
conn
=
new
adodb.connection
set
rs
=
new
adodb.recordset
conn.open
"driver=mysql
odbc
3.51
driver;"
&
_
"server=伺服器地址;"
&
_
"port=3306;"
&
_
"database=資料庫名;"
&
_
"uid=用戶名;"
&
_
"pwd=密碼"
conn.defaultdatabase
=
"資料庫名"
conn.cursorlocation
=
aseclient
set
rs.activeconnection
=
conn
rs.locktype
=
adlockbatchoptimistic
rs.cursortype
=
adopenkeyset
rs.open
sql
set
exesql
=
rs
set
rs
=
nothing
set
conn
=
nothing
end
function
❻ vb如何連接操作MYSQL資料庫
必須安裝MYSQL的驅動才行,不管是VB還是c++都需要這個驅動。代碼如下: Public Function exemysql(ByVal sql As String) As ADODB.Recordset sql = Trim$(sql) Set conn = New ADODB.Connection Set rs = New ADODB.Recordset conn.Open "Driver=MySQL ODBC 3.51 Driver;" & _ "Server=伺服器地址;" & _ "Port=3306;" & _ "Database=資料庫名;" & _ "Uid=用戶名;" & _ "Pwd=密碼" conn.DefaultDatabase = "資料庫名" conn.CursorLocation = adUseClient Set rs.ActiveConnection = conn rs.LockType = adLockBatchOptimistic rs.CursorType = adOpenKeyset rs.Open sql Set exesql = rs Set rs = Nothing Set conn = Nothing End Function
❼ VB6.0使用什麼方法連接mysql資料庫比較好且不需要再每台客戶端機器上安裝程序
在VB中可以使用ADO數據控制項實現對MySQL資料庫的訪問,但ADO控制項不能直接訪問MySQL,需要安裝MyODBC(可以在網上下載)並在ODBC中注冊一個用戶DSN(控制面板/管理工具/數據源),這樣就能使VB順利連接到MySQL資料庫了。
連接上了還需要執行SQL查詢操作並能取得查詢結果,可以使用ADODB的Command對象和RecordSet對像。
❽ vb6.0如何連接MYSQL資料庫
最簡單的辦法是:在VB里添加一個ADO部件,將其放到窗體中,然後右鍵選擇屬性,在其屬性頁的下面有個生成連接字元串的項,點擊其後的按鈕,然後一步步的來做(很簡單的),然後連接字元串就會在那裡生成,只要拷貝這個字元串,就可以用ADODB來連接了。。。
❾ vb6.0 連接資料庫共有那幾種方法
1. 定義連接對象
Global DBconnect As New ADODB.Connection
2. 執行連接語句
If DBconnect.State = adStateOpen And Not IsEmpty(adStateOpen) Then DBconnect.Close
l 連接ODBC
DBconnect.ConnectionString = "dsn=DataAliasName;uid=UserID;pwd=Passwd;"
l 直接連接Access為
Dbconnect.Provider = "Microsoft.jet.OLEDB.4.0" // Access 97為3.51
DBconnect.ConnectionString = "FilePathFileName.mdb"
l 連接Oracle
DBconnect.Provider = "MSADORA"
DBconnect.ConnectionString = "user/mypass@servicename
l 連接SQL Server
DBconnect.Provider = "SQLOLEDB.1"
DBconnect.ConnectionString = "DATABASE= ;SERVER= ;UID= ;PWD= ;"
或者可以使用 DBconnect.Open "SERVER" , "USERID" , "PASSWORD"
DBConnect.Open
3. 取查詢結果集
Global RS As New ADODB.Recordset
Global PS As New ADODB.Recordset
If RS.State = adStateOpen And Not IsEmpty(adStateOpen) Then RS.Close
RS.Open SQLStr, DBconnect, adOpenKeyset
4. 執行SQL語句
DBconn.Execute SQLStr
5. 關閉資料庫
DBconnect.Close