Webmatrix QueryValue 调用引发 SQLException "Incorrect syntax near '0'."
Webmatrix QueryValue call raising SQLException "Incorrect syntax near '0'."
以下方法查找 IMEI 代码的 ID
public static int GetId(string imei)
{
int returnVal = 0;
string sqlGetVehicle = "sp_Scalar_VehicleId_For_IMEI @IMEI = {0}";
using (var db = Database.Open("ClientApp"))
{
returnVal = db.QueryValue(sqlGetVehicle, imei);
}
return returnVal;
}
它调用的 sp 设置为 return 单个值。当 SMO 中的 运行 时,程序运行良好。给它一个 IMEI 字符串,如果它在 table 中,它将 return vehicleId。如果不是,则 return 为 0。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: *trim*
-- Create date: 2017-05-12
-- Description: Returns the VehicleId Associated with an IMEI
-- =============================================
ALTER PROCEDURE [dbo].[sp_Scalar_VehicleId_For_IMEI]
@IMEI varchar(20)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @VehicleId int
SELECT @VehicleId = v.VehicleId
FROM Vehicles v
WHERE v.TabletIMEI = @IMEI
-- ISNULL() in query wasn't working
IF @VehicleId IS NULL
BEGIN
SET @VehicleId = 0
END
SELECT @VehicleId
END
当从代码中 运行 时,过程抛出 System.Data.SqlClient.SqlException: Incorrect syntax near '0'.
它在我删除 ISNULL() 检查之前以及在我用 IF 语句替换它之后抛出这个。此错误的根本原因是什么?
[SqlException (0x80131904): Incorrect syntax near '0'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +2442126
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5736904
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +628
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +3731
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +58
System.Data.SqlClient.SqlDataReader.get_MetaData() +89
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +379
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) +2026
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +375
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
System.Data.SqlClient.SqlCommand.ExecuteScalar() +271
WebMatrix.Data.Database.QueryValue(String commandText, Object[] args) +111
*trim*.Forms.*trim*.Vehicle.GetId(String imei) in C:\Users\*trim*\Documents\Visual Studio 2017\WebSites\*trim*\App_Code\Vehicle.cs:258
ASP.Functions.UpdateSessionVehicle() in C:\Users\*trim*\Documents\Visual Studio 2017\WebSites\*trim*\App_Code\Functions.cshtml:148
ASP._Page_Default_cshtml.Execute() in C:\Users\*trim*\Documents\Visual Studio 2017\WebSites\*trim*\Default.cshtml:8
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +198
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +69
System.Web.WebPages.WebPage.ExecutePageHierarchy() +131
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +116
Webmatrix 的占位符似乎是 @0
而不是 {0}
。此外,要实际执行存储过程,应使用 "Exec"
调用它。
考虑到这一点,命令文本应定义为:
string sqlGetVehicle = "Exec sp_Scalar_VehicleId_For_IMEI @IMEI = @0";
根据 this post 另一种方式可能是:
returnVal = db.Query("Exec sp_Scalar_VehicleId_For_IMEI", imei);
以下方法查找 IMEI 代码的 ID
public static int GetId(string imei)
{
int returnVal = 0;
string sqlGetVehicle = "sp_Scalar_VehicleId_For_IMEI @IMEI = {0}";
using (var db = Database.Open("ClientApp"))
{
returnVal = db.QueryValue(sqlGetVehicle, imei);
}
return returnVal;
}
它调用的 sp 设置为 return 单个值。当 SMO 中的 运行 时,程序运行良好。给它一个 IMEI 字符串,如果它在 table 中,它将 return vehicleId。如果不是,则 return 为 0。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: *trim*
-- Create date: 2017-05-12
-- Description: Returns the VehicleId Associated with an IMEI
-- =============================================
ALTER PROCEDURE [dbo].[sp_Scalar_VehicleId_For_IMEI]
@IMEI varchar(20)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @VehicleId int
SELECT @VehicleId = v.VehicleId
FROM Vehicles v
WHERE v.TabletIMEI = @IMEI
-- ISNULL() in query wasn't working
IF @VehicleId IS NULL
BEGIN
SET @VehicleId = 0
END
SELECT @VehicleId
END
当从代码中 运行 时,过程抛出 System.Data.SqlClient.SqlException: Incorrect syntax near '0'.
它在我删除 ISNULL() 检查之前以及在我用 IF 语句替换它之后抛出这个。此错误的根本原因是什么?
[SqlException (0x80131904): Incorrect syntax near '0'.] System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +2442126 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5736904 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +628 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +3731 System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +58 System.Data.SqlClient.SqlDataReader.get_MetaData() +89 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +379 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) +2026 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +375 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53 System.Data.SqlClient.SqlCommand.ExecuteScalar() +271 WebMatrix.Data.Database.QueryValue(String commandText, Object[] args) +111 *trim*.Forms.*trim*.Vehicle.GetId(String imei) in C:\Users\*trim*\Documents\Visual Studio 2017\WebSites\*trim*\App_Code\Vehicle.cs:258 ASP.Functions.UpdateSessionVehicle() in C:\Users\*trim*\Documents\Visual Studio 2017\WebSites\*trim*\App_Code\Functions.cshtml:148 ASP._Page_Default_cshtml.Execute() in C:\Users\*trim*\Documents\Visual Studio 2017\WebSites\*trim*\Default.cshtml:8 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +198 System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +69 System.Web.WebPages.WebPage.ExecutePageHierarchy() +131 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78 System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +116
Webmatrix 的占位符似乎是 @0
而不是 {0}
。此外,要实际执行存储过程,应使用 "Exec"
调用它。
考虑到这一点,命令文本应定义为:
string sqlGetVehicle = "Exec sp_Scalar_VehicleId_For_IMEI @IMEI = @0";
根据 this post 另一种方式可能是:
returnVal = db.Query("Exec sp_Scalar_VehicleId_For_IMEI", imei);