|
楼主 |
发表于 2015-4-23 11:30
|
显示全部楼层
- Sub ado() 'by feiren228
- Dim arr, i&, j&, str$, mycol$
- Dim cnn As Object, rs As Object
- Dim CnStr As String, sql As String
- Set cnn = CreateObject("Adodb.Connection")
- Set rs = CreateObject("adodb.recordset")
- Dim FileNamw$, DbIp$, DbName$, DbUser$, DbPw$
- '以上5个字符串变量分别表示文件路径和文件名、数据库地址、数据库名、数据操作员用户名、操作员密码
- 'Filename = App.Path & "\'数据库名'"
- DbIp = "."
- DbName = "cell"
- DbUser = "cell"
- DbPw = "cell"
- 'CnStr = "PROVIDER=MSDataShape;Data PROVIDER=MSDASQL;uid=" & DbUser & ";pwd=" & DbPw & ";DRIVER=SQL Server;DATABASE=" & DbName & ";WSID=GQSOFT;SERVER=" & DbIp
- CnStr = "Provider=SQLOLEDB;Data Source=" & DbIp & ";DATABASE=" & DbName & ";UID=" & DbUser & ";pwd=" & DbPw
- cnn.Open CnStr
- arr = Sheet1.UsedRange
- For i = 2 To UBound(arr)
- str = "values ("
- mycol = "("
- For j = 1 To UBound(arr, 2)
- mycol = mycol & arr(1, j) & ","
- If Len(arr(i, j)) = 0 Then
- str = str & "null" & ","
- ' ElseIf IsNumeric(arr(i, j)) Then
- ' str = str & arr(i, j) & ","
- Else
- str = str & "'" & arr(i, j) & "'" & ","
- End If
- Next j
- mycol = Left(mycol, Len(mycol) - 1) & ") "
- str = Left(str, Len(str) - 1) & ")"
- sql = "insert into tf_farmer_draft " & mycol & str
- Debug.Print sql
- cnn.Execute sql
- Next i
- MsgBox "数据添加成功!"
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|