Export data from Excel to MS SQL

Forestq

Active Member
Joined
May 9, 2010
Messages
482
Hi,

I have data as:
idnamevalue
1A11
2A222
3A333
..........

<tbody>
</tbody>


How to export the data (Range:A1:C&i) to MS SQL server, to table called "test"?


I know how to get/select the data from MS SQL: TAB DATA/Connections/Workbook Connections/Add... then just add connection string & into command line put: "select * from..."
Then into TAB DATA, from Existing Connection menu, connect to the data, and put range/table/pivot_table, where data should be save.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
if someone is interested:

Code:
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Dim conn As ADODB.Connection
Dim rcrds As ADODB.Recordset
Dim iRowNo, i As Long
Dim id_insert, name_insert, value_insert As String

Dim wb As Workbook
Dim ws As Worksheet

Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")

   Dim login, passwd, host, dbname As String
   
   login = ""
   passwd = ""
   host = ""
   dbname = ""
  
   Set conn = New ADODB.Connection
    
    With conn
        .ConnectionString = "driver={sql server};Server=" + host + _
                            ";Database=" + dbname + _
                            ";User ID=" + login + _
                            ";Password=" + passwd + _
                            ";Trusted_Connection=False;"
        .Open
    End With 

    iRowNo = 2    
    
        Do Until ws.Cells(iRowNo, 1) = ""
        
                id_insert = ws.Cells(iRowNo, 1)
                name_insert = ws.Cells(iRowNo, 2)
                value_insert = ws.Cells(iRowNo, 3)
                
                conn.Execute "insert into table_name(id, name, value)  values ('" & id_insert& "', '" & name_insert& "', '" & value_insert& "')"
                
                iRowNo = iRowNo + 1
                
        Loop   

   rcrds.Close
   conn.Close
   Set rcrds = Nothing

Application.DisplayAlerts = True
Application.ScreenUpdating = True
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,417
Messages
6,124,777
Members
449,187
Latest member
hermansoa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top