C# ASP.NET MVC viewmodel SQL查询语法
我正在尝试将基本模型转换为模型vİew和SQL查询。那么,我的查询或其他文件有什么问题C# ASP.NET MVC viewmodel SQL查询语法,c#,sql-server,asp.net-mvc,action,viewmodel,C#,Sql Server,Asp.net Mvc,Action,Viewmodel,我正在尝试将基本模型转换为模型vİew和SQL查询。那么,我的查询或其他文件有什么问题 // GET: Cars public ActionResult Index() { List<CarsTrxViewModel> model = db.Database.SqlQuery<CarsTrxViewModel>(SqlCars.CarsTrxByID()).ToList(); return View(model); // return View(
// GET: Cars
public ActionResult Index()
{
List<CarsTrxViewModel> model = db.Database.SqlQuery<CarsTrxViewModel>(SqlCars.CarsTrxByID()).ToList();
return View(model);
// return View(db.Cars.ToList());
}
查询:
public static class SqlCars
{
public static string CarsTrxByID()
{
return "select " + " * , " +
"cars.CarID as CarID " +
",cars.CarBrand as CarBrand " +
",cars.CarModel as CarModel " +
",cars.CreateDate as CreateDate " +
"from (SELECT * from [NagisOfflineDB].[dbo].[cars] )";
}
public static string GetCardholderByID()
{
return "SELECT TOP 1 * from [NagisOfflineDB].[dbo].[cars]";
}
}
型号:
public class Cars
{
[Key]
public int CarID { get; set; }
[StringLength(50)]
[Required]
public string CarBrand { get; set; }
[StringLength(250)]
[Required]
public string CarModel { get; set; }
public DateTime CreateDate { get; set; }
}
我不是join或其他操作,我只是在一个表中选择一列并设置别名行,那么我的查询出了什么问题
编译器说您的查询错误,其错误为System.Data.SqlClient.SqlException:“附近的语法不正确”。”
错误的完整版本
“/”应用程序中的服务器错误。“')附近的语法不正确 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息:System.Data.SqlClient.SqlException:靠近“')的语法不正确 源错误: 第20行:
第21行:
第22行:List model=db.Database.SqlQuery(SqlCars.CarsTrxByID()).ToList() 和堆栈跟踪 [SqlException(0x80131904):靠近''的语法不正确。]
System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,Action
1 wrapCloseInAction)+2552942
System.Data.SqlClient.SqlInternalConnection.OneError(SqlException异常,布尔断开连接,操作
1 wrapCloseInAction)+5952492System.Data.SqlClient.TdsParser.throweexception和warning(TdsParserStateObject stateObj,布尔调用者连接锁,布尔异步关闭)+285 System.Data.SqlClient.TdsParser.TryRun(RunBehavior RunBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean&dataReady)+4169 System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()+58 System.Data.SqlClient.SqlDataReader.get_MetaData()+89 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds、RunBehavior RunBehavior、String ResetOptionString、Boolean isInternal、Boolean ForDescriptionParameterEncryption、Boolean shouldCacheForAlwaysEncrypted)+430 System.Data.SqlClient.SqlCommand.RunExecuteReaderDS(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、Boolean async、Int32超时、任务与任务、Boolean asyncWrite、Boolean inRetry、SqlDataReader ds、Boolean describeParameterEncryptionRequest)+2598 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior RunBehavior,Boolean returnStream,String方法,TaskCompletionSource
1完成,Int32超时,任务与任务,Boolean&usedCache,Boolean asyncWrite,Boolean inRetry)+1483
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior RunBehavior,Boolean returnStream,String方法)+64
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior,String方法)+240
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior)+41
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior)+12
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b_uc(DbCommand t,DbCommandInterceptionContext
1c)+14
System.Data.Entity.Infrastructure.InternalDispatcher1.Dispatch(t目标目标,Func
3操作,TInterceptionContext拦截上下文,Action3执行,Action
3执行)+72
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand命令,DbCommandInterceptionContext interceptionContext)+402
System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior)+166
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior)+12
System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal(String commandText,String entitySetName,ExecutionOptions ExecutionOptions,Object[]参数)+266
System.Data.Entity.Core.Objects.c_uuudisplayClass691.b_uu68()+44
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func
1 Func,IDBEcutionStrategy executionStrategy,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess)+288
System.Data.Entity.Core.Objects.c__DisplayClass691.b__67()+167
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func
1操作)+190
System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably(String commandText、String entitySetName、ExecutionOptions ExecutionOptions、Object[]参数)+462
System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery(String commandText,ExecutionOptions ExecutionOptions,Object[]参数)+60
System.Data.Entity.Internal.c_uuuDisplayClass141.b_uuu13()+130
System.Data.Entity.Internal.LazyEnumerator
1.MoveNext()+45
System.Collections.Generic.List1..ctor(IEnumerable
1 collection)+387
System.Linq.Enumerable.ToList(IEnumerable1源代码)+58
C:\Dev\nagisdevolffline\NgsOfflineDev\NgsOfflineDev\Controllers\carscocontroller.cs:22中的NgsOfflineDev.Controllers.carscocontroller.Index()
lambda_方法(闭包、控制器基、对象[])+61
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,对象[]参数)+14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary
2个参数)+157
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary2参数)+27
System.Web.Mvc.Async.c.b_9_0(IAsyncResult asyncResult,ActionInvocation innerInvokeState)+22
System.Web.Mvc.Async.WrappedAsyncResult
2.CallEndDelegate(IAsyncResult asyncResult)+29
System.Web.Mvc.Async.WrappedAsyncResultBase1.End()+49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+32
public class Cars
{
[Key]
public int CarID { get; set; }
[StringLength(50)]
[Required]
public string CarBrand { get; set; }
[StringLength(250)]
[Required]
public string CarModel { get; set; }
public DateTime CreateDate { get; set; }
}