简体   繁体   English

查询表达式'@ID = @@ IDENTITY'II中的语法错误(缺少运算符)

[英]Syntax error (missing operator) in query expression '@ID = @@IDENTITY' II

Access 2003 存取2003

VS 2010 C# VS 2010 C#

As subject title says I am having problems with. 正如主题标题所说,我遇到了问题。 This is related to my previous question I asked, Here . 这与我之前在这里问过的问题有关。 I hope the mod's will be OK with this thread but I am not sure. 我希望该线程的mod可以正常运行,但我不确定。

Martin Parkin advised not to close the connection between Insert and Select when using @@Identity with C# and MS-Access. 当在C#和MS-Access中使用@@Identity时,Martin Parkin建议不要关闭Insert和Select之间的连接。 I thought I got it working until I discovered that was not the case. 我以为直到发现事实并非如此,我才开始努力。 To be honest I don't know how to solve this issue. 老实说,我不知道如何解决这个问题。 So if anyone can help me I would appreciate it. 因此,如果有人可以帮助我,我将不胜感激。

This is my btnLogin method.. 这是我的btnLogin方法。

        cmd.CommandText = "INSERT INTO LoginLogTable (UserName, LoggedInDate, LoggedInTime) VALUES (@UserName, @LoggedInDate, @LoggedInTime)";
        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();

        cmd.CommandText = "SELECT @ID = @@IDENTITY";
     // cmd.Parameters.AddWithValue("@ID", OleDbType.WChar); << tried this, unsuccessful
        int id = (int)cmd.ExecuteScalar(); // getting the same error? 
        myCon.Close();

This is my btnLogOut method... 这是我的btnLogOut方法...

        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;

         int id = 0;
        cmd.CommandText = " UPDATE [LoginLogTable] SET [LoggedOutDate] = @LoggedOutDate, [LoggedOutTime] = @LoggedOutTime WHERE [ID] = @ID";
        cmd.Parameters.AddWithValue("@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();
        Close();

Or 要么

In the btnLogin method if I do 如果我在btnLogin方法中

cmd.CommandText = "SELECT @ID = @@IDENTITY";

and hide the cmd.ExecuteNonQuery(); 并隐藏cmd.ExecuteNonQuery(); after it. 之后。 Then date and time will get logged in the database but the date and time will not get saved in the database, for logging out. 然后日期和时间将被记录在数据库中,但日期和时间将不会被保存在数据库中以进行注销。

I am not sure if the problem is with btnLogin method or btnLogOut method, or both. 我不确定问题是否出在btnLogin方法或btnLogOut方法,或两者​​都有。

Working Solution 工作方案

Originally I did 我原来是

 cmd.CommandText = " UPDATE [LoginLogTable] SET [LoggedOutDate] = @LoggedOutDate,   
 [LoggedOutTime] = @LoggedOutTime WHERE [ID] = @ID";
 cmd.Parameters.AddWithValue("@ID", id);
 cmd.Parameters.AddWithValue("@LoggedOutDate", DateTime.Now.ToShortDateString());
 cmd.Parameters.AddWithValue("@LoggedOutTime", DateTime.Now.ToString("HH:mm"));

Then I did this 然后我做了

 cmd.CommandText = " UPDATE [LoginLogTable] SET [UserName] = @UserName, [LoggedOutDate] = 
 @LoggedOutDate, [LoggedOutTime] = @LoggedOutTime WHERE ID = @ID";
 cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
 cmd.Parameters.AddWithValue("@LoggedOutDate", DateTime.Now.ToShortDateString());
 cmd.Parameters.AddWithValue("@LoggedOutTime", DateTime.Now.ToString("HH:mm"));
 cmd.Parameters.AddWithValue("@ID", id);

Thanks to D Stanley and Gord Thompson. 感谢D Stanley和Gord Thompson。

The @ID variable does not persist in the database the way you seem to think it does. @ID变量不会像您认为的那样保留在数据库中。 It will go out of scope when the connection is closed (possibly sooner). 当连接关闭时(可能更快),它将超出范围。 I would advise that you store the new identity within your application instead: 我建议您将新身份存储在应用程序中:

Assuming these are button handlers that are methods on the form, you could store the ID as a property of the form: 假设这些是表单上的方法的按钮处理程序,则可以将ID存储为表单的属性:

    // somewhere in the form definition:
    private int ID {get; set;}
    ...

    cmd.CommandText = "SELECT @@IDENTITY";
    int id = (int)cmd.ExecuteScalar();
    this.ID = id;

Then use the ID in your Logout method: 然后在您的注销方法中使用ID:

    // get the id from the form
    int id = this.ID;

    cmd.CommandText = " UPDATE [LoginLogTable] SET [LoggedOutDate] = @LoggedOutDate, [LoggedOutTime] = @LoggedOutTime WHERE [ID] = @ID";
    cmd.Parameters.AddWithValue("@ID", id);
    cmd.Parameters.AddWithValue("@LoggedOutDate", DateTime.Now.ToShortDateString());
    cmd.Parameters.AddWithValue("@LoggedOutTime", DateTime.Now.ToString("HH:mm"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM