我希望 MS Access 中最后一条记录的相应 ID 出现在消息框内 [重复]
Posted
技术标签:
【中文标题】我希望 MS Access 中最后一条记录的相应 ID 出现在消息框内 [重复]【英文标题】:I want the corresponding ID of last record in MS Access to appear inside a Message Box [duplicate] 【发布时间】:2021-02-23 13:40:04 【问题描述】:我正在使用 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();
【问题讨论】:
它没有回答我的问题。如果新记录添加成功,我需要 ID 出现在 MessageBox 中! 我想如果你能得到 id 然后你可以做任何你需要做的事。 对不起,我如何操作上面代码的给定链接中的答案。我想这是我的弱点。 为什么用户需要查看记录 ID?这对他们有任何意义吗? 虽然它将作为帐户所有者的学生 ID,但在忘记密码表单中也需要它来恢复忘记的登录详细信息。 【参考方案1】:你可以这样做:
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 插入。
【讨论】:
谢谢。这解决了我的问题【参考方案2】:在解决我的询问时,我有以下代码(来自 Kevin):
string query = "Insert Into Categories (CategoryName) Values (?)";
string query2 = "Select @@Identity";
int ID;
string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb";
using (OleDbConnection conn = new OleDbConnection(connect))
using (OleDbCommand cmd = new OleDbCommand(query, conn))
cmd.Parameters.AddWithValue("@CategoryName", OleDbType.Integer);
conn.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = query2;
ID = (int)cmd.ExecuteScalar();
现在,我的挑战是如何在成功创建记录后让 ID 出现在消息框中。请考虑我在这篇文章中最早的代码。
【讨论】:
以上是关于我希望 MS Access 中最后一条记录的相应 ID 出现在消息框内 [重复]的主要内容,如果未能解决你的问题,请参考以下文章