删除sqldatabase中的一些记录会从C#中分配自动编号 [英] Deleting some records in sqldatabase re assign autocount number from C#

查看:154
本文介绍了删除sqldatabase中的一些记录会从C#中分配自动编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的Datagridview与SQL数据库文件绑定在一起。我在SQL中维护一个名为slno的列。我正在datagridview中显示slno。

I bounded my Datagridview with SQL database file.I am Maintaining a column in SQL called slno. I am Displaying that slno in datagridview.

When I am selecting a row and deleting the record I want to reassign the autocount numbers to slno.





我尝试了什么:



我试图计算数据库文件中的行并将该数字分配给



What I have tried:

I tried to count the rows in database file and assign that number to

DBCC CHECKIDENT ('tempsales', RESEED, rowcount)\\in my case 5
But error is coming "Parameter 5 is incorrect for the DBCC statement"

推荐答案

如果您尝试使用变量重新播种表格,请在名称或变量之前使用@。例如

If you're trying to use a variable for re-seeding the table, use @ before the name or the variable. For example
DECLARE @rowcount INT;
SET @rowcount = 5;
DBCC CHECKIDENT ('tempsales', RESEED, @rowcount);



或者只是


Or simply

DBCC CHECKIDENT ('tempsales', RESEED, 5);



请注意,通常不需要重新接种身份,因为唯一的目的是给出独特的价值观。永远不要假设值是连续的!


Note that normally reseeding the identity isn't necessary, since the only purpose is to give unique values. Never assume that the values are continuous!


不要。 IDENTITY字段的想法是它是唯一的,但(deliberatly)不一定是顺序的。在实践中,它们是顺序的,但SQL在删除记录时从不重用释放的数字,因此通过该字段引用该行的任何其他系统或表都不会选择错误的数据。

如果您想根据Identity字段为行设置序号,请使用ROW_NUMBER和ORDER BY:

Don't. The idea of an IDENTITY field is that it's unique, but (deliberatly) not necessarily sequential. In practice, they are sequential, but SQL never "reuses" freed numbers when you delete records, so that any other systems or tables that reference the row via the field don't pick up the "wrong" data.
If you want a sequential number for your rows based on the Identity field, then use ROW_NUMBER and ORDER BY:
SELECT ROW_NUMBER() OVER (ORDER BY ID ASC) AS [Sequence Number], * FROM MyTable

这将始终为您提供从1开始的顺序,没有间隙。





是它正在给予,但我希望在datagridview中显示所有内容,以便每次如果用户删除记录,它将重新计算





这是微不足道的!您只需要一个列,并处理RowPrePaint事件:

That will always give you a sequence starting from 1 with no gaps.


"yes It is Giving but I want to display all in datagridview so that every time if user delets the record it will recount"


That's trivial! All you need is a column to put it in, and to handle the RowPrePaint event:

void myDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
    {
    myDataGridView.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
    }


这篇关于删除sqldatabase中的一些记录会从C#中分配自动编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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