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

问题描述

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

推荐答案


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)


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

08-11 05:04