c# - 我希望 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();
}
解决方案
你可以这样做:
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 插入。
推荐阅读
- php - 使用 certbot 安装 ssl 证书时如何解决未经授权的错误?
- html - 树形图 - 将最后一个孩子移动到 html/css 中的一级
- java - 为什么我在使用休眠时总是遇到同样的错误:ORA-00979:不是 GROUP BY 表达式?
- javascript - 传递给其他刀片后,我的 id 值未从第一个数字中读取数字 0
- css - 如何创建自定义样式的键盘快捷键
- javascript - 通过在 Elasticsearch 中优先考虑索引来查询多个索引?
- web-services - 如何才能实现松耦合“面向服务的架构”(SOA)?解释使用合适的解决方案
- javascript - 是否可以从 Redux 存储中检索特定评论评论的状态
- javascript - 将二进制数据传输到服务器时出错
- c# - 覆盖率问题:noescape:资源数据视图未关闭或保存在 Table.get 中