首页 > 解决方案 > 如何使用vba连接mysql数据库?

问题描述

选项显式

Sub excelmysql() ' 连接变量 Dim conn As New ADODB.Connection Dim Server_Name As String Dim Database_Name As String Dim User_ID As String Dim Password As String ' 表动作变量 Dim i As Long ' counter Dim strSQL As String ' 执行各种动作的 SQL将表 1 调暗为字符串 将字段 1 调为字符串,将字段 2 调为字符串 调暗 rs 为 ADODB.Recordset 调暗 vtype 作为变体

Server_Name = "localhost" ' 在此处输入您的服务器名称 - 如果从本地计算机运行,请使用 127.0.0.1

Database_Name = "PRMJIRA" ' 在此处输入您的数据库名称

User_ID = "root" ' 在此处输入您的用户 ID

Password = "Pradee@1234" ' 在此输入您的密码

Set conn = New ADODB.Connection

conn.Open "DRIVER={MySQL ODBC 8.0 Unicode Driver}" _
          & ";SERVER=" & Server_Name _
          & ";DATABASE=" & Database_Name _
          & ";UID=" & User_ID _
          & ";PWD=" & Password _
          & ";OPTION=16427"

'conn.Open


vtype = Array("varchar(255)", "Text", "LongText", "Int(10)", "Float", "Double", "Date", "Time")    ' array of commonly used MySQL variable types
table1 = "P" & Range("H1").Value

field1 = "field1text"

field2 = "field2text"

strSQL = "CREATE TABLE `" & table1 & "` (`" _
         & field1 & "` " & vtype(0) & ",`" _
         & field2 & "` " & vtype(4) _
         & ")"

conn.Execute strSQL

On Error Resume Next

rs.Close

Set rs = Nothing

conn.Close

Set conn = Nothing

On Error GoTo 0

结束子

标签: mysqlexcelvbadatabase

解决方案


推荐阅读