【发布时间】:2014-09-18 19:05:21
【问题描述】:
在我的 SQL Server Management Studio 中,我创建的其中一个表 (ACTION_LOCATION_MAP) 具有以下设计:
在我的一个函数中,我正在调用一个存储过程,以检索所有具有匹配 ActionId 的记录,这些记录是我传递给函数的。
存储过程:
CREATE PROCEDURE [dbo].[GetMapCoordinates]
@ActionId NVARCHAR(20)
AS
SET NOCOUNT ON;
BEGIN
SELECT * FROM ACTION_LOCATION_MAP
WHERE ActionId = @ActionId
END
函数(C#):
public IEumerable<MapCoordinate> GetActionMapCoordinates(string id)
{
/* Here a call is made to the stored procedure and the matching records are
returned. Truncating other parts of the code for brevity. */
....
cm.Parameters.Add(new SqlParameter("@ActionId", SqlDbType.NVarChar, 20) { Value = id });
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
var coordinate = new MapCoordinate
{
Map_Location_Id = int.Parse(dr["MapLocationId"].ToString()),
Action_Id = dr["ActionId"].ToString(),
Location_Title = dr["LocationTitle"].ToString(),
Location_Latitude = decimal.Parse(dr["Latitude"].ToString()),
Location_Longitude = decimal.Parse(dr["Longitude"].ToString()),
Shape_Group_Id = int.Parse(dr["ShapeGroupId"].ToString()),
Shape_Group_Order = int.Parse(dr["ShapeGroupOrder"].ToString())
};
coordinates.Add(coordinate);
}
// rest of the code truncated
}
调用该函数时,出现以下错误:
Input string was not in a correct format. System.FormatException
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at (/* namespace intentionally hidden */).GetActionMapCoordinates(String id)
at (/* namespace intentionally hidden */).LocationController.MapCoordinatesById(String id)
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}
我的预感是十进制值可能没有被正确转换,但作为 SQL 的新手(充其量),我已经为此苦苦挣扎了好几个小时。任何建议将不胜感激。
【问题讨论】:
-
简单问题..你用过调试器吗..?它在哪一行抛出错误..
-
看起来是解析问题。尝试在对 Parse () 的调用中使用与 ddbb 中存储的数字格式相对应的 CultureInfo 参数。否则从查询中删除列,直到找到违规的列;-)
-
另外,不要在查询中使用 *,这是邪恶的