SQL.CommandText和ADODB更新

时间:2017-11-04 19:05:58

标签: sql-server vba

丹,代码有效!我想在代码中手动编写我的条目  让他们进来。谢谢你的帮助!

Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

Dim Name As String
Dim Name1 As String
Name = "test1"
Name1 = "test2"

Server_Name = "."
Database_Name = "MyDatabase"

Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & ";"

Dim SQL As ADODB.Command
Set SQL = New ADODB.Command

SQL.CommandText = "UPDATE [MyDatabase].[dbo].[Test] SET [name] =(?) WHERE [id] = 1;"
SQL.CommandText = SQL.CommandText + "UPDATE [Mydatabase].[dbo].[Test] SET [name] =(?) WHERE [id] = 2;"


SQL.Parameters.Append SQL.CreateParameter("name", adVarChar, adParamInput, 50, Name)
SQL.Parameters.Append SQL.CreateParameter("name1", adVarChar, adParamInput, 50, Name1)

SQL.ActiveConnection = Cn
SQL.Execute

Cn.Close
Set Cn = Nothing

End Sub

3 个答案:

答案 0 :(得分:1)

您需要添加3个参数,每个值一个。尝试:

SQL.Parameters.Append SQL.CreateParameter("Value1", adVarChar, adParamInput, 50, Value1)
SQL.Parameters.Append SQL.CreateParameter("Value2", adVarChar, adParamInput, 50, Value2)
SQL.Parameters.Append SQL.CreateParameter("Value3", adVarChar, adParamInput, 50, Value3)

此外,您的代码未正确连接单独的UPDATE语句。这也是包含语句终止符的版本:

SQL.CommandText = "UPDATE [dbMyDatabase].[dbo].[tbl_ExcelTest] SET [Value] =(?) WHERE [id] = 1;"
SQL.CommandText = SQL.CommandText + "UPDATE [dbMyDatabase].[dbo].[tbl_ExcelTest] SET [Value] =(?) WHERE [id] = 2;"
SQL.CommandText = SQL.CommandText + "UPDATE [dbMyDatabase].[dbo].[tbl_ExcelTest] SET [Value] =(?) WHERE [id] = 3;"

修改

这是我用来创建数据库,表和测试数据的T-SQL脚本:

CREATE DATABASE MyDatabase;
GO
USE MyDatabase;
GO
CREATE TABLE dbo.Test(
     id int NOT NULL
        CONSTRAINT PK_Test PRIMARY KEY
    ,name varchar(100)
    );
INSERT INTO dbo.Test VALUES(1,''),(2,''),(3,'');
GO

以下是我测试过的问题的修订代码。我做的唯一更改是第二个更新语句中的服务器名称和表名称,我从name更改为Test。我没有adParamInputadVarchar常数。它们包含在ActiveX对象引用中,因此代码可以使用或不使用。

Private Sub CommandButton1_Click()

    Dim Cn As ADODB.Connection
    Dim Server_Name As String
    Dim Database_Name As String
    Dim SQLStr As String
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset

    Dim Name As String
    Dim Name1 As String
    Name = "test1"
    Name1 = "test2"

    Server_Name = "."
    Database_Name = "MyDatabase"

    Set Cn = New ADODB.Connection
    Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & ";"

    Dim SQL As ADODB.Command
    Set SQL = New ADODB.Command

    SQL.CommandText = "UPDATE [MyDatabase].[dbo].[Test] SET [name] =(?) WHERE [id] = 1;"
    SQL.CommandText = SQL.CommandText + "UPDATE [Mydatabase].[dbo].[Test] SET [name] =(?) WHERE [id] = 2;"


    SQL.Parameters.Append SQL.CreateParameter("name", adVarChar, adParamInput, 50, Name)
    SQL.Parameters.Append SQL.CreateParameter("name1", adVarChar, adParamInput, 50, Name1)

    SQL.ActiveConnection = Cn
    SQL.Execute

    Cn.Close
    Set Cn = Nothing

End Sub

答案 1 :(得分:1)

像这样(未经测试)

Sub DoUpdate(id, newValue)

    Set Cn = New ADODB.Connection
    Cn.Open "Driver={SQL Server};Server=" & Server_Name & _
             ";Database=" & Database_Name & ";"

    Dim SQL As ADODB.Command
    Set SQL = New ADODB.Command

    SQL.CommandText = "UPDATE [dbMyDatabase].[dbo].[tbl_ExcelTest] SET" _
                      " [Value] = ? WHERE [id] = ?"


    SQL.Parameters.Append SQL.CreateParameter("Value", adVarChar, _
                                          adParamInput, 50, newValue)
    SQL.Parameters.Append SQL.CreateParameter("Id", adVarNumeric, _
                                          adParamInput, 50, id)

    SQL.ActiveConnection = Cn
    SQL.Execute

    Cn.Close
    Set Cn = Nothing

End Sub

然后打电话给:

DoUpdate 1, 20
DoUpdate 2, 30
DoUpdate 3, 88

答案 2 :(得分:0)

当您使用

指示时,将执行sql命令文本
 SQL.Execute

之前您所做的只是在执行命令文本之前三次更改命令文本。

你需要执行每一个

 SQL.CommandText = "UPDATE [dbMyDatabase].[dbo].[tbl_ExcelTest] SET [Value] =(?) WHERE [id] = 1"
  SQL.Execute
 SQL.CommandText = "UPDATE [dbMyDatabase].[dbo].[tbl_ExcelTest] SET [Value] =(?) WHERE [id] = 2"
sql.Execute
SQL.CommandText = "UPDATE [dbMyDatabase].[dbo].[tbl_ExcelTest] SET [Value] =(?) WHERE [id] = 3"
sql.execute