如何生成新的发布ID [英] how to generate new posting id

查看:136
本文介绍了如何生成新的发布ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成发布ID,例如J12PUN,即"J"是类别,10是发布ID,而"PUN"是城市的前三个字母.... i是生成代码,但是发布ID不是增量...

我的代码是:-

i want to generate posting id like J12PUN that is ''J'' is category ,10 is posting id and ''PUN'' is City''s first three letters....i was generate code but posting id is not increment...

My code is:-

string sel = "SELECT COUNT(Comp_PostPlc_Id) FROM Company_PostPlc_Details";
           SqlCommand cmnd = new SqlCommand(sel, cn);
           int l = Convert.ToInt32(cmnd.ExecuteScalar());
           string sub1;
           if (l <= 0)
               sub1 = "PL1";
           else
           {
        string qery = "select Comp_PostPlc_Id from Company_PostPlc_Details order by Comp_PostPlc_Id desc;";
               SqlCommand cmd = new SqlCommand(qery, cn);

               string zzzz = Convert.ToString(cmd.ExecuteScalar());
               int a = 0;

               a = a + 1;
               string z = Convert.ToString(a);
               sub1 = "PL" + a;

               string query = "select City from User_Details where User_Name= '" + Session["uid"] + "'";
               SqlCommand cmd1 = new SqlCommand(query, cn);

               string kk = Convert.ToString(cmd1.ExecuteScalar());
               string sub = kk.Substring(0, 3);
               sub1 += sub;
           }


所以请帮帮我....


so plz help me....

推荐答案

这种方法存在很多问题-第一个问题是仅从数据库中获取项目计数不会更改项目数,因此只有在您的代码随后将记录添加到数据库时,它才会增加.

其次,更重要的是,它会间歇性地失败,并且在生产中会非常非常糟糕.问题在于,两个不同的用户可以同时获取计数,并将该值用作其ID.在一个或另一个数据库更改之前,计数不会更改,并且假设它们具有正确的ID,它们都将继续进行操作.此外,如果您使用count生成新ID,那么我将删除五个ID是怎么回事?该计数由五个值调回,这意味着接下来的五个ID很可能已经存在.如果要使用这样的方法(强烈建议您不要),则需要通过存储过程来执行此操作,以分配ID并在一条SQL指令中将其返回,并且不要删除任何记录.

坏主意!
There are a number of problems with that approach - the first being that just getting a count of items from a database does not change the number of items, so it will only increment if your code subsequently adds a record to the database.

Secondly, and much more importantly, it will fail intermittently and very very badly in production. The problem is that two different users can get the count at the same time, and use that value as their ID. Until one or the other changes the database the count will not change and they will both proceed assuming they have the correct id. Additionally, if You use teh count to genertate a new ID, what happend is I delete five IDs? The count is set back by five values, which means that the next five IDs very probably will exist already. If you want to use an approach like this (and I strongly recommend you don''t) then you need to do this via a stored procedure to allocate the ID and return it in a single SQL instruction, and don''t delete any records, ever.

Bad, bad idea!


您忘了只选择插入的最高/最后ID.
更改此内容:
You forgot to pick just the top/last ID inserted.
Change this:
string qery = "select Comp_PostPlc_Id from Company_PostPlc_Details order by Comp_PostPlc_Id desc;";




to

string qery = "select TOP 1 Comp_PostPlc_Id from Company_PostPlc_Details order by Comp_PostPlc_Id desc;";



只是一个插件,可以删除以下行:
字符串z = Convert.ToString(a);
将"a"添加到字符串"后,它将自动转换为字符串并将其添加.



Just an addon, following line can be removed:
string z = Convert.ToString(a);
Once you add ''a'' to the ''string'', it automatically converts into string and add it.


这篇关于如何生成新的发布ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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