将数据上传到SQLServer

时间:2015-06-08 20:52:02

标签: excel vba excel-vba adodb

我正在制作一个宏来将数据上传到SQLServer,我想要这个表以及每次运行宏

  1. 想要使用Recordset添加最后添加的行
  2.    Sub GetData()
           Call RecordsetFromSheet(prueba)
       End Sub
    
    Public Function RecordsetFromSheet(ByVal tableName As String)
    Dim cn As Object
    Dim rs As Object
    Dim strCon As String
    Dim strSQL, strInput As String
    Dim cmd As Object
    
      

    打开与SQL Server的连接

    Set cn = CreateObject("ADODB.Connection")
    cn.ConnectionString = OleDbConnectionString("xxxxx", "xxxx", "xx", "xxxxxxx")
    cn.Open
    
      

    关闭连接

    If CBool(cn.State And adStateOpen) = True Then
        cn.Close
        Set cn = Nothing
    End If
    End Function
    
      

    函数到StringConnection

    Function OleDbConnectionString(ByVal Server As String, ByVal Database As String, _
        ByVal UserName As String, ByVal Password As String) As String
        If UserName = "" Then
            OleDbConnectionString = "Provider=SQLOLEDB.1;Data Source=" & Server _
                & ";Initial Catalog=" & Database _
                & ";Integrated Security=SSPI;Persist Security Info=False;"
        Else
            OleDbConnectionString = "Provider=SQLOLEDB.1;Data Source=" & Server _
                & ";Initial Catalog=" & Database _
                & ";User ID=" & UserName & ";Password=" & Password & ";"
        End If
    End Function
    

0 个答案:

没有答案