通过 VBA 将 Excel 范围推到 SQL Table
Push Excel Range to SQL Table via VBA
每次同事执行 VBA 宏时,我都需要将 Excel 中的范围推到 SQL table 中的新行。到目前为止,我已经在 Excel 中将数据分成单行和多列(总共 110 个数据单元格)。我现在的问题是如何将 Excel sheet 中的每个单独的单元格插入相应的列和第一个 empty 行 SQL table。我在互联网上做了一些相当广泛的搜索,但没有发现任何与我正在尝试做的事情相差甚远的东西。
是否有正确的程序允许我将 110 列的行转储到 SQL table 中的第一个空行?
我写了 table 并且我设置了范围:
Set DataBaseData = ActiveSheet.Range("DO1:HT1")
除此之外,我不知道以何种方式打开与服务器、数据库和 Table 的连接。这是我到目前为止所拥有的:
Sub Connection()
Dim Conn As ADODB.Connection
Dim Command As ADODB.Command
Set Conn = New ADODB.Connection
Set Command = New ADODB.Command
Dim i As Integer
Dim columnnumber As Integer
i = 0
Conn.ConnectionString = "Provider=SQLOLEDB; Data Source=[Nope];Initial Catalog=[NopeNope];User ID=[NopeNopeNope];Password=[AbsolutelyNot]; Trusted_Connection=no;"
Conn.Open
Command.ActiveConnection = Conn
End Sub
如有任何帮助,我们将不胜感激。
如果您对我正在尝试做的事情感到好奇:我正在将一系列数据从 CMM 推送到数据库,这样我就可以将数据存储所需的时间,然后调用它数据返回到 PowerBI 和 Minitab。
我能够使用以下方法成功地将 Excel 中的整行写入 SQL 数据库:
Sub Connection()
Const Tbl As String = "NEIN"
Dim InsertQuery As String, xlRow As Long, xlCol As Integer
Dim DBconnection As Object
Set DBconnection = CreateObject("ADODB.Connection")
DBconnection.Open "Provider=SQLOLEDB.1;Password=NEIN" & _
";Persist Security Info=false;User ID=NEIN" & _
";Initial Catalog=NEIN;Data Source=NEIN"
InsertQuery = ""
xlRow = 1 'only one row being used *as of now*, and that is the top row in the excel sheet
xlCol = 119 'First column of data
While Cells(xlRow, xlCol) <> ""
InsertQuery = InsertQuery & "INSERT INTO " & Tbl & " VALUES('"
For xlCol = 119 To 229 'columns DO1 to HT1
InsertQuery = InsertQuery & Replace(Cells(xlRow, xlCol), "'", "''") & "', '" 'Includes mitigation for apostrophes in the data
Next xlCol
InsertQuery = InsertQuery & Format(Now(), "M/D/YYYY") & "')" & vbCrLf 'The last column is a date stamp, either way, don't forget to close that parenthesis
Wend
DBconnection.Execute InsertQuery
DBconnection.Close
Set DBconnection = Nothing
End Sub
每次同事执行 VBA 宏时,我都需要将 Excel 中的范围推到 SQL table 中的新行。到目前为止,我已经在 Excel 中将数据分成单行和多列(总共 110 个数据单元格)。我现在的问题是如何将 Excel sheet 中的每个单独的单元格插入相应的列和第一个 empty 行 SQL table。我在互联网上做了一些相当广泛的搜索,但没有发现任何与我正在尝试做的事情相差甚远的东西。
是否有正确的程序允许我将 110 列的行转储到 SQL table 中的第一个空行?
我写了 table 并且我设置了范围:
Set DataBaseData = ActiveSheet.Range("DO1:HT1")
除此之外,我不知道以何种方式打开与服务器、数据库和 Table 的连接。这是我到目前为止所拥有的:
Sub Connection()
Dim Conn As ADODB.Connection
Dim Command As ADODB.Command
Set Conn = New ADODB.Connection
Set Command = New ADODB.Command
Dim i As Integer
Dim columnnumber As Integer
i = 0
Conn.ConnectionString = "Provider=SQLOLEDB; Data Source=[Nope];Initial Catalog=[NopeNope];User ID=[NopeNopeNope];Password=[AbsolutelyNot]; Trusted_Connection=no;"
Conn.Open
Command.ActiveConnection = Conn
End Sub
如有任何帮助,我们将不胜感激。
如果您对我正在尝试做的事情感到好奇:我正在将一系列数据从 CMM 推送到数据库,这样我就可以将数据存储所需的时间,然后调用它数据返回到 PowerBI 和 Minitab。
我能够使用以下方法成功地将 Excel 中的整行写入 SQL 数据库:
Sub Connection()
Const Tbl As String = "NEIN"
Dim InsertQuery As String, xlRow As Long, xlCol As Integer
Dim DBconnection As Object
Set DBconnection = CreateObject("ADODB.Connection")
DBconnection.Open "Provider=SQLOLEDB.1;Password=NEIN" & _
";Persist Security Info=false;User ID=NEIN" & _
";Initial Catalog=NEIN;Data Source=NEIN"
InsertQuery = ""
xlRow = 1 'only one row being used *as of now*, and that is the top row in the excel sheet
xlCol = 119 'First column of data
While Cells(xlRow, xlCol) <> ""
InsertQuery = InsertQuery & "INSERT INTO " & Tbl & " VALUES('"
For xlCol = 119 To 229 'columns DO1 to HT1
InsertQuery = InsertQuery & Replace(Cells(xlRow, xlCol), "'", "''") & "', '" 'Includes mitigation for apostrophes in the data
Next xlCol
InsertQuery = InsertQuery & Format(Now(), "M/D/YYYY") & "')" & vbCrLf 'The last column is a date stamp, either way, don't forget to close that parenthesis
Wend
DBconnection.Execute InsertQuery
DBconnection.Close
Set DBconnection = Nothing
End Sub