插入数据库的最后一条记录 [英] last record inserted in database

查看:94
本文介绍了插入数据库的最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

又是我...

在具有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


这篇关于插入数据库的最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆