'GO' 附近使用 SqlCommand 的语法不正确

Incorrect syntax near 'GO' using SqlCommand

每次我执行您在下面找到的这段代码时都会出现此界面:

str1 = "USE [" + database + "];" + vbCrLf +
   "GO" + vbCrLf +
   " CREATE TABLE  [dbo].[ARMSet] ([Id] Int IDENTITY(1, 1) Not NULL,

    );" + vbCrLf +
              " GO"
Dim myCommand1 As SqlCommand = New SqlCommand(str1, myConn)

错误的描述是:

Incorrect syntax near 'GO'

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go?view=sql-server-ver15#remarks

您不能使用您的代码select任何数据库

必须在 SQL 连接函数中 select 编辑数据库。

而不是强制服务器解析您的 USE 语句,然后保留 command/connection 以回收发送 CREATE TABLE,而是将您的两个语句分开,因为, GO 不是 SQL 服务器命令。

您可以改为在 myConn 上调用 ChangeDatabase,然后再使用它:

   myConn.ChangeDatabase(database)
   str1 = "CREATE TABLE  [dbo].[ARMSet] (
               [Id] Int IDENTITY(1, 1) Not NULL,
               [Name] nvarchar(max)  Not NULL,
               [ArmNumber] Int  Not NULL,
               [IP] nvarchar(max)  Not NULL,
               [Port] Int  Not NULL,
               [Modbusaddress] Int  Not NULL,
               [Type] Int  Not NULL,
               [IsMaster] bit  Not NULL,
               [DeliveryType] Int  Not NULL,
               [CurretPosition] Int  Not NULL,
               [IsSwingArm] bit  Not NULL,
               [IsLoadScale] bit  Not NULL,
               [PresetId] Int  Not NULL
           );"
   Dim myCommand1 As SqlCommand = New SqlCommand(str1, myConn)