C# 输入字符串的强制转换无效
我有一个由数据填充的类。并非所有数据类型都是字符串:C# 输入字符串的强制转换无效,c#,sql,asp.net-mvc,C#,Sql,Asp.net Mvc,我有一个由数据填充的类。并非所有数据类型都是字符串: public class NDCContract { public string ndc_id { get; set; } public string contract_num_val { get; set; } public string quote_price { get; set; } public string eff_dt { get; set; } public string end_dt
public class NDCContract
{
public string ndc_id { get; set; }
public string contract_num_val { get; set; }
public string quote_price { get; set; }
public string eff_dt { get; set; }
public string end_dt { get; set; }
public string discount_pct { get; set; }
public string rebate_pct { get; set; }
}
我有一个调用存储过程的服务,该存储过程通过连接返回如下内容:
然后循环我的代码以填充列表:
var ndccont = new List<BusinessObjects.Models.NDCContract>();
while (reader.Read())
{
var item = new BusinessObjects.Models.NDCContract();
item.ndc_id = reader.GetString(0);
item.contract_num_val = reader.GetString(1);
item.quote_price = reader.GetString(2);
item.eff_dt = reader.GetString(3);
item.end_dt = reader.GetString(4);
item.discount_pct = reader.GetString(5);
item.rebate_pct = reader.GetString(6);
ndccont.Add(item);
}
var ndccont=new List();
while(reader.Read())
{
var item=new BusinessObjects.Models.NDCContract();
item.ndc_id=reader.GetString(0);
item.contract_num_val=reader.GetString(1);
item.quote_price=reader.GetString(2);
item.eff_dt=reader.GetString(3);
item.end_dt=reader.GetString(4);
item.discount\u pct=reader.GetString(5);
item.retrieve_pct=reader.GetString(6);
ndccont.添加(项目);
}
在item.quote\u price=reader.GetString(2)行上代码>我收到错误:无法将“System.Decimal”类型的对象强制转换为“System.String”类型。
为什么当属性是字符串并且作为字符串读入时会出现此错误?如何修复此问题?只需使用Convert.ToString(reader.GetValue(I))
检索字段即可
reader.GetString(int)
不执行任何类型转换
指定区域性
如果要提供自己的区域性而不是当前区域性,可以使用类指定它:
正确声明您的模型。我不确定数据库中的数据类型是什么,但它们可能是这样的:
public class NDCContract
{
public string ndc_id { get; set; }
public string contract_num_val { get; set; }
public decimal quote_price { get; set; }
public DateTime eff_dt { get; set; }
public DateTime end_dt { get; set; }
public decimal discount_pct { get; set; }
public decimal rebate_pct { get; set; }
}
然后正确填充结构:
while (reader.Read())
{
var item = new BusinessObjects.Models.NDCContract();
item.ndc_id = reader.GetString(0);
item.contract_num_val = reader.GetString(1);
item.quote_price = reader.GetDecimal(2);
item.eff_dt = reader.GetDateTime(3);
item.end_dt = reader.GetDateTime(4);
item.discount_pct = reader.GetDecimal(5);
item.rebate_pct = reader.GetDecimal(6);
ndccont.Add(item);
}
使用读取器[2].ToString()
相反,您从db接收的值不是字符串而是十进制数。您确定第2列(即第三列)不是十进制数吗?若它是一个十进制数,为什么要把它转换成字符串呢?我担心数据库中所有的列都是字符串。但是,如果它们(感谢上帝)有正确的类型,为什么要为模型使用所有字符串?为什么不为每个属性使用正确的数据类型?@Evk,因为将非字符串值存储为字符串更有趣?像你对答案的评论中的1234,或者约会中的10/11/12都是很棒的例子-让生活变得不那么无聊。:)众所周知的做法-请注意,这种转换取决于当前的文化,这可能会产生意想不到的后果。@Evk很好。更新了我的答案。好的,这里有一个报价和一个折扣。“一个人如何将这两者相乘?”克里斯范德莫顿这是另一个问题的另一个问题problem@KrisVandermotten这很简单-只要十进制。解析和乘法。
while (reader.Read())
{
var item = new BusinessObjects.Models.NDCContract();
item.ndc_id = reader.GetString(0);
item.contract_num_val = reader.GetString(1);
item.quote_price = reader.GetDecimal(2);
item.eff_dt = reader.GetDateTime(3);
item.end_dt = reader.GetDateTime(4);
item.discount_pct = reader.GetDecimal(5);
item.rebate_pct = reader.GetDecimal(6);
ndccont.Add(item);
}