簡體   English   中英

無法將參數值從String轉換為Decimal?

[英]Failed to convert parameter value from a String to a Decimal?

我有一個這樣的存儲過程:

ALTER PROCEDURE [dbo].[usp_CSR_UpdateDailyCustomerWithLCDHistory]
    -- Add the parameters for the stored procedure here
    @person_id numeric(18, 0),
    @last_contact_date datetime,
    @source nvarchar(50),
    @callLogId int,
    @createdBy nvarchar(50)
AS
BEGIN...END

在我的代碼中,我調用此存儲過程:

public void UpdateDailyCustomerWithLCDHistory(string callerId, 
    int callLogId, string csrName)
{
    try
    {
        Database db = DatabaseFactory.CreateDatabase
           ("MarketingWriteConnectionString");
        DbCommand dbCommand = 
           db.GetStoredProcCommand("usp_CSR_UpdateDailyCustomerWithLCDHistory");
        dbCommand.CommandTimeout = AppSetting.Instance.GetCommandTimeout();

        db.AddInParameter(dbCommand, "person_id", DbType.Decimal, callerId);
        db.AddInParameter(dbCommand, "last_contact_date", DbType.Date,
            DateTime.Now.ToShortDateString());
        db.AddInParameter(dbCommand, "source", DbType.String, "CSR");
        db.AddInParameter(dbCommand, "callLogId", DbType.Int32, callLogId);
        db.AddInParameter(dbCommand, "createdBy", DbType.String, csrName);

        db.ExecuteNonQuery(dbCommand);
    }
}

這是我桌子的結構:

CREATE TABLE [dbo].[tblDailyCustomerLastContactDateHistory](
    [person_id] [numeric](18, 0) NOT NULL,
    [source] [nvarchar](50) NOT NULL,
    [callLogId] [int] NULL,
    [createdBy] [nvarchar](50) NULL,
    [last_contact_date] [datetime] NOT NULL,

部署我的應用程序后,我收到了錯誤

無法將參數值從String轉換為Decimal

經常,不是所有的時間。 誰能告訴我什么可能是錯的?

public void UpdateDailyCustomerWithLCDHistory(string callerId, int callLogId, string csrName)
{
    try
    {
        Database db = DatabaseFactory.CreateDatabase("MarketingWriteConnectionString");
        DbCommand dbCommand = db.GetStoredProcCommand("usp_CSR_UpdateDailyCustomerWithLCDHistory");
        dbCommand.CommandTimeout = AppSetting.Instance.GetCommandTimeout();

        db.AddInParameter(dbCommand, "person_id", DbType.Decimal, callerId);

callId是一個字符串,在分配給值之前需要將其轉換為小數。

您可以使用Decimal.TryParse()將字符串轉換為十進制數。 http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx

如果你想要一個數字標識符,我建議你使用INT作為Id而不是NUMERIC (十進制)字段。 除非你有1.2,1.3等的ID。

另外,看看AddWithValue - 您不必在.NET代碼中指定類型。 該框架將為您處理。

最后,如果callerId不是匹配類型,您應該更改它,或者至少轉換它。

暫無
暫無

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

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