按照教材上写的,教材上是2015版,我用的是2019版,调用SQL的语句不知道哪里出错了,一直显示语法错误。
此异常最初是在此调用堆栈中引发的:
System.Data.SqlClient.SqlConnection.OnError(System.Data.SqlClient.SqlException, bool, System.Action<System.Action>)
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(System.Data.SqlClient.TdsParserStateObject, bool, bool)
System.Data.SqlClient.TdsParser.TryRun(System.Data.SqlClient.RunBehavior, System.Data.SqlClient.SqlCommand, System.Data.SqlClient.SqlDataReader, System.Data.SqlClient.BulkCopySimpleResultSet, System.Data.SqlClient.TdsParserStateObject, out bool)
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(string, bool, int, bool)
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(System.Threading.Tasks.TaskCompletionSource<object>, string, bool, int, out bool, bool, bool)
System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
MyDD.DataOperator.ExecSQLResult(string) - 位于 DataOperator.cs
MyDD.Frm_Login.pboxLogin_Click(object, System.EventArgs) - 位于 Frm_Login.cs
System.Windows.Forms.Control.OnClick(System.EventArgs)
System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message, System.Windows.Forms.MouseButtons, int)
...
[调用堆栈已截断]
```c#
if(ValidateInput())
{
string sql = "select count(*) from tb_User where ID= " + int.Parse(txtID.Text.Trim())
+ "and Pwd = '" + txtPwd.Text.Trim() + "'";
int num = dataoper.ExecSQL(sql);
if(num==1)
{
PublicClass.loginID = int.Parse(txtID.Text.Trim());
if(cboxRemember.Checked)
{
dataoper.ExecSQLResult("updata tb_User set Remember=1 where ID= " + int.Parse(txtID.Text.Trim()));
if (cboxAutoLogin.Checked)
dataoper.ExecSQLResult("updata tb_User set AutoLogin=1 where ID= " + int.Parse(txtID.Text.Trim()));
}
else
{
dataoper.ExecSQLResult("updata tb_User set Remember=0 where ID= " + int.Parse(txtID.Text.Trim()));
dataoper.ExecSQLResult("updata tb_User set AutoLogin=0 where ID= " + int.Parse(txtID.Text.Trim()));
}
dataoper.ExecSQLResult("updata tb_User set Flag=1 where ID= " + int.Parse(txtID.Text.Trim()));
Frm_Main frmMain = new Frm_Main();
frmMain.Show();
this.Visible = false;
private static string connString = @"Data Source=0BOJB1X0ET7EZM4;Database=db_MyQQ;User ID=sa;Pwd=123456;";
public static SqlConnection connection = new SqlConnection(connString);
public int ExecSQL(string sql)
{
SqlCommand command = new SqlCommand(sql, connection);
if (connection.State == ConnectionState.Closed)
connection.Open();
int num = Convert.ToInt32(command.ExecuteScalar());
connection.Close();
return num;
}
public int ExecSQLResult(string sql)
{
SqlCommand command = new SqlCommand(sql, connection);
if (connection.State == ConnectionState.Closed)
connection.Open();
int result = command.ExecuteNonQuery();
connection.Close();
return result;
updata 换成 update