Sql server 我需要使用VBA将数据从Excel插入SQL server
我需要将Sql server 我需要使用VBA将数据从Excel插入SQL server,sql-server,vba,excel,Sql Server,Vba,Excel,我需要将test vba.xlsx数据插入到SQL server中的特定数据库中 Sub insertion() Dim conn As ADODB.Connection Dim rs As ADODB.Recordset Dim sConnString As String Dim rsstring As String Dim m, nrows As Integer Set wkb = Workbooks("test-vba.xlsx").Wor
test vba.xlsx
数据插入到SQL server中的特定数据库中
Sub insertion()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim rsstring As String
Dim m, nrows As Integer
Set wkb = Workbooks("test-vba.xlsx").Worksheets("Sheet1").Activate
sConnString = "Provider=SQLOLEDB;Data Source=PRATEEP-PC\SQLEXPRESS;" & _
"Initial Catalog=PPDS_07Dec_V1_Decomposition;" & _
"Integrated Security=SSPI;"
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.Open sConnString
For m = 0 To nrows - 1
这里有一些想法
Sub UpdateTable()
Dim cnn As Object
Dim wbkOpen As Workbook
Dim objfl As Variant
Dim rngName As Range
Workbooks.Open "C:\Users\Excel\Desktop\Excel_to_SQL_Server.xls"
Set wbkOpen = ActiveWorkbook
Sheets("Sheet1").Select
Set rngName = Range(Range("A1"), Range("A1").End(xlToLeft).End(xlDown))
rngName.Name = "TempRange"
strFileName = wbkOpen.FullName
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFileName & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes"";"
nSQL = "INSERT INTO [odbc;Driver={SQL Server};Server=Server_Name;Database=[Database_Name].[dbo].[TBL]]"
nJOIN = " SELECT * from [TempRange]"
cnn.Execute nSQL & nJOIN
MsgBox "Uploaded Successfully"
wbkOpen.Close
Set wbkOpen = Nothing
End Sub
Sub InsertInto()
'Declare some variables
Dim cnn As adodb.Connection
Dim cmd As adodb.Command
Dim strSQL As String
'Create a new Connection object
Set cnn = New adodb.Connection
'Set the connection string
cnn.ConnectionString = "Server_Name;Database=Database_Name;Trusted_Connection=True;"
'Create a new Command object
Set cmd = New adodb.Command
'Open the connection
cnn.Open
'Associate the command with the connection
cmd.ActiveConnection = cnn
'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
cmd.CommandType = adCmdText
'Create the SQL
strSQL = "UPDATE TBL SET JOIN_DT = 2013-01-13 WHERE EMPID = 2"
'Pass the SQL to the Command object
cmd.CommandText = strSQL
'Open the Connection to the database
cnn.Open
'Execute the bit of SQL to update the database
cmd.Execute
'Close the connection again
cnn.Close
'Remove the objects
Set cmd = Nothing
Set cnn = Nothing
End Sub
还有一个想法
Sub Button_Click()
'TRUSTED CONNECTION
On Error GoTo errH
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strPath As String
Dim intImportRow As Integer
Dim strFirstName, strLastName As String
Dim server, username, password, table, database As String
With Sheets("Sheet1")
server = .TextBox1.Text
table = .TextBox4.Text
database = .TextBox5.Text
If con.State <> 1 Then
con.Open "Provider=SQLOLEDB;Data Source=" & server & ";Initial Catalog=" & database & ";Integrated Security=SSPI;"
'con.Open
End If
'this is the TRUSTED connection string
Set rs.ActiveConnection = con
'delete all records first if checkbox checked
If .CheckBox1 Then
con.Execute "delete from tbl_demo"
End If
'set first row with records to import
'you could also just loop thru a range if you want.
intImportRow = 10
Do Until .Cells(intImportRow, 1) = ""
strFirstName = .Cells(intImportRow, 1)
strLastName = .Cells(intImportRow, 2)
'insert row into database
con.Execute "insert into tbl_demo (firstname, lastname) values ('" & strFirstName & "', '" & strLastName & "')"
intImportRow = intImportRow + 1
Loop
MsgBox "Done importing", vbInformation
con.Close
Set con = Nothing
End With
Exit Sub
errH:
MsgBox Err.Description
End Sub
子按钮\u单击()
'可信连接
关于错误转到错误
Dim con作为新的ADODB连接
将rs设置为新ADODB.Recordset
将strPath设置为字符串
Dim intImportRow作为整数
Dim strFirstName,strLastName作为字符串
Dim服务器、用户名、密码、表、数据库作为字符串
附页(“第1页”)
服务器=.TextBox1.Text
表=.TextBox4.Text
数据库=.TextBox5.Text
如果符合第1条,则
con.Open“Provider=SQLOLEDB;Data Source=“&server&”Initial Catalog=“&database&”Integrated Security=SSPI;”
“开
如果结束
'这是受信任的连接字符串
设置rs.ActiveConnection=con
'如果选中复选框,则首先删除所有记录
如果是,则选中方框1
con.执行“从tbl_演示中删除”
如果结束
'设置要导入的记录的第一行
如果你愿意,你也可以在一个范围内循环。
intImportRow=10
直到.Cells(intImportRow,1)=“”
strFirstName=.Cells(intImportRow,1)
strLastName=.Cells(intImportRow,2)
'将行插入数据库
con.Execute“insert into tbl_demo(firstname,lastname)值(“&strFirstName&“,”&strLastName&“)”
intImportRow=intImportRow+1
环
MsgBox“完成导入”,vbInformation
结案
设置con=Nothing
以
出口接头
呃:
MsgBox错误说明
端接头
此外,请查看下面的链接
此代码是否有效?有没有具体的问题?如果是,请描述问题(什么、哪里、如何)以及代码应该做什么。请阅读。你连一个问题都没问。描述好你的问题,什么已经起作用,什么不起作用,你期望什么。这里还有代码。您可以轻松地调整代码以满足您的需要。如果只想导入该Excel文件一次,则以下解决方案可能更容易/更快: