如何向以编程方式创建的SQL Server CE表添加索引?

时间:2013-12-06 22:55:23

标签: sql sql-server-ce compact-framework windows-ce ddl

我正在使用这样的代码来创建SQL Server CE表:

command.CommandText = "CREATE TABLE InventoryItems (Id nvarchar(50) NOT NULL, PackSize smallint NOT NULL, Description nvarchar(255), DeptDotSubdept float, UnitCost float, UnitList float, UPCCode nvarchar(50), UPCPackSize smallint, CRVId int);";
command.ExecuteNonQuery();

现在我也需要添加一个索引。我这样做是这样的:

command.CommandText = "CREATE TABLE InventoryItems (Id nvarchar(50) NOT NULL, PackSize smallint NOT NULL, Description nvarchar(255), DeptDotSubdept float, UnitCost float, UnitList float, UPCCode nvarchar(50), UPCPackSize smallint, CRVId int);
CREATE INDEX idxUnitCost ON InventoryItems (UnitCost) ASC";
command.ExecuteNonQuery();

...(其中命令文本包含两个分号分隔的DDL语句)或类似:

command.CommandText = "CREATE TABLE InventoryItems (Id nvarchar(50) NOT NULL, PackSize smallint NOT NULL, Description nvarchar(255), DeptDotSubdept float, UnitCost float, UnitList float, UPCCode nvarchar(50), UPCPackSize smallint, CRVId int);"
command.ExecuteNonQuery();

command.CommandText = "CREATE INDEX idxUnitCost ON InventoryItems (UnitCost) ASC";
command.ExecuteNonQuery();

...(命令,执行,命令,执行)或其他方式?

1 个答案:

答案 0 :(得分:2)

两个单独的命令。 SQL Compact不允许您一次执行多个命令。