无法将参数值从String转换为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不是匹配类型,您应该更改它,或者至少转换它。