未能参数值从字符串到十进制转换? [英] Failed to convert parameter value from a String to a Decimal?
问题描述
我有这样一个存储过程:
ALTER PROCEDURE [DBO] [usp_CSR_UpdateDailyCustomerWithLCDHistory]
- 添加参数存储过程在这里
@person_id数字(18,0),
@last_contact_date日期时间,
@source为nvarchar(50),
@callLogId INT,
@createdBy为nvarchar(50)
AS
BEGIN ... END
在我的代码调用这个存储过程:
公共无效UpdateDailyCustomerWithLCDHistory(字符串来电显示,
INT callLogId,串csrName )
{
试
{
数据库DB = DatabaseFactory.CreateDatabase
(MarketingWriteConnectionString);
的DbCommand的DbCommand =
db.GetStoredProcCommand(usp_CSR_UpdateDailyCustomerWithLCDHistory);
dbCommand.CommandTimeout = AppSetting.Instance.GetCommandTimeout();
db.AddInParameter(的DbCommand,为person_id,DbType.Decimal,来电显示);
db.AddInParameter(的DbCommandlast_contact_date,DbType.Date,
DateTime.Now.ToShortDateString());
db.AddInParameter(的DbCommand,源,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] [数字](18,0)NOT NULL,
[来源] [为nvarchar(50)NOT NULL,
[callLogId] [INT] NULL,
[createdBy] [为nvarchar(50)NULL,
[last_contact_date] [日期时间] NOT NULL,
在部署应用程序,我得到了错误
的无法参数值从字符串转换为十进制的
块引用>
很多时候,并不是所有的时间虽然。谁能告诉我是什么地方出错了?
解决方案公共无效UpdateDailyCustomerWithLCDHistory(字符串来电显示,诠释callLogId,串csrName)
{
试
{
数据库DB = DatabaseFactory.CreateDatabase(MarketingWriteConnectionString );
的DbCommand的DbCommand = db.GetStoredProcCommand(usp_CSR_UpdateDailyCustomerWithLCDHistory);
dbCommand.CommandTimeout = AppSetting.Instance.GetCommandTimeout();
db.AddInParameter(的DbCommand,为person_id,DbType.Decimal,来电显示);
的CALLID是一个字符串,你需要分配给值之前将其转换小数。
您可以使用Decimal.TryParse()将字符串转换为十进制数。
http://msdn.microsoft.com/en -us /库/ system.decimal.tryparse.aspxI have a stored procedure like this:
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
In my code I call this stored procedure:
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); } }
And this is my table's structure:
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,
After I deploy my application, I got the error
Failed to convert parameter value from a String to a Decimal
quite often, not all the time though. Can anybody tell me what could be wrong?
解决方案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);
the callId is a string, you need convert it the decimal before you assign to the value.
you can use Decimal.TryParse() to convert string to Decimal number. http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx
这篇关于未能参数值从字符串到十进制转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!