ASP.NET Sql insert将数据类型nvarchar转换为numeric时出错

时间:2017-01-15 15:38:03

标签: c# sql

实体类我想要添加到DB的对象:

  public void AddItem(Item item)
    {

        var cnnString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

        var cmd = "INSERT INTO Items([Id],[Name],[Description] ,[Price] , [Currency] ,  [Negotiable], [CategoryId], [OwnerId] ) VALUES(@id,@name,@description,@price,@currency,@negotiable,@categoryId,@ownerId)";

        using (SqlConnection cnn = new SqlConnection(cnnString))
        {

            using (SqlCommand cmd1 = new SqlCommand(cmd, cnn))
            {
                cnn.Open();
                cmd1.CommandText = "SELECT MAX(Id) FROM Items";
                using (SqlDataReader reader = cmd1.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        int maxId = Convert.ToInt32(cmd1.ExecuteScalar());
                        item.Id = maxId;
                    }
                }

                cmd1.Parameters.AddWithValue("@id", item.Id);
                cmd1.Parameters.AddWithValue("@name", item.Name);
                cmd1.Parameters.AddWithValue("@description", item.Description);
                cmd1.Parameters.Add("@price", SqlDbType.Decimal).Value = item.Price;
                cmd1.Parameters.AddWithValue("@currency", item.Currency);
                cmd1.Parameters.AddWithValue("@negotiable", item.Negotiable);
                cmd1.Parameters.AddWithValue("@categoryId", item.CategoryId);
                cmd1.Parameters.AddWithValue("@ownerId", item.OwnerId);



                cmd1.ExecuteNonQuery();  //*
            }
        }

    }

使用代码优先方法生成的本地Db。 现在我想使用Sql Command在我的应用程序中添加一行:

CREATE TABLE [dbo].[Items] (
[Id]          BIGINT          IDENTITY (1, 1) NOT NULL,
[Name]        NVARCHAR (100)  NOT NULL,
[Description] NVARCHAR (1000) NULL,
[Price]       DECIMAL (18, 2) NOT NULL,
[Currency]    INT             NOT NULL,
[Negotiable]  BIT             NOT NULL,
[CategoryId]  TINYINT         NOT NULL,
[Category_Id] INT             NULL,
[OwnerId]     NVARCHAR (128)  DEFAULT ('') NOT NULL,
CONSTRAINT [PK_dbo.Items] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_dbo.Items_dbo.Categories_Category_Id] FOREIGN KEY ([Category_Id]) REFERENCES [dbo].[Categories] ([Id]),
CONSTRAINT [FK_dbo.Items_dbo.AspNetUsers_OwnerId] FOREIGN KEY ([OwnerId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE

EDITED: 上面的代码抛出异常:这里已经是一个与这个命令关联的开放DataReader,必须先关闭它。""

T-SQL中的DB Schema定义如下:

android:src="@drawable/yourImage

);

2 个答案:

答案 0 :(得分:1)

更改您向此插入查询。并立即尝试

            public long AddItem(Item item) 
        { 

        var cnnString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; 
        var cmdInsert = "INSERT INTO Items([Name],[Description] ,[Price] , [Currency] , [Negotiable], [CategoryId], [OwnerId] ) VALUES(@name,@description,@price,@currency,@negotiable,@categoryId,@ownerId)"; 


        using (SqlConnection cnn = new SqlConnection(cnnString)) 
        { 
        cnn.Open(); 
        using (SqlCommand cmd1 = new SqlCommand(cmdInsert, cnn)) 
        { 

        cmd1.Parameters.AddWithValue("@name", item.Name); 
        cmd1.Parameters.AddWithValue("@description", item.Description); 
        cmd1.Parameters.Add("@price", SqlDbType.Decimal).Value = item.Price; 
        cmd1.Parameters.AddWithValue("@currency", item.Currency); 
        cmd1.Parameters.AddWithValue("@negotiable", item.Negotiable); 
        cmd1.Parameters.AddWithValue("@categoryId", item.CategoryId); 
        cmd1.Parameters.AddWithValue("@ownerId", item.OwnerId); 

        cmd1.ExecuteNonQuery(); 
        } 
        cnn.Close(); 
        } 

        }

答案 1 :(得分:0)

让我们看看您的代码:

system()

和表定义:

    var cmd = "INSERT INTO Items VALUES(@id,
@name,
@description,
@price,
@currency,    
@negotiable,    
@categoryId,    
@ownerId)";

你在表定义中有CategoryId,Category_Id,OwnerId,在C#中只有@categoryId,@ ownerId。你忘记了@category_Id值,我想