自动创建产品ID [英] Creating Product ID automatically

查看:83
本文介绍了自动创建产品ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的产品详细信息 Windows窗体中,有一个类似产品ID 的列等等。我对产品ID 列使用了 textBox1 控件。我想在加载事件或验证事件中自动创建产品ID ,如下所示: 02112013 / PID0001 。当用户将第一个产品ID(02112013 / PID0001)和其他详细信息提交给SQL Server 2005数据库时。在用户打开产品详细信息表单时提交第一个产品ID(02112013 / PID0001)后,应在加载事件或验证事件中自动创建产品ID : 02112013 / PID0002

我在textBox1的Validating事件中编写代码如下:

In my Product Details Windows Form, there is a column like Product ID and much more. I used textBox1 control for Product ID column. I want to create the Product ID automatically in the Load Event or Validating Event like this: 02112013/PID0001. When the user will submit the 1st Product ID(02112013/PID0001) and other details to SQL Server 2005 database. After submitting the 1st Product ID(02112013/PID0001) whenever the user will open the Product Details form, the Product ID should be created automatically in the Load Event or Validating Event like: 02112013/PID0002.
I wrote code in the Validating event of the textBox1 like this:

private void textBox1_Validating(object sender, CancelEventArgs e)
       {
           DateTime dt = DateTime.Now;
           int mon = dt.Month;
           int year = dt.Year;
           int day = dt.Day;
           string id1, id2;
           int id;
           SqlConnection con = new SqlConnection(Class1.cs);
           con.Open();
           string query = "select 'Max_No'=max(ProductID) from Product_Details";
           SqlCommand cm = new SqlCommand(query, con);
           SqlDataReader dr = cm.ExecuteReader();
           dr.Read();
           {
               if (dr[0].ToString() == "")
               {
                   id1 = "PID0000";
               }
               else
               {
                   id1 = dr[0].ToString();
               }
               id2 = id1.Substring(3, 4);
               id = Convert.ToInt32(id2);

               if ((id >= 0) && (id < 9))
               {
                   id = id + 1;
                   textBox1.Text = day.ToString() + mon.ToString() + year.ToString() + "/" + "PID000" + id;
               }
               else if ((id >= 9) && (id < 99))
               {
                   id = id + 1;
                   textBox1.Text = day.ToString() + mon.ToString() + year.ToString() + "/" + "PID00" + id;
               }
               else if ((id >= 99) && (id < 999))
               {
                   id = id + 1;
                   textBox1.Text = day.ToString() + mon.ToString() + year.ToString() + "/" + "PID0" + id;
               }
               dr.Close();
           }
           con.Close();
       }



第一个产品ID已成功提交,当我开始第二个产品ID时,它不会创建第二个产品ID。

我无法找出原因。请有人帮助我。


The first Product ID is submitted successfully, when I start for the second it does not create the 2nd Product ID.
I am unable to find out the reason. Please someone help me.

推荐答案

首先使用
select 'Max_No'=max(ProductID) from Product_Details

中选择'Max_No'= max(ProductID)是不安全的在多用户环境中,并且在较大的表上效率不高。



您最好将表格中的列定义为自动增量ID ...请参阅此处的msdn文档 [ ^ ]



关于第二个问题使用您拥有的代码的产品ID ...您是否实际使用每个新ID更新数据库...如果没有,那么您的select语句将始终返回零。

is unsafe in multi-user environments, and not efficient on larger tables.

You would be better off defining a column on your table as an auto-increment ID ... see msdn documentation here[^]

As to your problem with the 2nd Product ID with the code that you have ... do you actually update the database with each new ID ... if not then your select statement is always going to return zero.


这篇关于自动创建产品ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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