当前位置: 首页 > 面试题库 >

如何直接从Excel更新Sql表?

澹台冯浩
2023-03-14
问题内容

我有一个sql数据库,并且能够连接excel电子表格。但是,当我直接从excel更新表时,它并没有更新数据库,并且当我单击“刷新”时,所有输入的数据都不再在excel表中

是否可以在不使用任何查询的情况下从excel更新sql数据库?


问题答案:

有很多方法可以做到这一点。我建议使用类似的方法将数据从Excel推送到SQL Server。

Sub 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 "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

您也可以尝试使用Where子句。

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 = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist 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 object
Set cmd = New adodb.Command

'Open the Connection to the database
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-22' WHERE EMPID = 2"

'Pass the SQL to the Command object
cmd.CommandText = strSQL


'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


 类似资料:
  • 问题内容: 在 SQL Server中 ,可以使用以下语句将行插入表中: 是否还可以使用 更新 表?我有一个包含这些值的临时表,并想使用这些值更新另一个表。也许是这样的: 问题答案:

  • 问题内容: 我有一个表的结构为 现在,我需要执行一个查询, **表中已经 存在 if的 更新 ,如果不是,则在表3中插入记录作为新记录。 这就是我现在在代码中得到的 问题答案: 您可以使用SQL Server中的语法执行“ upsert”操作: 否则,您可以使用类似以下内容的方法:

  • 问题内容: 我需要针对将返回零或一的数据库运行查询(检查是否存在特定条件)。我已获得审查的技术规范指出,我应该创建一个存储过程,该存储过程将返回单行,并带有称为“结果”的单列,该列将包含0或1的位值。我不确定存储过程将是最好的方法,但是不确定一点,所以我想请教您一些意见。我可以想到的两个选择是: 1:创建一个SQL标量值函数,该函数执行查询并返回一点。然后,可以使用“ TEXT” SqlComma

  • 问题内容: 嗨,我已经发送到GUI的线程工作程序链接,并直接从QThread更新GUI信息。像这儿: 在主要我只是添加 这样的解决方案在PyQt5中非常不好吗?我是PyQt的新手。谢谢。如果我的解决方案不好,请帮助我修复它。 问题答案: 您不能也不能从辅助线程更新GUI,必须通过信号和插槽来完成: 为此,我们将通过class创建一个信号,并将指示参数的类型,然后将其连接到所需的插槽,如下所示: 线

  • 我需要将priorityId设置为以下。有人能建议一个sql更新语句吗?我想我需要一个由Rid和Gid组成的组或自加入 非常感谢。

  • 我有一个应用程序,我曾经托管在一个私人服务器上,并通过使用GCM发送到新版本的链接。我现在在Play Store上托管应用程序。当我有一个更新的版本时,我会增加版本代码并将其发布到生产中。有人能告诉我为什么手机没有收到更新通知吗?我已经选中了应用程序中的通知复选框,并且在play store应用程序设置中选中了自动更新。 马特