有人知道这件事吗?我收到的代码有问题。
"/"应用程序中的服务器错误。"1"附近的语法不正确。描述:在执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误以及错误在代码中的来源的更多信息。
异常详细信息:System.Data.SqlClient.SqlException:"1"附近的语法不正确。
来源错误:
Line 21: string checkuser = "select count(*) from 1Database where StudentName='" + txtStudentName.Text + "'";
Line 22: SqlCommand cmd = new SqlCommand(checkuser, conn);
Line 23: int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());
Line 24:
Line 25: if (temp == 1)
源文件:C:\Users\orall\Source\repos\DatabaseConnection\DatabaseConnection \loginpage.aspx行:23
堆栈跟踪:
[SqlException(0x80131904(:"1"附近的语法不正确。]System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,操作1 wrapCloseInAction) +2582782 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction(+603430System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用程序HasConnectionLock,布尔异步关闭(+297System.Data.SqlClient.TdsParser.TryRun(RunBehavior RunBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean&dataReady(+4291System.Data.SqlClient.SqlDataReader.TryConsumeMetaData((+59System.Data.SqlClient.SqlDataReader.get_MetaData((+91System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior RunBehavior,String resetOptionsString,布尔值为内部,布尔值用于DescribeParameterEncryption,布尔值应为AlwaysEncrypted缓存(+446System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior RunBehavior、Boolean returnStream、Boolean async、Int32超时、Task&Task、Boolean异步写入、Boolean inRetry、SqlDataReader ds、Boolean describeParameterEncryptionRequest(+2698System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior RunBehavior、Boolean returnStream、String方法、TaskCompletionSource`1完成、Int32超时、任务和任务、布尔缓存和已用缓存、布尔异步写入、布尔inRetry(+1611System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior RunBehavior、Boolean returnStream、String方法(+65System.Data.SqlClient.SqlCommand.ExecuteScaler((+283C:\Users\orall\source\repos\DatabaseConnection\DatabaseConnection \loginpage.Page_Load(对象发送方,EventArgs eSystem.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,EventArgs e(+52System.Web.UI.Control.OnLoad(EventArgs e(+97System.Web.UI.Control.LoadRecursive((+61System.Web.UI.Page.ProcessRequestMain(布尔值包括同步点之前的阶段,布尔值包括异步点之后的阶段(+693
假设Students是一个数据表名称,请尝试此查询:
tring checkuser = "select count(*) from Students where StudentName='" + txtStudentName.Text + "'";
我建议使用参数而不是txtStudentName.Text
IMHO这看起来更好
var temp = (int) cmd.ExecuteScalar();