VBA - 是否有用于VBA与SQL Server交互的库

Vit*_*ata 0 sql sql-server vba excel-vba access-vba

你好幸福友善的人!我的问题有点笼统,无论是否存在这样的问题.我正在编写一个与SQL Server交互的Excel应用程序.这是我到目前为止所写的,只是为了模仿命令INSERT - str_generate_order我必须为每个命令编写类似的东西.


我的问题是 - 是否有一个库,已经过验证和测试,使这更容易?我真的不想重新发明热水并进行测试.我是在谈论str_generate_order我的代码中的函数.代码工作得很好,只需要指定自己的代码str_connection_string即可将数据连接到数据库.很多,我需要像一个Python库(我不记得的名字),你只需要给出列和命令的名称,然后为你完成剩下的工作......或者我应该自己构建它?

Option Explicit

Public Sub GenerateDataIntoTable()

    Dim str_table_name      As String: str_table_name = "Main"
    Dim arr_column_names    As Variant
    Dim arr_values          As Variant

    ReDim arr_column_names(6)
    ReDim arr_values(6)

    arr_column_names(0) = "UserName"
    arr_column_names(1) = "CurrentDate"
    arr_column_names(2) = "CurrentTime"
    arr_column_names(3) = "CurrentLocation"
    arr_column_names(4) = "Status1"
    arr_column_names(5) = "Status2"
    arr_column_names(6) = "Status3"

    arr_values(0) = Environ("username")
    arr_values(1) = Date
    arr_values(2) = Time
    arr_values(3) = Application.ActiveWorkbook.FullName
    arr_values(4) = make_random(2, 6)
    arr_values(5) = arr_values(4) + make_random(2, 6)
    arr_values(6) = arr_values(5) - make_random(2, 6)

    Debug.Print b_insert_into_table(str_table_name, arr_column_names, arr_values)

End Sub

Function b_insert_into_table(str_table_name As String, arr_column_names As Variant, arr_values As Variant) As Boolean

    Dim conn            As Object
    Dim str_order       As String

    Set conn = CreateObject("ADODB.Connection")
    conn.Open str_connection_string

    str_order = "insert into dbo." & str_table_name
    str_order = str_order & str_generate_order(arr_column_names, arr_values)
    conn.Execute str_order
    conn.Close
    Set conn = Nothing

    b_insert_into_table = True

End Function

Public Function str_generate_order(arr_column_names As Variant, arr_values As Variant) As String

    Dim l_counter       As Long
    Dim str_result      As String

    Dim str_left        As String: str_left = "('"
    Dim str_midd        As String: str_midd = "','"
    Dim str_right       As String: str_right = "')"

    str_result = "("
    For l_counter = LBound(arr_column_names) To UBound(arr_column_names)
        str_result = str_result & arr_column_names(l_counter) & ","
    Next l_counter

    str_result = Left(str_result, Len(str_result) - 1)
    str_result = str_result & ")"
    str_result = str_result & "values"

    str_result = str_result & str_left
    For l_counter = LBound(arr_values) To UBound(arr_values)
        str_result = str_result & arr_values(l_counter)

        If l_counter < UBound(arr_values) Then
            str_result = str_result & str_midd
        Else
            str_result = str_result & str_right
        End If

    Next l_counter

    str_generate_order = str_result

End Function
Run Code Online (Sandbox Code Playgroud)

Mat*_*don 5

请参阅实现任何ADODB查询,动态创建ADODB参数,如果要推送它,请参阅YARPI:又一个存储库模式实现UnitOfWork,这是多个存储库的借口,所有这些都在Code Review Stack Exchange上 - 我已经使用过SqlCommand自从我写这篇文章以来已经三年了,它还没有让我失望.

我也把它放在GitHub上

哦,它是用编写的,用于与进行交互,但它也可以在中与进行交互- 只需给它一个提供者/连接字符串,如果ADODB可以处理它,它应该"正常工作".

用法

使用一次性连接选择单个值:

Dim result As Variant
SqlCommand.ConnectionString = "connection string"
result = SqlCommand.SelectSingleValue("SELECT SomeField FROM SomeTable WHERE SomeValue = ?", 123)
Run Code Online (Sandbox Code Playgroud)

选择具有实时连接的结果集(以支持事务):

Dim cmd As New SqlCommand
Dim result As ADODB.Recordset
Dim conn As New ADODB.Connection
conn.ConnectionString = "connection string"
conn.Open
Set result = cmd.Execute(conn, "SELECT * FROM SomeTable WHERE SomeField = ?", 123)
'use result
result.Close
conn.Close
Run Code Online (Sandbox Code Playgroud)

执行参数化SQL并传递连接(以支持事务):

Dim cmd As New SqlCommand
Dim conn As New ADODB.Connection
Dim result As Boolean
conn.ConnectionString = "connection string"
conn.Open
result = cmd.ExecuteNonQuery(conn, "UPDATE SomeTable SET SomeField = ? WHERE SomeValue = ?", 123, "abc")
conn.Close
Run Code Online (Sandbox Code Playgroud)

SqlCommand类确定ADODB参数来生成,生成它们,与实际的参数ADODB命令一起.

  • 哇我把它放在我的默认加载项中.非常好的工作! (2认同)