插入数据库的最后一条记录

编程入门 行业动态 更新时间:2024-10-27 10:25:37
本文介绍了插入数据库的最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

又是我... 在具有ComboBox的表单上,我将组合框与数据库中的某些数据绑定,接下来,组合框具有按钮插入新数据" 这是打开表单并按保存"按钮后的更好方法,在我的主表单的组合框中,可以自动选择要插入的新记录 我用这样的东西.

Me again... On a form i have a ComboBox, i bind the combobox with some data from a database, next the combobox i have a button "insert new data" which is the better way after in open the form and press the Save button, in my combobox in the main form to be auto selected the new record inserted I use somthing like this.

private void GetNewId() { int newID = 0; Program.Connection.CommandText = "SELECT MAX(ClientId) AS ClientId FROM DateClientiAmanet"; DataTable Table = new DataTable(); Program.Connection.FillDataTable(Table, true); newID = Convert.ToInt32(Table.Rows[0]["ClientId"]); InformatiiDespreClient(); cboNumeClient.SelectedValue = newID.ToString(); }

可以,可以,但是我在互联网上搜索,"MAX(ClientId)"不是很好. 谢谢. 代码块已从答案中移出[/EDIT]

Is ok,works,but i search on the internet and the "MAX(ClientId)" is not so good. thanks. Code block moved from answer[/EDIT]

推荐答案

-插入记录 - Insert record INSERT INTO tblBlah1 (Col1,Col2,Col3) VALUES (Val1,Val2,Val3);

-检索ID

- Retrieve id

SELECT SCOPE_IDENTITY()

-重新填充您的组合 -将selectedvalue设置为新ID

- Repopulate your combo - Set selectedvalue to the new id

更多推荐

插入数据库的最后一条记录

本文发布于:2023-10-20 12:58:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1511032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!