C# 我希望MS Access中最后一条记录的对应ID显示在消息框中

C# 我希望MS Access中最后一条记录的对应ID显示在消息框中,c#,winforms,ms-access,C#,Winforms,Ms Access,我在C#(Visual Studio 15)中使用Access作为数据库。我希望将表单条目(添加记录)保存到Access数据库中,并希望成功保存后记录的相应ID显示在MsgBox中。我尝试了以下方法: private void Form21_Load(object sender, EventArgs e) { try { connection.Open(); checkConnectio

我在C#(Visual Studio 15)中使用Access作为数据库。我希望将表单条目(添加记录)保存到Access数据库中,并希望成功保存后记录的相应ID显示在MsgBox中。我尝试了以下方法:

 private void Form21_Load(object sender, EventArgs e)
      {
          try
          {
              connection.Open();
              checkConnection.Text = "Server Connection, Successful";
              connection.Close();
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error, Server not connected " + ex);
          }
      }
    

    private void Button6_Click(object sender, EventArgs e)
    {
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = "insert into Students_File ([YourNames],[Nationality],[StateOfOrigin],[PlaceOfBirth],[DoB],[HomeAddress],[LastSchools1],[LastClass1],[LastSchools2],[LastClass2],[LastSchools3],[LastClass3],[AdmClass],[CurrentClass],[Guidian],[GuardianContact],[UserName],[PassWord],[Gender],[RegistrationDate]) values('" + YourNames.Text + "','" + Nationality.Text + "','" + StateOfOrigin.Text + "','" + PlaceOfBirth.Text + "','" + DoB.Text + "','" + HomeAddress.Text + "','" + LastSchools1.Text + "','" + LastClass1.Text + "','" + LastSchools2.Text + "','" + LastClass2.Text + "','" + LastSchools3.Text + "','" + LastClass3.Text + "','" + AdmClass.Text + "','" + CurrentClass.Text + "','" + Guidian.Text + "','" + GuardianContact.Text + "','" + UserName.Text + "','" + PassWord.Text + "','" + Gender.Text + "','" + RegistrationDate.Text + "')";
        command.ExecuteNonQuery();
        //MessageBox.Show("Congrats! Your registration, is successful. You may now click close button, then proceed to login");

        command.CommandText = "Select * from Students_File where UserName='" + UserName.Text + "' and PassWord='" + PassWord.Text + "'";
        OleDbDataReader reader = command.ExecuteReader();
        int count = 0;
        while (reader.Read())
        {
            count = count + 1;
        }
        if (count == 1)
        {
            MessageBox.Show("Congrats! Your registration, is successful. You may now click close button, then proceed to login");
            this.Close();
        }
        else if (count > 1)
        {
            MessageBox.Show("Sorry, the chosen username or password is currently existing or picked by another user. Consequently, your registration was not successful. Do please, decide another but a unique one. Thank you");
        }
        connection.Close();
    }

为了解决我的问题,我有以下代码(来自Kevin):


现在,我的挑战是如何在成功创建记录后让ID显示在messagebox中。请考虑我在这篇文章中的最早的代码与此有关。

< P>你可以这样做:< /P>
using (OleDbCommand Command = new OleDbCommand("", conn))
{
    Command.CommandText = "Your big ugly mess - need to change this to parmaters!)";
    Command.Connection.Open();
    Command.ExecuteNonQuery();

    Command.CommandText = "Select @@Identity";
    var intLastID = Command.ExecuteScalar;

    MsgBox("Last id into database = " + intLastID);
}
如前所述,您确实希望更改insert命令—该命令太长,维护起来太困难,并且容易出现代码错误,甚至sql注入错误


但是,现在,您可以使用上述方法获取/获取最后一个ID插入。

它没有回答我的问题。如果新记录添加成功,我需要ID出现在MessageBox中!我想如果你能得到这个id,那么你就可以做任何你需要做的事情。对不起,请问,我如何在上面我的代码的给定链接中操纵答案。我想这是我的弱点。为什么用户需要查看记录ID?它对他们有什么意义吗?虽然它将作为帐户所有者的学生id,但在忘记密码表单中也需要它来恢复忘记的登录详细信息。谢谢。这解决了我的问题
using (OleDbCommand Command = new OleDbCommand("", conn))
{
    Command.CommandText = "Your big ugly mess - need to change this to parmaters!)";
    Command.Connection.Open();
    Command.ExecuteNonQuery();

    Command.CommandText = "Select @@Identity";
    var intLastID = Command.ExecuteScalar;

    MsgBox("Last id into database = " + intLastID);
}