How to automatically generate alpha-numeric identifier in C # using MS Access database?

I have a table called emp. The table contains three columns empid, enameand salary. I have to store empidas auto-generated identifiers, for example: E001, E002, E003. How to do it with C #?

+3
source share
5 answers

If this is not homework:

  • Think about the limitations that this ID naming scheme places on your data.

  • , . -, E , empid INT E? , , . GUID (. .NET System.Guid), ( ). empid.

:

  • ( ) , ?

  • .NET, ; 1"001".
    .NET, , . "001"1.
    Ennn .

  • :
    , , .
    , (.. 1). , .


:

E...:

int numericPart = 123;   // <-- example input

string empid = numericPart.ToString("'E'000");

E...
( , ):

using System.Text.RegularExpressions;
...

string empid = "E123";   // <-- example input

var empidPattern = new Regex(@"^E(?<NumericPart>\d{3})$");

if (empidPattern.IsMatch(empid))
{
    // extract numeric part from key and convert it to int:
    int numericPart = Int32.Parse(
                          empidPattern.Match(empid).Groups["NumericPart"].Value);
}
else
{
    // 'empid' does not represent a valid key (wrong format)
}

:

using System.Data;

// create a new collection that will hold all empid keys in emp
var empidList = new List<string>();

// open a database connection -- you need to add appropriate code here:
using (IDbConnection db = ...)
{
    // define the query for retrieving all empid keys:
    IDbCommand getEmpidList = db.CreateCommand();
    getEmpidList.CommandType = CommandType.Text;
    getEmpidList.CommandText = "SELECT empid FROM emp ORDER BY empid ASC";

    // execute the query and transfer its result set to the collection:
    using (IDataReader reader = getEmpidList.ExecuteReader())
    {
        while (reader.Read())
        {
            empidList.Add(reader.GetString(0));
        }
    }
}

, , , , .

+2

int "E" .

+1
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;
}
+1
source

This is my answer

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;
        }
0
source
        Sql DataAdapter sda = new SqlDataAdapter("select * from emp", con);
        Dataset ds = new DataSet();
        sda.Fill(ds, "emp_id");
        string EmpId;

        //this condition checks that table is empty
        // or not if table is empty that bill id is E0001 other wise else par is run
        if (ds.Tables[0].Rows.Count == 0)
        {
            EmpId = "E0001";
        }
        else
        {
            string s = ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["emp_id"].ToString();
            //retriving empid column last cell data.
            int len = s.Length;
            string splitno = s.Substring(3, len - 3); //spliting string
            int num = Convert.ToInt32(splitno); //converting splited string in integer
            num++; //increasing splited string by 1
            EmpId = s.Substring(0, 3) + num.ToString("0000"); //adding String and store in empid string
        }
0
source

Source: https://habr.com/ru/post/1739677/


All Articles