VB 串連mysql資料庫

來源:互聯網
上載者:User

 1.使用xamp內建的mysql資料庫

2,下載mysql-connector-odbc-3.51.28-win32.msi,添加資料來源conn 配置如下

 

 

 

3.開啟xamp控制台,運行apache,mysql服務

 

4,鍵入localhost,進入管理頁面,添加sohard資料庫,並在sohard資料庫下添加user表,在表中寫入一條記錄,使用者名稱wangyang

 

5,建立VB項目,鍵入如下代碼

Private Sub Command1_Click()

' 定義並建立資料庫連接和訪問對象
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

' 定義資料庫連接字串變數
Dim strCn As String

' 定義資料庫連接參數變數
Dim db_host As String
Dim db_user As String
Dim db_pass As String
Dim db_data As String
Dim db_result As String

' 定義 SQL 陳述式變數
Dim sql As String

' 初始化資料庫連接變數
db_host = "127.0.0.1"
db_user = "root"
db_pass = ""
db_data = "sohard"

strCn = "DRIVER={MySQL ODBC 3.51 Driver};" & _
         "SERVER=" & db_host & ";" & _
         "DATABASE=" & db_data & ";" & _
         "UID=" & db_user & ";PWD=" & db_pass & ";" & _
         "OPTION=3;stmt=SET NAMES GB2312"

' stmt=SET NAMES GB2312
' 這句是設定資料庫編碼方式
' 中文作業系統需要設定成 GB2312
' 這樣中文才不會有問題
' 版本要求 mysql 4.1+

' 串連資料庫
cn.Open strCn
' 設定該屬性, 使 recordcount 和 absolutepage 屬性可用
cn.CursorLocation = adUseClient

' 訪問表users
sql = "select UName from dh_user"
 rs.Open sql, cn
MsgBox rs.Fields(0)

 

End Sub

Private Sub Command2_Click()
Unload Me
End Sub

Private Sub Form_Load()
Text1.Text = "127.0.0.1"
Text2.Text = "3306"
Text3.Text = "root"
Text4.Text = ""
Text5.Text = "sohard"

End Sub

 

6,運行結果如下

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.