0

尝试在我的表中执行插入语句时,出现此错误:

数据转换失败。[ OLE DB 状态值(如果已知)= 2 ]

我已经在网上完成了我的研究,似乎无法找到解决这个问题的答案。

插入语句如下所示:

INSERT INTO [Beverages] (BeverageID, ParentBeverageID, Name, PreferredRecipeVersion, 
                         SpecificCustomerID, AcceptabilityRating, ShowBeverage, Availability, 
                         Family, EngagedPriority, SmilePriority, Collections) 
VALUES (1475614, 1475633, 'Caffeine-free', 3, 1, 0, 1, 1, 'A', 'NULL', 1, 'UK')

我最初构建的表(确实创建了它)如下所示:

string beverages = "CREATE TABLE [Beverages]([BeverageID] INT NOT NULL,[ParentBeverageID] INT, [Name] NVARCHAR(40) NOT NULL,[PreferredRecipeVersion] INT NOT NULL DEFAULT(1),[SpecificCustomerID] INT NOT NULL DEFAULT(0),[AcceptabilityRating] INT NOT NULL DEFAULT(0),[ShowBeverage] INT NOT NULL DEFAULT(1),[Availability] INT NOT NULL DEFAULT(1),[Family] NVARCHAR(1),[EngagedPriority] INT,[SmilePriority] INT NOT NULL DEFAULT(0), [Collections] NVARCHAR(50) NOT NULL);";
string beverages1 = "ALTER TABLE [Beverages] ADD CONSTRAINT [PK__Beverages__000000000000134A] PRIMARY KEY ([BeverageID]);";
string beverages2 = "CREATE INDEX [PK_BeverageID] ON [Beverages] ([BeverageID] ASC);";
db.ExecuteSqlCommand(beverages, null, __gulfstreamDataConn);
db.ExecuteSqlCommand(beverages1, null, __gulfstreamDataConn);
db.ExecuteSqlCommand(beverages2, null, __gulfstreamDataConn);

我不知道是什么导致了这个问题,但提前感谢您的帮助!

4

3 回答 3

2

如果要将字段EngagedPriority设置为 NULL,则应写入不带单引号的 NULL。在单词 NULL 周围使用引号会使解析器将值识别为字符串,但字段EngagedPriority是一个 Int...。

 string beverages = "CREATE TABLE [Beverages]([BeverageID] INT NOT NULL," + 
                    "[ParentBeverageID] INT, [Name] NVARCHAR(40) NOT NULL," + 
                    "[PreferredRecipeVersion] INT NOT NULL DEFAULT(1)," + 
                    "[SpecificCustomerID] INT NOT NULL DEFAULT(0)," + 
                    "[AcceptabilityRating] INT NOT NULL DEFAULT(0)," + 
                    "[ShowBeverage] INT NOT NULL DEFAULT(1)," + 
                    "[Availability] INT NOT NULL DEFAULT(1)," + 
                    "[Family] NVARCHAR(1)," + 
                -->>"[EngagedPriority] INT," + 
                    "[SmilePriority] INT NOT NULL DEFAULT(0), " + 
                    "[Collections] NVARCHAR(50) NOT NULL);";


  "INSERT INTO [Beverages] (BeverageID, ParentBeverageID, Name, PreferredRecipeVersion," + 
  "SpecificCustomerID, AcceptabilityRating, ShowBeverage, Availability, Family," +
  "EngagedPriority, SmilePriority, Collections) " + 
  "Values (1475614,1475633,'Caffeine-free ',3,1,0,1,1,'A',NULL,1,'UK')
                                                          ^^^^
于 2013-07-30T18:54:13.990 回答
0

您的问题是您插入的是“NULL”而不是 NULL。当您使用“NULL”时,SQL 将其作为 VarChar。NULL(无引号)将为您提供您正在寻找的答案。

于 2013-07-30T19:05:40.657 回答
0

NullNULL不是'NULL'- 从 INSERT 中删除单引号。

于 2013-07-30T19:00:12.330 回答