获取最后记录的价值 [英] Get value of Last record

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

问题描述

我想在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)


这篇关于获取最后记录的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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