在需要时增加主键 [英] Increment the primary key when needed

查看:107
本文介绍了在需要时增加主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

我只想再问一遍:)

我在如何增加唯一代码方面遇到问题,这样它就不会再次重复.

例如最后一条记录的唯一代码是01,我想为下一条记录将其设置为2,以便设置主键. :)

感谢您的帮助:)

我尝试了MAX,但无法继续执行代码:(

hello.

I just want to ask again :)

I''m having a problem regarding how can I increment the unique code, so that it will not repeat again.

e.g. the last record''s unique code is 01, I want to make it 2 for the next record so that the primary key will be set. :)

thanks for helping :)

I tried the MAX, but I can''t continue the code :(

Dim connection As OleDbConnection
       connection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\emong\Documents\Visual Studio 2008\WebSite1\Database\dbLupon.mdb")

       Dim command As OleDbCommand
       command = New OleDbCommand("SELECT MAX(UNIQUE_C) AS myMax FROM tblLupon", connection)




OP的更新:




UPDATE from OP:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       Dim connection As OleDbConnection
       connection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\emong\Documents\Visual Studio 2008\WebSite6\DataBase\Database1.mdb")
       Dim x As Integer
       Dim command As OleDbCommand
       command = New OleDbCommand("SELECT MAX(Cat_Id) + 1 AS myMax FROM myTable", connection)
       connection.Open()
       If GridView1.Rows.Count = 0 Then
           Label1.Text = "01"
       Else

       End If

       Dim DataReader As OleDbDataReader
       DataReader = command.ExecuteReader()
       GridView1.DataSource = DataReader
       GridView1.DataBind()
       connection.Close()



我需要else代码.我不知道如何获取myMax,然后将其放在标签上.

感谢



The else code is I need. I don''t know how can I get the myMax then put it on a label.

Thanks

推荐答案

其他人没有指出的是,该代码在多用户环境中既不稳定也不可用.您不会从代码中生成自动编号字段.您让数据库来做.为什么?当两个客户端几乎同时执行您的SELECT语句时会发生什么?您的代码可以让两个客户端生成相同的唯一" ID.

正确的方法是在数据库中创建一条新记录,至少要用最少的数据才能完成一条记录.数据库将自己分配此ID,并且不会出现您引入的并发问题.然后,您可以通过阅读并实现 [
What everyone else hasn''t pointed out is that this code is NOT stable nor usable in a multi-user environment. You do NOT generate autonumber fields from your code. You let the database do it. Why? What happens when two clients execute your SELECT statement at nearly the same time? Your code can have two clients generate the same "unique" ID.

The proper way to do this would be to created a new record in the database with at least the minimal data to complete a record. The database will assign this ID itself, and without the concurrency problem you introduced. You can then retrieve the ID of this record by reading and implementing this[^].


据我所知理解,我认为您在递增时遇到问题,并且我假设代码是整数类型,但是如果不是,则可以将其强制转换为Integer:因此可能的解决方案是:


...
...
...
command = New OleDbCommand("SELECT MAX(UNIQUE_C)+1 as myMax FROM tblLupon",connection)
As far as I understood, I think you are having problem in incrementing, and I am assuming that the code is in integer type but if it is not, then you can cast it to Integer: so a possible solution is:


...
...
...
command = New OleDbCommand("SELECT MAX(UNIQUE_C) + 1 AS myMax FROM tblLupon", connection)


您已设置了自动增量的主键.

首先这样做.
Have you set your primary key to autoincrement.

First do that.


这篇关于在需要时增加主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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