使用 firebird 和 c# 创建身份字段时创建生成器错误错误
create GENERATOR error error while creating identity field using firebird and c#
我在我的一个 win 表单项目中使用 C# 和 Firebird SQL 数据库。在其中一个表格中,我在 firebird 数据库中创建了一个 table,其中有一个字段 "id" 作为 identity/autoincrement 字段。我的代码是
private void createOrLoadGIIR(int pid, int tid,int mid)
{
string qryStrCL1 = @"EXECUTE BLOCK AS BEGIN
if (not exists(select 1 from rdb$relations where rdb$relation_name = 'loc_soningqp')) then
execute statement 'CREATE TABLE loc_soningqp (
q_r_id integer NOT NULL,
q_r_seccd varchar(5) NOT NULL,
q_r_subseccd varchar(5) NOT NULL,
q_r_direction blob,
q_r_desc blob NOT NULL,
q_r_caopt1 smallint DEFAULT 0,
q_r_caopt2 smallint DEFAULT 0,
q_r_caopt3 smallint DEFAULT 0,
q_r_caopt4 smallint DEFAULT 0,
q_r_caopt5 smallint DEFAULT 0,
q_r_sol blob,
q_r_difficulty varchar(10) DEFAULT NULL,
id integer NOT NULL,
PRIMARY KEY (id)
);';
CREATE GENERATOR gen_loc_soningqp_id;
CREATE TRIGGER loc_soningqp_bi FOR loc_soningqp
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.id IS NULL) THEN
NEW.id = GEN_ID(gen_loc_soningqp_id,1);
END^
END";
try
{
using (FbConnection conCL1 = new FbConnection(connectionString))
{
conCL1.Open();
using (FbCommand cmdCL1 = new FbCommand(qryStrCL1, conCL1))
{
cmdCL1.CommandType = CommandType.Text;
using (FbDataReader rdrCL1 = cmdCL1.ExecuteReader())
{
if (rdrCL1 != null)
{
//some code
}
}
}// command disposed here
} //connection closed and disposed here
}
catch (FbException ex)
{
//table exists
MessageBox.Show(ex.Message);
}
}
当我 运行 代码然后它在 CREATE GENERATOR 行附近生成一个错误说 'CREATE is an unknown token'。请告知我的 code.I 有什么问题也想知道它是否可能在 EXECUTE BLOCK 中创建存储过程。
您的查询有误。我觉得应该是这样的:
"EXECUTE BLOCK AS BEGIN
if (not exists(select 1 from rdb$relations where rdb$relation_name = 'loc_soningqp')) then
begin
execute statement 'CREATE TABLE loc_soningqp (
q_r_id integer NOT NULL,
q_r_seccd varchar(5) NOT NULL,
q_r_subseccd varchar(5) NOT NULL,
q_r_direction blob,
q_r_desc blob NOT NULL,
q_r_caopt1 smallint DEFAULT 0,
q_r_caopt2 smallint DEFAULT 0,
q_r_caopt3 smallint DEFAULT 0,
q_r_caopt4 smallint DEFAULT 0,
q_r_caopt5 smallint DEFAULT 0,
q_r_sol blob,
q_r_difficulty varchar(10) DEFAULT NULL,
id integer NOT NULL,
PRIMARY KEY (id)
);';
execute statement 'CREATE GENERATOR gen_loc_soningqp_id;';
execute statement '
CREATE TRIGGER loc_soningqp_bi FOR loc_soningqp
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.id IS NULL) THEN
NEW.id = GEN_ID(gen_loc_soningqp_id,1);
END^
';
end
END";
问题是不允许在 PSQL 代码中执行 DDL(如 EXECUTE BLOCK
)。作为 ,您还需要将 CREATE GENERATOR
和 CREATE TRIGGER
语句包装在 EXECUTE STATEMENT
中。使用 EXECUTE STATEMENT
作为解决方法很聪明,但有时有点麻烦。
我在我的一个 win 表单项目中使用 C# 和 Firebird SQL 数据库。在其中一个表格中,我在 firebird 数据库中创建了一个 table,其中有一个字段 "id" 作为 identity/autoincrement 字段。我的代码是
private void createOrLoadGIIR(int pid, int tid,int mid)
{
string qryStrCL1 = @"EXECUTE BLOCK AS BEGIN
if (not exists(select 1 from rdb$relations where rdb$relation_name = 'loc_soningqp')) then
execute statement 'CREATE TABLE loc_soningqp (
q_r_id integer NOT NULL,
q_r_seccd varchar(5) NOT NULL,
q_r_subseccd varchar(5) NOT NULL,
q_r_direction blob,
q_r_desc blob NOT NULL,
q_r_caopt1 smallint DEFAULT 0,
q_r_caopt2 smallint DEFAULT 0,
q_r_caopt3 smallint DEFAULT 0,
q_r_caopt4 smallint DEFAULT 0,
q_r_caopt5 smallint DEFAULT 0,
q_r_sol blob,
q_r_difficulty varchar(10) DEFAULT NULL,
id integer NOT NULL,
PRIMARY KEY (id)
);';
CREATE GENERATOR gen_loc_soningqp_id;
CREATE TRIGGER loc_soningqp_bi FOR loc_soningqp
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.id IS NULL) THEN
NEW.id = GEN_ID(gen_loc_soningqp_id,1);
END^
END";
try
{
using (FbConnection conCL1 = new FbConnection(connectionString))
{
conCL1.Open();
using (FbCommand cmdCL1 = new FbCommand(qryStrCL1, conCL1))
{
cmdCL1.CommandType = CommandType.Text;
using (FbDataReader rdrCL1 = cmdCL1.ExecuteReader())
{
if (rdrCL1 != null)
{
//some code
}
}
}// command disposed here
} //connection closed and disposed here
}
catch (FbException ex)
{
//table exists
MessageBox.Show(ex.Message);
}
}
当我 运行 代码然后它在 CREATE GENERATOR 行附近生成一个错误说 'CREATE is an unknown token'。请告知我的 code.I 有什么问题也想知道它是否可能在 EXECUTE BLOCK 中创建存储过程。
您的查询有误。我觉得应该是这样的:
"EXECUTE BLOCK AS BEGIN
if (not exists(select 1 from rdb$relations where rdb$relation_name = 'loc_soningqp')) then
begin
execute statement 'CREATE TABLE loc_soningqp (
q_r_id integer NOT NULL,
q_r_seccd varchar(5) NOT NULL,
q_r_subseccd varchar(5) NOT NULL,
q_r_direction blob,
q_r_desc blob NOT NULL,
q_r_caopt1 smallint DEFAULT 0,
q_r_caopt2 smallint DEFAULT 0,
q_r_caopt3 smallint DEFAULT 0,
q_r_caopt4 smallint DEFAULT 0,
q_r_caopt5 smallint DEFAULT 0,
q_r_sol blob,
q_r_difficulty varchar(10) DEFAULT NULL,
id integer NOT NULL,
PRIMARY KEY (id)
);';
execute statement 'CREATE GENERATOR gen_loc_soningqp_id;';
execute statement '
CREATE TRIGGER loc_soningqp_bi FOR loc_soningqp
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.id IS NULL) THEN
NEW.id = GEN_ID(gen_loc_soningqp_id,1);
END^
';
end
END";
问题是不允许在 PSQL 代码中执行 DDL(如 EXECUTE BLOCK
)。作为 CREATE GENERATOR
和 CREATE TRIGGER
语句包装在 EXECUTE STATEMENT
中。使用 EXECUTE STATEMENT
作为解决方法很聪明,但有时有点麻烦。