当前位置:   article > 正文

VBA对MySQL进行读取和写入操作介绍_vba openschema

vba openschema

转自:微点阅读  https://www.weidianyuedu.com

"以下代码用于32位系统,Office 2003,环境,MySql版本5.1

"在使用前需要先安装MySql的驱动,进行正确配置

"注意:必须给出正确的服务器名,数据库名,表名,数据库连接的用户名,密码

选项显式

Dim Cnn作为ADODB.Connection"定义ADO连接对象

Dim记录为ADODB.Recordset"定义ADO记录集对象

"连接到数据库

函数CnnOpen(ByVal ServerName As String,ByVal DBName As String,ByVal TblName As String,ByVal User As String,ByVal PWD As String)"服务器名或IP,数据库名,登录用户,密码

Dim CnnStr As String"定义连接字符串

Set Cnn = CreateObject(“ADODB.Connection”)"创建ADO连接对象

Cnn.CommandTimeout = 15"设置超时时间

CnnStr =“DRIVER = {MySql ODBC 5.1 Driver}; SERVER =”&ServerName&“; Database =”&DBName&“; Uid =”&User&“; Pwd =”&PWD &“; Stmt = set names GBK”"

Cnn.ConnectionString = CnnStr

Cnn.Open

End Function

"关闭连接

函数CnnClose()

如果Cnn.State = 1那么

Cnn.Close

结束如果

结束函数

"取得记录集

函数GetRecordset(ByVal SqlStr As String)

Set Records = CreateObject(“ADODB.recordset”)

Records.CursorType = adOpenStatic"设置游标类型,否则无法获得行数

Records.CursorLocation = adUseClient"设置游标属性,否则无法获得行数

"对于连接对象的执行方法产生的记录集对象,一般是一个只读并且只向前的记录集

"如果需要对记录集进行操作,譬如修改和增加,则需要用一个记录对象

“并正确设置好CursorType和LockType为适当类型,然后调用打开方法打开

Records.Open SqlStr,Cnn "使用这个语句,行数将返回-1,设置记录= Conn.Execute(SqlStr)

结束函数

"写入Excel表

函数输入表(ByVal) SheetName As String)

Dim Columns,Rows As Integer

Dim i,j As Integer

Columns = Records.Fields.Count

Rows = Records.RecordCount

如果Records.EOF = False且Records.BOF = False那么

对于i = 0到行 - 1

对于j = 0到列 - 1个

表格(SheetName)。细胞(i + 2,j + 1)。选择

表格(SheetName) .Cells(i + 2,j + 1)= Records.Fields.Item(j).Value

Next

Records.MoveNext

Next

End If

Sheets(SheetName).Cells(1,1)。选择

MsgBox“输出!”,vb OKOnly ,“MySql to Excel”

End Function

"把Excel写入MySql中的数据库

函数InsertToMySql(ByVal SheetName As String,ByVal TblName As String)

Dim SqlStr As String

Dim i,j As Integer

Dim Columns,Rows As Integer

Columns = VBAProject.func_public.GetTotalColumns(SheetName)

Rows = VBAProject.func_public.GetTotalRows(SheetName)

Set Records = CreateObject(“ADODB.recordset”)

"取得结果集并插入数据到数据库

Set Records = CreateObject(“ADODB.Recordset”)

"以下语句提供了插入思路,我只是把单条记录的插入方式改为循环,以把所有的记录添加到表中

"rs。打开“插入新表值("”&ActiveSheet.Cells(i,1).Value&“",”&“"”&ActiveSheet.Cells(i,2 ).Value&“")”,cnn,0

For i = 2 To

Sows SqlStr =“INSERT INTO”&TblName&“values("”&Sheets(SheetName).Cells(i,1).Value&“"” "注意:"values("“,字母”v“之前是有空格的!!!

for j = 2 To Columns

SqlStr = SqlStr&”,"“&Sheets(SheetName).Cells(i,j).Value& “"”

Next

SqlStr = SqlStr&“)”

Set Records = Cnn.Execute(SqlStr)"rs。打开SqlStr,cnn,0不能用这条语句实现!!!

下一个

MsgBox“插入!”,vbOKOnly,“Excel To MySql”

End Function

"

清除对象功能ClearObj()

设置Cnn = Nothing

Set Records = Nothing

End Function

"获得数据表的字段名称

"OpenSchema可以获得数据库的各种信息

函数InputColumns(ByVal SheetName As String)

CnnOpen“localhost”,“mydb”,“employees”,“root”,“”

Set Records = Cnn.OpenSchema( adSchemaColumns)

Dim i As Integer

i = 1

而Not Records.EOF

Sheets(SheetName).Cells(1,i)= Records!COLUMN_NAME

i = i + 1

Records.MoveNext

Wend

CnnClose

ClearObj

结束函数

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/203369
推荐阅读
相关标签
  

闽ICP备14008679号