我有以下要求:
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查询并且没有任何问题地得到了我的结果,所以我认为它与我的查询中的一些引号有关。
这是我的StackTrace:
[FormatException:输入字符串格式不正确。] 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。&lt;&gt; 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。&lt;&gt; c__DisplayClass631.<ExecuteStoreQueryReliably>b__61() +140 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func
1操作)+189 System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably(String commandText,String entitySetName,ExecutionOptions executionOptions,Object [] parameters)+335 System 。数据.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery(String commandText,ExecutionOptions executionOptions,Object [] parameters)+61 System.Data.Entity.Internal。&lt;&gt; 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()位于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 []参数)+14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,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.EndInvokeAct ionMethod(IAsyncResult asyncResult)+32 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3c()+50 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass45.b__3e()+ 225 System.Web.Mvc.Async。&lt。 ;&gt; c__DisplayClass45.b__3e()+ 225 System.Web.Mvc.Async。&lt;&gt; 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。&lt;&gt; c__DisplayClass28.b__19()+26 System.Web.Mvc.Async。&lt;&gt; 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 asyncRes ult)+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&amp; completedSynchronously)+155来源错误:
第273行var trackrinfo = db.Database.SqlQuery(sqlStr,empKey).ToList();第274行:第275行:
答案 0 :(得分:9)
内部格式会出现问题。它根据堆栈跟踪调用StringBuilder.AppendFormat
。该问题与SQL语句中的'{'
有关。
只需使用StringBuilder
即可完全测试。
下面是
System.FormatException:输入字符串的格式不正确。
var sb = new StringBuilder();
sb.AppendFormat("'{' {0}", 1);
你可以通过加倍{
var sb = new StringBuilder();
sb.AppendFormat("'{{' {0}", 1);
工作得很好。
{{
只能输出一个{
。加倍努力逃脱了它。
答案 1 :(得分:2)
我没有尝试这个的环境,但是从阅读documentation开始,看起来你的想法是正确的:
“您可以在SQL查询字符串中包含参数占位符,然后提供参数值作为附加参数。”
但是,查询中的占位符似乎不正确。
将{0}
替换为@p0
字符串中的sqlStr
并再次运行。
答案 2 :(得分:0)
我不确定这一部分:
a.EmpKey = {0}
看起来您可能尝试在某些时候使用string.format()构建此查询。在任何情况下,这对我来说看起来都不是有效的SQL(int不会有{/},如果这些是你要比较的值的一部分,它们应该在's。)