获取最后记录的价值

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

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

更多推荐

获取最后记录的价值

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

发布评论

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

>www.elefans.com

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