簡體   English   中英

MSSQL Server:正在運行存儲過程

[英]MsSql server:running stored procedure

我有桌子

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)

貨幣應該是一個字符串值,例如USD,CAD,EUR等

當我執行存儲過程時,這是我得到的錯誤

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;

            }

您正在使用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)

在我看來,您的表定義和存儲過程代碼似乎不同步。 這是你的表定義的列數據類型currency實際上是int ,但不知你在編輯器中改變了它nvarchar

要解決此問題,請右鍵單擊表並檢查表的定義(而不是編輯器窗口中的代碼),您將得到答案。

希望對您有幫助。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM