查询表达式"@ID = @@ IDENTITY"中的语法错误(缺少运算符) [英] Syntax error (missing operator) in query expression '@ID = @@IDENTITY'
问题描述
我从此处
但是,正如主题标题所说,我在btnLogin方法中收到错误,该错误与我之前的问题有关,但我确实是同一问题,我认为呢?我之所以这样说,是因为我以前的问题没有给我带来错误.在此先感谢您能在这里为我提供帮助.
But as subject title says I am receiving error in the btnLogin method, related to my previous question but really the same problem, I think? I say this because I was not getting error from my pervious question. Thanks in advance if anyone can help me here.
访问2003 VS 2010 c#
access 2003 VS 2010 c#
这是我的btnLogin方法
This is my btnLogin method
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO LoginLogTable (UserName, LoggedInDate, LoggedInTime) VALUES (@UserName, @LoggedInDate, @LoggedInTime)";
cmd.CommandText = "SELECT @ID = @@IDENTITY";
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
cmd.Parameters.AddWithValue("@LoggedInDate", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("@LoggedInTime", DateTime.Now.ToString("HH:mm"));
cmd.Connection = myCon;
myCon.Open();
cmd.ExecuteNonQuery();
myCon.Close();
这是我的btnLogOut方法.
This is my btnLogOut method.
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = " UPDATE [LoginLogTable] SET [LoggedOutDate] = ?, [LoggedOutTime] = ? WHERE ID = @ID";
cmd.Parameters.AddWithValue("@LoggedOutDate", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("@LoggedOutTime", DateTime.Now.ToString("HH:mm"));
cmd.Connection = myCon;
myCon.Open();
cmd.ExecuteNonQuery();
代码的用途是记录用户登录的日期和时间以及用户注销的日期和时间.
The purpose of the code the code is record the date and time when user logs in and the date and time when the user logs out.
更新2 我也尝试过...
Update 2 I have tried this as well...
cmd.CommandText = "INSERT INTO LoginLogTable (UserName, LoggedInDate, LoggedInTime) VALUES (@UserName, @LoggedInDate, @LoggedInTime)" + "SELECT @ID = @@IDENTITY";
然后我收到了...的错误
Then I am receiving error of...
SQL语句末尾缺少分号(;).
Missing semicolon (;) at end of SQL statement.
更新3
cmd.CommandText = "INSERT INTO LoginLogTable (UserName, LoggedInDate, LoggedInTime) VALUES
(@UserName, @LoggedInDate, @LoggedInTime)"; "SELECT @ID = @@IDENTITY";
我已经放置了;而且我得到了...的错误
I have placed a ; and I am getting error of...
仅赋值,调用,递增,递减和新对象表达式可以用作语句
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
寻找其他解决方案为我的其他成员提供了
Looking at other solutions provided my other members
更新4
我认为这里的一些建议可能仅适用于SQL Server.我正在使用MS Access2003.
I think some of the advise here may only work for SQL Server. I am using MS Access 2003.
更新5
cmd.CommandText = "INSERT INTO LoginLogTable (UserName, LoggedInDate, LoggedInTime) VALUES
(@UserName, @LoggedInDate, @LoggedInTime); SELECT @ID = @@IDENTITY";
接收错误...
SQL语句结束后找到字符.
Characters found after end of SQL statement.
推荐答案
似乎您的代码中可能有错误.您将cmd的文本设置为第一行,然后将其完全替换为第二行:
It appears that you may have an error in your code. You set the Text of the cmd to the first line, then completely replace it with the second one:
cmd.CommandText = "INSERT INTO LoginLogTable (UserName, LoggedInDate, LoggedInTime) VALUES (@UserName, @LoggedInDate, @LoggedInTime)";
cmd.CommandText = "SELECT @ID = @@IDENTITY";
因此,您实际上只在执行:
So you are effectively only executing:
cmd.CommandText = "SELECT @ID = @@IDENTITY";
这篇关于查询表达式"@ID = @@ IDENTITY"中的语法错误(缺少运算符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!