本文介绍了获取最后记录的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在SQL Server中创建具有两个字段Sno(int,identity),Name(Varchar)的表. 表格看起来像 Sno -----文本框1 名称-----文本框2 保存按钮 第一次,文本框1要显示值1. 然后,当我保存记录时,数据要以1,2,3的增量顺序显示.
I want create table with two fields Sno(int,identity),Name(Varchar) in SQL server. Form Looks Like Sno -----Text box1 Name-----Text box2 SAVE Button At first time, text box1 want to display the value 1. Then when i save the record the data want display increment order 1,2,3......
推荐答案我不建议创建序数您的代码记录的编号.您可以使用例如身份 [ ^ ]. 但是,请记住,即使数字是自动递增的,您也可能会有空白,例如,删除记录等.因此,不应将此数字用作有意义的数字,而应将其用作唯一标识符.另一个独特的标识符机制是: uniqueidentifier [ ^ ] I wouldn''t advice to create an ordinal number for your records by your code. You can use for example identity[^]. However, remember that even though the number is auto incremented, you may have gaps for example if a record is deleted and so on. So this number should not used as a meaningful number, just a unique identifier. Another unique identifier mechanism is: uniqueidentifier[^]
Mika的回答非常好! 如果要从表中获取最后一条记录,则有两种方法: 1)使用SELECT TOP(1) AND ORDER BY ... DESC Mika''s answer is very good! If you would like to get the last record from your table, there are 2 ways: 1) Using SELECT TOP(1) AND ORDER BY ... DESC SELECT TOP(1) * FROM YourTable ORDER BY FieldID DESC
2)使用表光标 [ ^ ]
2) Using table Cursor[^]
DECLARE a_cursor SCROLL CURSOR FOR DECLARE @iNum INT SELECT [FieldID] FROM YourTable OPEN a_cursor; -- Fetch the last row in the cursor. FETCH LAST FROM a_cursor INTO @iNum; CLOSE a_cursor; DEALLOCATE a_cursor SELECT @iNum
Dim da As SqlDataAdapter Dim i As Integer da = New SqlDataAdapter("select id from sample order by id desc ", con) Dim ds As DataSet = New DataSet() da.Fill(ds) If ds.Tables(0).Rows.Count > 0 Then i = Convert.ToInt32(ds.Tables(0).Rows(0)(0).ToString()) i = i + 1 TextBox1.Text = i Else i = 1 TextBox1.Text = i End If Then Write simple insert quary like insert into sample values(id,name)
更多推荐
获取最后记录的价值
发布评论