MsSql server:运行存储过程

时间:2017-06-26 11:58:17

标签: c# sql-server-2016-express

我有一张桌子

CREATE TABLE [dbo].[DealerInfo](
    [DealerName] [nvarchar](100) NULL,
    [Address] [nvarchar](100) NULL,
    [City] [nvarchar](100) NULL,
    [County] [nvarchar](100) NULL,
    [Fax] [nvarchar](50) NULL,
    [CompanyWebSite] [nvarchar](max) NULL,
    [EmailAddress] [nvarchar](100) NULL,
    [Currency] [nvarchar](20) NULL,
    [LicenceID] [int] NULL,
    [TaxRegistration] [int] NULL,
    [Phone] [nvarchar](50) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

问题是,当我尝试插入货币值时,报告它需要一个整数值

我使用存储过程插入值

ALTER PROCEDURE [dbo].[DealerInformation]
    -- Add the parameters for the stored procedure here
    @DealerName nvarchar(100),
    @Address nvarchar(100),
    @City nvarchar (100),
    @County nvarchar (100), 
    @Phone nvarchar (100),
    @Fax nvarchar(50),
    @CompanyWebSite nvarchar (100),
    @EmailAddress nvarchar(Max),
    @Currency nvarchar(20),
    @LicenceID int,
    @TaxRegistration int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    Insert into DealerInfo values (@DealerName, @Address, @City, @County, @Phone, @Fax, @CompanyWebSite,@EmailAddress,@Currency,@LicenceID,@TaxRegistration)

货币应该是美元,加元,欧元等字符串值

当我执行存储过程时,这是我得到的错误

Msg 245, Level 16, State 1, Procedure DealerInformation, Line 26 [Batch Start Line 2]
Conversion failed when converting the nvarchar value 'USD' to data type int.


using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=CarDealership;Integrated Security=True") )
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand("DealerInformation", conn);

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("DealerName", dealership.DealershipName);
                cmd.Parameters.AddWithValue("Address", dealership.Address);
                cmd.Parameters.AddWithValue("City", dealership.City);
                cmd.Parameters.AddWithValue("County", dealership.Country);
                cmd.Parameters.AddWithValue("Phone", dealership.Telephone);
                cmd.Parameters.AddWithValue("Fax", dealership.Fax);
                cmd.Parameters.AddWithValue("CompanyWebSite", dealership.CompanyWebSite);
                cmd.Parameters.AddWithValue("EmailAddress", dealership.Email);
                cmd.Parameters.AddWithValue("Currency", dealership.Currency); //The currency is string type
                cmd.Parameters.AddWithValue("LicenceID", dealership.LicenceID);
                cmd.Parameters.AddWithValue("TaxRegistration", dealership.TaxRegistration);

                int RowsAffected = cmd.ExecuteNonQuery();
                return RowsAffected;

            }

2 个答案:

答案 0 :(得分:3)

您正在使用INSERT INTO语句而未指定字段名称 因此,您的参数应完全按照表

中字段名称的顺序列出

现在,数据库引擎正在使用这些参数设置字段

[DealerName]  = @DealerName, 
[Address] = @Address
[City] = @City
[County] = @County
[Fax] = @Phone 
[CompanyWebSite] = @Fax 
[EmailAddress] = @CompanyWebSite
[Currency] = @EmailAddress,
[LicenceID] = @Currency,
[TaxRegistration]  = @LicenceID
[Phone] = @TaxRegistration

如您所见,您的LicenceID字段(int)接收@Currency参数(nvarchar)的值。
这是错误的原因,修复是始终在INSERT INTO语句中指定字段名称,并以正确的顺序列出参数以设置关联字段。

Insert into DealerInfo 
   (DealerName, Address, City, County, Fax, CompanyWebSite,
   EmailAddress,Currency,LicenceID,TaxRegistration,Phone)
values (@DealerName, @Address, @City, @County, @Fax, @CompanyWebSite,
   @EmailAddress,@Currency,@LicenceID,@TaxRegistration, @Phone)

答案 1 :(得分:0)

对我而言,似乎您的表定义和存储过程代码不同步。那就是currency的表定义列数据类型实际上是int,但不知何故,您已在编辑器中将其更改为nvarchar

要解决此问题,请右键单击您的表并检查您的表的定义(而不是编辑器窗口中的代码),您将得到答案。

我希望,它会对你有所帮助。 感谢