国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
VBA對(duì)MySql數(shù)據(jù)庫(kù)進(jìn)行讀取和寫(xiě)入操作_MySQL_開(kāi)發(fā)學(xué)院
VBA對(duì)MySql數(shù)據(jù)庫(kù)進(jìn)行讀取和寫(xiě)入操作時(shí)間:2009-10-06 09:18:47來(lái)源:網(wǎng)絡(luò) 作者:未知 點(diǎn)擊:256次
'以下代碼用于32位系統(tǒng),Office 2003,環(huán)境,MySql版本5.1
'在使用前需要先安裝MySql的驅(qū)動(dòng),進(jìn)行正確配置
'注意:必須給出正確的服務(wù)器名、數(shù)據(jù)庫(kù)名、表名、數(shù)據(jù)庫(kù)連接的用戶名、密碼
Option Explicit
Dim Cnn As ADODB.Con
'以下代碼用于32位系統(tǒng),Office 2003,環(huán)境,MySql版本5.1
'在使用前需要先安裝MySql的驅(qū)動(dòng),進(jìn)行正確配置
'注意:必須給出正確的服務(wù)器名、數(shù)據(jù)庫(kù)名、表名、數(shù)據(jù)庫(kù)連接的用戶名、密碼
Option Explicit
Dim Cnn As ADODB.Connection '定義ADO連接對(duì)象
Dim Records As ADODB.Recordset '定義ADO記錄集對(duì)象
'連接到數(shù)據(jù)庫(kù)
Function CnnOpen(ByVal ServerName As String, ByVal DBName As String, ByVal TblName As String, ByVal User As String, ByVal PWD As String) '服務(wù)器名或IP、數(shù)據(jù)庫(kù)名、登錄用戶、密碼
Dim CnnStr As String '定義連接字符串
Set Cnn = CreateObject("ADODB.Connection") '創(chuàng)建ADO連接對(duì)象
Cnn.CommandTimeout = 15 '設(shè)置超時(shí)時(shí)間
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
'關(guān)閉連接
Function CnnClose()
If Cnn.State = 1 Then
Cnn.Close
End If
End Function
'取得記錄集
Function GetRecordset(ByVal SqlStr As String)
Set Records = CreateObject("ADODB.recordset")
Records.CursorType = adOpenStatic '設(shè)置游標(biāo)類型,否則無(wú)法獲得行數(shù)
Records.CursorLocation = adUseClient '設(shè)置游標(biāo)屬性,否則無(wú)法獲得行數(shù)
'對(duì)于Connection對(duì)象的Execute方法產(chǎn)生的記錄集對(duì)象,一般是一個(gè)只讀并且只向前的記錄集
'如果需要對(duì)記錄集進(jìn)行操作,譬如修改和增加,則需要用一個(gè)Recordset對(duì)象
'并正確設(shè)置好CursorType和LockType為適當(dāng)類型,然后調(diào)用Open方法打開(kāi)
Records.Open SqlStr, Cnn '使用這個(gè)語(yǔ)句,行數(shù)將返回-1,Set Records = Conn.Execute(SqlStr)
End Function
'寫(xiě)入Excel表
Function InputSheet(ByVal SheetName As String)
Dim Columns, Rows As Integer
Dim i, j As Integer
Columns = Records.Fields.Count
Rows = Records.RecordCount
If Records.EOF = False And Records.BOF = False Then
    For i = 0 To Rows - 1
        For j = 0 To Columns - 1
            Sheets(SheetName).Cells(i + 2, j + 1).Select
            Sheets(SheetName).Cells(i + 2, j + 1) = Records.Fields.Item(j).Value
        Next
    Records.MoveNext
    Next
End If
Sheets(SheetName).Cells(1, 1).Select
MsgBox "Output!", vbOKOnly, "MySql to Excel"
End Function
'把Excel寫(xiě)入MySql中的數(shù)據(jù)庫(kù)
Function 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")
'取得結(jié)果集并插入數(shù)據(jù)到數(shù)據(jù)庫(kù)
Set Records = CreateObject("ADODB.Recordset")
'以下語(yǔ)句提供了插入思路,我只是把單條記錄的插入方式改為循環(huán),以把所有的記錄添加到表中
'rs.Open "insert   into   newtable  values('" & ActiveSheet.Cells(i, 1).Value & "'," & "'" & ActiveSheet.Cells(i, 2).Value & "')", cnn, 0
For i = 2 To Rows
    SqlStr = "INSERT INTO " & TblName & " values('" & Sheets(SheetName).Cells(i, 1).Value & "'" '注意:" values('",字母“v”之前是有空格的!?。?br>    For j = 2 To Columns
        SqlStr = SqlStr & ",'" & Sheets(SheetName).Cells(i, j).Value & "'"
    Next
    SqlStr = SqlStr & ")"
    Set Records = Cnn.Execute(SqlStr) 'rs.Open SqlStr, cnn, 0  不能用這條語(yǔ)句實(shí)現(xiàn)?。?!
Next
MsgBox "Insert!", vbOKOnly, "Excel To MySql"
End Function
'清除對(duì)象
Function ClearObj()
Set Cnn = Nothing
Set Records = Nothing
End Function
'獲得數(shù)據(jù)表的字段名稱
'OpenSchema可以獲得數(shù)據(jù)庫(kù)的各種信息
Function InputColumns(ByVal SheetName As String)
CnnOpen "localhost", "mydb", "employees", "root", ""
Set Records = Cnn.OpenSchema(adSchemaColumns)
Dim i As Integer
i = 1
While Not Records.EOF
   Sheets(SheetName).Cells(1, i) = Records!COLUMN_NAME
   i = i + 1
   Records.MoveNext
Wend
CnnClose
ClearObj
End Function
 
本篇文章來(lái)源于:開(kāi)發(fā)學(xué)院 http://edu.codepub.com   原文鏈接:http://edu.codepub.com/2009/1006/16171.php
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
VB打開(kāi)EXCEL的方法
excel 導(dǎo)入導(dǎo)出 vb.net版,網(wǎng)上很難找的哦
vba處理excel數(shù)據(jù)(學(xué)生成績(jī)自動(dòng)分班統(tǒng)計(jì))
Excel259個(gè)常用宏
誰(shuí)知道怎么調(diào)用EXCEL做報(bào)表?
VB 導(dǎo)出 Excel (導(dǎo)入指定格式的 Excel 文檔中)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服