输入字符串的格式不正确错误 #2
本文关键字:错误 不正确 格式 字符串 输入 | 更新日期: 2023-09-27 18:31:00
我有以下要求:
string sqlStr = @"Select a.Text, a.TrackKey, a.NoteType, a.TranKey, a.TranType,
Case a.TranType when '1110' then 'https://www.cps/SAM/SO/RMA/RMAView.asp?key='
when '502' then 'https://www.cps/SAM/AR/InvInqDetail.asp?Key='
when '511' then 'https://www.cps/SAM/AR/InvInqDetail.asp?Key='
when '801' then 'https://www.cps/SAM/AR/OrdInqDetail.asp?Key='
when '1101' then 'https://www.cps/SAM/AP/POInqDetail.asp?key='
when '1111' then 'https://www.cps/SAM/PO/TruckDetail.asp?truckkey='
when '835' then 'https://www.cpsm/SAM/SO/RMA/RMAView.asp?key=' End HREF,
Case when a.TranType = '1110' then 'M'
when a.TranType = '502' then 'W'
when a.TranType = '511' then 'W'
when a.TranType = '1101' then 'B'
when a.TranType = '1111' then 'B'
when a.TranType = '835' then '('
when (a.TranType = '801' and a.NoteType = '9') then '{'
when (a.TranType = '801' and a.NoteType = '8') then '(' End dataicon
From tamTrackrEngine a
Where a.CreateDate > DATEADD(DAY, DATEDIFF(DAY, 0, GetDate()), 0)
AND (a.EmpKey = {0} OR a.EmpKey IS NULL)
AND a.Text IS NOT NULL
Order by a.CreateDate DESC";
var trackrinfo = db.Database.SqlQuery<TrackrData>(sqlStr, empKey).ToList();
var TrackrInfo = JsonConvert.SerializeObject(trackrinfo);
Response.Write(TrackrInfo);
HttpContext.Response.AppendHeader("Content-Type", "application/json");
return new EmptyResult();
当我运行此代码时,出现错误:
我输入字符串格式不正确。
输入了一个更简单的sql查询,并毫无问题地返回了我的结果,所以我认为这与我的查询中的一些引号有关。
这是我的堆栈跟踪:
[格式异常:输入字符串的格式不正确。 System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) +10930058 System.String.Format(IFormatProvider provider, String format, Object[] args) +63 System.Data.Entity.Core.Objects.ObjectContext.CreateStoreCommand(String commandText, Object[] parameters) +790 System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal(String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters) +208 System.Data.Entity.Core.Objects.<>c__DisplayClass63
1.<ExecuteStoreQueryReliably>b__62() +45 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func
1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +333 System.Data.Entity.Core.Objects.<>c__DisplayClass631.<ExecuteStoreQueryReliably>b__61() +140 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func
1 操作) +189 System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryRelyly(String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters) +335 System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery(String commandText, ExecutionOptions executionOptions, Object[] parameters) +61 System.Data.Entity.Internal.<>c__DisplayClass131.<ExecuteSqlQuery>b__12() +151 System.Lazy
1.CreateValue() +415 System.Lazy1.LazyInitValue() +152 System.Lazy
1.get_Value() +75 System.Data.Entity.Internal.LazyEnumerator1.MoveNext() +12 System.Collections.Generic.List
1..ctor(IEnumerable1 collection) +381 System.Linq.Enumerable.ToList(IEnumerable
1 source) +58 SAM3.Controllers.SharedDataController.Trackr() in c:''Users''candicesteele''Source''Repos''SAM3''SAM3''Controllers''SharedDataController.cs:273 lambda_method(Closure , ControllerBase , Object[] ) +62 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContextContext, IDictionary2 parameters) +157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 个参数) +27 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3c() +50 System.Web.Mvc.Async.<>c__DisplayClass45.b__3e() +225 System.Web.Mvc.Async.<>c__DisplayClass45.b__3e() +225 System.Web.Mvc.Async.<>c__DisplayClass30.b__2f(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.<>c__DisplayClass28.b__19() +26 System.Web.Mvc.Async.<>c__DisplayClass1e.b__1b(IAsyncResult asyncResult) +100 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +36 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +54 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +12 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +54 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.MvcHandler.b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +21 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +36 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +54 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9657896 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completeSyncally) +155源错误:
第 273 行 var trackrinfo = db。Database.SqlQuery(sqlStr, empKey).ToList();274行:275行:
这将是内部格式的问题。它根据该堆栈跟踪调用StringBuilder.AppendFormat
。该问题与 SQL 语句中的'{'
有关。
这完全可以通过使用 StringBuilder
进行测试。
下面炸了
System.FormatException : 输入字符串的格式不正确。
var sb = new StringBuilder();
sb.AppendFormat("'{' {0}", 1);
您可以通过加倍{
来解决此问题
var sb = new StringBuilder();
sb.AppendFormat("'{{' {0}", 1);
工作得很好。
{{
应该只输出一个{
。加倍它逃脱了它。
我没有一个环境来尝试这个,但从阅读文档来看,你的想法似乎是正确的:
"您可以在 SQL 查询字符串中包含参数占位符,然后提供参数值作为附加参数。"
但是,查询中的占位符似乎不正确。
将{0}
替换为sqlStr
字符串中的@p0
,然后再次运行它。
我不确定这部分:
a.EmpKey = {0}
看起来您可能在某个时候尝试使用 string.format() 构建此查询。 无论如何,这对我来说看起来不像有效的 SQL(int 不会有 {/},如果这些是您比较的值的一部分,它们应该在 ''s 中。