又是我... 在具有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
更多推荐
插入数据库的最后一条记录
发布评论