无法将数据类型nvarchar转换为数字

时间:2014-04-08 18:34:30

标签: c# sql sql-server parameterized-query table-valued-parameters

我使用以下查询将数据从Excel文件导入SQL Server数据库。 Excel文件将所有值都作为字符串类型('在每个单元格之前)。

导入时出现此错误。“无法将数据类型nvarchar转换为数字”

如果我从导入中删除了两列SalePrice和Price2,则导入成功。

我的表的数据类型是

CREATE TYPE [dbo].[InventoryType] AS TABLE
(
   [LocalSKU] [varchar](200) NOT NULL,
   [ItemName] [varchar](200) NULL,
   [QOH] [int] NULL,
   [Price] [decimal](19, 4) NULL,
   [Discontinued] [bit] NULL,
   [Barcode] [varchar](25) NULL,
   [Integer2] [int] NULL,
   [Integer3] [int] NULL,
   [SalePrice] [decimal](19, 4) NULL,
   [SaleOn] [bit] NULL,
   [Price2] [decimal](19, 4) NULL
)
GO

我使用的查询是:

SqlCommand sqlcmd = new SqlCommand
   (@"MERGE Inventory AS target
      USING (SELECT
                LocalSKU, ItemName, QOH, Price, Discontinued, 
                Barcode, Integer2, Integer3, SalePrice, SaleOn, Price2 
             FROM @source) AS Source ON (Source.LocalSKU = target.LocalSKU)
      WHEN MATCHED THEN
         UPDATE 
           SET ItemName = source.ItemName,
               Price = source.Price,
               Discontinued = source.Discontinued,
               Barcode = source.Barcode,
               Integer2 = source.Integer2,
               Integer3 = source.QOH,
               SalePrice = source.SalePrice,
               SaleOn = source.SaleOn,
               Price2 = source.Price2;", sqlconn);

SqlParameter param;
param = sqlcmd.Parameters.AddWithValue("@source", dr);
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.InventoryType";

sqlconn.Open();
sqlcmd.ExecuteNonQuery();
sqlconn.Close();

1 个答案:

答案 0 :(得分:0)

要记住的一件事是,您可能在给定列中有一些无效数据会导致错误。例如,如果您有一个数字列并且具有字母或格式不正确的数值,则会抛出此错误。检查单元格中先无法更正的值。这也包括尾随或前导空格。

相关问题