想要随机生成没有。通过某种特定模式编码。 [英] want to generate random no. through coding in some specific pattern.

查看:71
本文介绍了想要随机生成没有。通过某种特定模式编码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#.net并将ms-access作为Windows应用程序的后端。我想以特定模式自动生成一个数字(例如AB / 2014/2/2)。

这里AB:将是一些固定的字符串

2014:今年

2:月

5:auto生成号码(将在提交表格后增加)



每当有人填写表格并点击提交按钮然后这个没有。必须生成。这个号码的最后一位数字。是序列号。检查前一个号码后会增加。

这个号码。将作为数据库中的主键。



有人可以帮我解决这个问题吗?我没有得到如何编码.....

I am working in C#.net and taking ms-access as backend for windows application. I want to auto generate one number in a particular pattern(eg. AB/2014/2/2).
Here AB: will be some fixed string
2014: present year
2:month
5:auto generate no.(will increment after submission of form)

whenever some one will fill the form and will click on submit button then this no. has to be generated. last digit in this no. is sequence no. which will be incremented after checking previous no.
this no. will act as a primary key in database.

can somebody help me out with this? I am not getting how to code this.....

推荐答案

试试这样,

做一些小作业..



Try like this,
do some little homework..

public string GenerateNext(string text)
   {
       string check = string.Format("{0}/{1}/{2}/",text,DateTime.Now.Year,DateTime.Now.Month);
       // query the DB to filter the primary key by passing the 'check' as Start with 'Like' Search
       // and return only the primary key values in a list of string or datatable
       List<string> lstValues = new List<string>();
      // lstValues =   GetDBValues(check); // you need to write logic for this..
       if (lstValues.Count == 0)
           return check + "0";
       else
       {
          int nextID =  lstValues.Select(k=>  k.Replace(check,"")).Select(k0=> Convert.ToInt32(k0)).Max() + 1;
           return check + nextID.ToString();
       }


   }


在数据库中使用标识值,并使用计算列生成整个项目。

假设您有一个名为Ident的Identity字段,一个固定的文本字段调用Prefix,以及一个名为InsertDate的DateTime字段:

Use an identify value in the database, and a computed column to generate the whole item.
Assuming you have an Identity field called Ident, a fixed text field call Prefix, and a DateTime field called InsertDate:
[ID]  AS (((((([Prefix]+'/')+CONVERT([varchar],datepart(year,[InsertDate]),0))+'/')+CONVERT([varchar],datepart(month,[InsertDate]),0))+'/')+CONVERT([varchar],[Ident],0))


这样的东西? (我正在使用文本框试试)



Something like this? (i'm using a textbox just to try)

string number = "";
string[] split = this.textBox1.Text.Split('/'); //I suppose here you'll get the last generated number from you database
number += split[0] + "/";

if (split[1] == DateTime.Now.Year.ToString("0000") && split[2] == DateTime.Now.Month.ToString("0"))
{
      //if actual year/month is the same as last generated number,generate next secuencial number
      number += split[1] + "/" + split[2] + "/" + (int.Parse(split[3]) + 1).ToString();
}
else
{      
      //month has changed,start with 0
      number += DateTime.Now.Year.ToString("0000") + "/" + DateTime.Now.Month.ToString("0") + "/0";
}
this.textBox1.Text = number;


这篇关于想要随机生成没有。通过某种特定模式编码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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