VB.net帮助数据库中的字段增加1 [英] Help with VB.net incrementing a field in a database by 1

查看:87
本文介绍了VB.net帮助数据库中的字段增加1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

有人可以帮我解决以下问题吗?

我正在将Visual Web Developer Express与Microsoft的StarterSite结合使用,以开始学习少量的Web开发.到目前为止,我一直在尝试使用一个网页将数据输入到几个表的字段中(使用SQL Server Compact),但是遇到了一个小障碍.

我有一小页,上面有一个按钮.当我单击按钮时,我希望代码查看表中的字段(调用字段"InvoiceNumber"并调用表"InvoiceTable"),取存储的最后一个数字,将其递增"1",然后存储将其放回发票表"中,并在页面上显示新的发票号.

当前该按钮称为新发票",因此,其想法是单击该按钮将创建一个新的发票,其编号比表中的最新编号新.

如果有人可以帮助我,我将永远感激不已.

非常感谢

Hi all,

Would someone mind helping me with the following please?

I''m using Visual Web Developer Express, with Microsoft''s StarterSite in order to start to learn a small amount of web development. I''ve been practising getting a webpage to enter data into fields in a couple of tables so far (using SQL Server Compact), but have run into a small roadblock.

I have a small page, with a button on it. When I click the button I''d like the code to look at a field in a table (call the field "InvoiceNumber" and call the table "InvoiceTable"), take the last number stored, increment it by "1" then store it back in the "InvoiceTable" and display the new invoice number on the page.

Currently the button is called "New Invoice", so the idea is that clicking this button is going to create a new invoice, with a number one newer than the latest one in the table.

If someone could help I''d be eternally grateful.

Many thanks

推荐答案

可以使用存储的proc
轻松完成此操作
This could easily be done with a stored proc

DECLARE @invnum INT;
SELECT @invnum = MAX(InvoiceNumber) FROM InvoiceTable

@invnum = @invnum + 1

INSERT INTO InvoiceTable (InvoiceNumber) VALUES (@invnum)

RETURN @invnum





using(SqlCommand cmd = new SqlCommand("proc name", conection))
{
  int invnum = (int)cmd.ExecuteScalar();
}



如果该字段是数字,则可以通过创建一个自动编号字段并只允许数据库增加该值来获得相同的效果.



If the field is a number you could get the same affect by creating a autonumber field and just allowing the database to increment the value.


是的,永远不要在真实数据库中执行此操作.由于多个用户执行完全相同的操作的问题,因此AutoId列对此更好.如果他们俩都运行代码以获取最后的号码,则他们各自将得出相同的结果,并尝试创建相同的发票号码.显然,这将是一件非常糟糕的事情.
Yoy don''t EVER do this in a real database. AutoId columns are better for this because of the problems of multiple users doing the exact same operation. If they both run the code to get the last number, they will each come up with the same result and try to create the same invoice number. Obviously this would be a very bad thing.


我将更改您的方法,并使用IDENTITY列.我将阅读这些文章

了解身份列 [ SQL SERVER –添加或删除身份属性列 [
I would change your approach and use an IDENTITY column. I would have a read of these articles

Understanding Identity Columns[^]
SQL SERVER – Add or Remove Identity Property on Column[^]


这篇关于VB.net帮助数据库中的字段增加1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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