我有一个sql并且可以连接数据库excel电子表格。但是,当我直接从excel更新表时,它没有更新数据库,当我单击刷新时,所有输入的数据都不再存在excel表中 9 l( M% E9 F. I+ x) {- r. s6 b是否可以在不使用任何查询的情况下从excel更新sql数据库? 8 X6 S; V9 m5 B% _ 7 K8 G, l+ U0 P2 C; k* F 解决方案: 1 C4 Z, h2 ]0 b3 p9 s
这样做的方法有很多。我建议使用类似的方法从数据中获取数据Excel推送到SQL Server。 1 N( I/ s- y* h9 o* [( S& n0 ASub ButtonClick()'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 "rovider=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 = 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 Loop MsgBox "Done importing",vbInformation con.Close Set con = Nothing End WithExit SuberrH: MsgBox Err.DescriptionEnd Sub也可以尝试使用Where子句。+ E' ^! K0 d, Z! e: ?! C0 s6 W
Sub InsertInto()'Declare some variablesDim cnn As adodb.ConnectionDim cmd As adodb.CommandDim strSQL As String'Create a new Connection objectSet cnn = New adodb.Connection'Set the connection stringcnn.ConnectionString = "rovider=SQLOLEDB.1;Integrated Security=SSPIersist Security Info=True;Initial Catalog=Northwind;Data Source=Excel-PC\SQLEXPRESS"'cnn.ConnectionString = "DRIVER=SQL Server;SERVER=Excel-PC\SQLEXPRESS;DATABASE=Northwind;Trusted_Connection=Yes"'Create a new Command objectSet cmd = New adodb.Command'Open the Connection to the databasecnn.Open'Associate the command with the connectioncmd.ActiveConnection = cnn'Tell the Command we are giving it a bit of SQL to run,not a stored procedurecmd.CommandType = adCmdText'Create the SQLstrSQL = "UPDATE TBL SET JOIN_DT = '2013-01-22' WHERE EMPID = 2"'Pass the SQL to the Command objectcmd.CommandText = strSQL'Execute the bit of SQL to update the databasecmd.Execute'Close the connection againcnn.Close'Remove the objectsSet cmd = NothingSet cnn = NothingEnd Sub