解析查询时出错。怎么解决?

问题描述 投票:0回答:3

我想运行我首先从qazxsw poi文件中读取的sql语句。

我收到了这个错误:.sql

{"There was an error parsing the query. [ Token line number = 13,Token line offset = 1,Token in error = ALTER ]"}文件中有我的sql语句:

.sql

阅读文件:

CREATE TABLE [Test]
(
   [Id] INT NOT NULL IDENTITY (1,1),
   [DatabaseVersion] NVARCHAR(20) NOT NULL,
   [Autorun] BIT,
   [CurrentCulture] NVARCHAR(10),
   [MailNotificationEnabled] BIT,
   [RefreshInterval] INT,
   [ModifiedDate] DATETIME NOT NULL,
   [schemat] NVARCHAR(255)
)

ALTER TABLE [Test] ADD CONSTRAINT [PK_Test] PRIMARY KEY ([Id])

UPDATE [AppConfig]
SET [DatabaseVersion] = '0.12'

string oldVersion = GetOldDatabaseVersion(); string sqlScript = ""; sqlScript = GetScriptFromAssembly(oldVersion, ConfigurationSettings.ValidDatabaseVersion); ExecuteNonQuery(CommandType.Text, sqlScript); 方法:

ExecuteNonQuery

我不知道如何解决它。当我直接在fb上运行脚本时,它可以工作。

c# parsing sql-server-ce executenonquery
3个回答
1
投票

您必须将每个语句作为单独的命令运行,如果使用GO分隔每个命令,则可以使用辅助函数之类的代码分隔命令:

public int ExecuteNonQuery(CommandType type, string sql) { using (SqlCeConnection connection = CreateConnection()) { return ExecuteNonQuery(connection, type, sql); } } private int ExecuteNonQuery(SqlCeConnection connection, CommandType type, string sql) { using (SqlCeCommand command = new SqlCeCommand()) { command.Connection = connection; command.CommandType = type; command.CommandText = sql; return command.ExecuteNonQuery(); } }

正如您所看到的,我正在使用DbUp包中的https://github.com/ErikEJ/SqlCeToolbox/blob/master/src/API/Repositories/ServerDBRepository.cs#L639来执行此操作


2
投票

解决了:

SqlCommandReaderStreamed

为sql添加了分隔符:

string sqlScript = GetScriptFromAssembly(GetOldDatabaseVersion(), ConfigurationSettings.ValidDatabaseVersion);

string[] scripts = sqlScript.Split(new string[] { @"/*$$*/" }, StringSplitOptions.None);

foreach(var script in scripts)
    ExecuteNonQuery(CommandType.Text, script);

-1
投票
  • 请在更改表之前使用Go关键字
© www.soinside.com 2019 - 2024. All rights reserved.