自动生成代码 [英] Auto generation of code

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

问题描述

SqlConnection con = new SqlConnection(Data Source = Abc // SQLEXPRESS; Initial Catalog = Demo; Integrated Security = True);

SqlCommand cmd = new SqlCommand();



string maxid =E00;

public Form2()

{

InitializeComponent( );

}



private void Form2_Load(object sender,EventArgs e)

{



con.Open();

cmd.Connection = con;

cmd.CommandText =select MAX(Emp_Id)来自EmpTbl;

maxid = cmd.ExecuteScalar()。ToString();

con.Close();

maxid = maxid。子串(2);

maxid =(Convert.ToInt32(maxid)+ 1).ToString();



}



i在maxid = maxid.Substring(2)中出错;说startIndex的行不能大于字符串的长度。



我怎样才能摆脱它...

帮助我..

或者有一个人用于自动生成Id的代码,其中包含字母数字值,如E001,E002,E003,然后继续.. Plz发送给我gauravjadhav351@gmail.com。

SqlConnection con = new SqlConnection("Data Source=Abc//SQLEXPRESS;Initial Catalog=Demo;Integrated Security=True");
SqlCommand cmd = new SqlCommand();

string maxid = "E00";
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{

con.Open();
cmd.Connection = con;
cmd.CommandText = "select MAX(Emp_Id) from EmpTbl";
maxid = cmd.ExecuteScalar().ToString ();
con.Close();
maxid = maxid.Substring (2);
maxid = (Convert.ToInt32(maxid) + 1).ToString ();

}

i got error in maxid = maxid.Substring (2); line saying startIndex cannot be larger than length of string.

How can i get out of it...
help me..
Or some one having code for auto generation of Id which contains alphanumeric values like "E001", "E002","E003" and go on.. Plz send me on gauravjadhav351@gmail.com.

推荐答案

CODE在C#中自动生成字母数字ID



CODE to auto generate an alpha numeric id in C#

public string GetLatestOrderId()
{
    string ReceivedId = string.Empty;
    string displayString = string.Empty;
    String query = "SELECT MAX(OrderReceivedNo) FROM [Order_Received]";
    String data = DataManager.RunExecuteScalar(ConnectionString.Constr, query);
    ReceivedId = data;
    if (string.IsNullOrEmpty(ReceivedId))
    {
        ReceivedId = "OR0000";//It is the start index of alpha numeric value
    }
    int len = ReceivedId.Length;
    string splitNo = ReceivedId.Substring(2, len - 2);//This line will ignore the string values and read only the numeric values
    int num = Convert.ToInt32(splitNo);
    num++;// Increment the numeric value
    displayString = ReceivedId.Substring(0, 2) + num.ToString("0000");//Concatenate the string value and the numeric value after the increment
    return displayString;
}














OR

public static IEnumerable<string> Numbers()
{
    return Enumerable.Range(0xA0000, 0xFFFF9 - 0xA0000 + 1)
        .Select(x => x.ToString("X"));
}
You could also have an id generator class:

public class IdGenerator
{
    private const int Min = 0xA0000;
    private const int Max = 0xFFFF9;
    private int _value = Min - 1;

    public string NextId()
    {
        if (_value < Max)
        {
            _value++;
        }
        else
        {
            _value = Min;
        }
        return _value.ToString("X");
    }
}


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

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