数据库
首页 > 数据库> > Excel VBA:写入mysql数据库

Excel VBA:写入mysql数据库

作者:互联网

我想在Excel中编写一个写入mysql数据库的宏.有人可以请我开始这个吗?

解决方法:

您可以使用连接字符串和ADO连接到MySQL

''http://support.microsoft.com/kb/246335
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

strCon = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=MyDB;" _
& "User=root;Password=pw;Option=3;"

cn.Open strCon

您还可以使用Jet驱动程序将DSN连接到Excel:

Dim cn As ADODB.Connection

''Not the best way to get the name, just convenient for notes
strFile = Workbooks(1).FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

Set cn = CreateObject("ADODB.Connection")

''For this to work, you must create a DSN and use the name in place of 
''DSNName
strSQL = "INSERT INTO [ODBC;DSN=DSNName;].NameOfMySQLTable " _
& "Select AnyField As NameOfMySQLField FROM [Sheet1$];"

cn.Execute strSQL

标签:sql,mysql,excel,vba,excel-vba
来源: https://codeday.me/bug/20190923/1815533.html