I'm trying to create a log table that will log every login attempt.
I get this error:
You cannot add or change a record because a related record is required in table 'MyUser'.
MyUser.Username
is primary key and UserLog.username
is foreign key.
Code:
string query = "INSERT INTO Userlog(username,lTime) VALUES (@1,@2)";
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.Parameters.Add("@1", OleDbType.Char, 20).Value = user;
cmd.Parameters.Add("@2", OleDbType.Char, 20).Value = DateTime.Now.ToString();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
As written by Steve:
Before INSERT INTO Userlog
, check that the username exists in MyUser
. If not, insert there first.
Or in your use case, that was probably someone who mistyped their username. You should log that separately, or not at all.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.