无法检索Microsoft Access自动编号值 [英] Unable to Retrieve Microsoft Access Autonumber Values
问题描述
Access 2003
Access 2003
VS 2010 C#
VS 2010 C#
正如主题标题所说,我对此有疑问.当它应在当前ID中标记日期和时间时,它将创建一个新字段来打印日期和时间.我也尝试过UPDATE命令参数,但没有成功.
As subject title says I am having a problem with this. It's creating a new field to print date and time when it should be stamping the date and time in the current ID. I have also tried UPDATE command parameter without success.
我有另一种方法(btnloggedIn),它可以保存用户名,登录日期和登录时间.这可以正常工作.我创建了另一个有问题的方法(btnLoggedOut).目的是将登录用户的注销日期"和注销时间"保存在Access的访问"列中,登录时会在其中创建自动ID.
I have a different method (btnloggedIn) which saves Usernames, Logged In Date and Logged In Time. This works as it should be. I have created another method (btnLoggedOut) which I am having problems with. The purposes is to save Logged Out Date and Logged Out Time when user who logged out, in the came column in Access where Auto ID is created when logged in.
表名-LoginTable
Table Name - LoginTable
>
FieldName Data Type
UserName Text
Password Text
表名-LoginLogTable
Table name - LoginLogTable
FieldName Data Type
ID AutoNumber
UserName Text
LoggedInDate Date/Time
LoggedInTime Date/Time
LoggedOutDate Date/Time
LoggedOutTime Date/Time
ID为PK.它一对多的关系.登录的用户可以拥有有关日期和时间详细信息的许多详细信息
ID is PK. Its one to many relationship. User who logs in can have many details about the date and time details
如果有人可以在这里帮助我,我将不胜感激.
If anyone can help me here I would be grateful.
private void btnLogOut_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = " UPDATE [LoginLogTable] SET [LoggedOutDate] = ?, [LoggedOutTime] = ?
WHERE 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();
}
这是btnLogin方法的部分代码...
This the partial code for btnLogin method...
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
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();
myCon.Close();
推荐答案
如果在用户单击注销"按钮时执行SELECT @@IDENTITY
查询,则您不太可能会得到想要的值.打算在创建记录的INSERT
之后立即调用SELECT @@IDENTITY
(在这种情况下,当用户在中登录时).然后,您可以将该值存储在应用程序中,并在用户注销时使用它选择相同的记录.
If you execute a SELECT @@IDENTITY
query when the user clicks the "Log out" button you'll not likely get the value you're hoping for. SELECT @@IDENTITY
is intended to be called immediately after the INSERT
that creates the record (in this case, when the user logs in). You can then stash that value away in your application and use it to select that same record when the user logs out.
如果您的应用程序插入了其他记录(在其他表中),导致创建了新的Identity(也称为"AutoNumber")值,则SELECT @@IDENTITY
将返回这些值中的最新值.因此,只需在用户登录时获取@@IDENTITY
值,然后将其保存以备用户再次注销时使用即可.
If your application inserts any other records (in other tables) that cause a new Identity (a.k.a. "AutoNumber") value to be created then SELECT @@IDENTITY
will return the most recent one of those values. So, just grab the @@IDENTITY
value when the user logs in and save it for when the user logs out again.
这篇关于无法检索Microsoft Access自动编号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!