首页 > 解决方案 > 我希望 MS Access 中最后一条记录的相应 ID 出现在消息框内

问题描述

我正在使用 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();
    }

标签: c#winformsms-access

解决方案


你可以这样做:

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);
}

如前所述,您确实希望更改该插入命令 - 它太长、太难维护,并且容易出现代码错误甚至 sql 注入。

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


推荐阅读