如何使用VBA连接mysql数据库?

时间:2018-08-30 06:12:17

标签: mysql excel database excel-vba

显式选项

子excelmysql() '连接变量 昏暗的conn作为新的ADODB.Connection 昏暗的Server_Name作为字符串 昏暗的Database_Name作为字符串 昏暗的User_ID作为字符串 暗淡的密码作为字符串     '表动作变量 点心柜台 Dim strSQL As String'SQL执行各种操作 昏暗table1作为字符串 Dim field1 as String,field2 As String 作为ADODB.Recordset的昏暗 变型vim类型

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

结束子

0 个答案:

没有答案