Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>If this <em>is not</em> homework:</strong></p> <ul> <li><p>Think about the limitations that this ID naming scheme puts on your data.</p></li> <li><p>If those limitations are unacceptable, look for a better mechanism to generate unique IDs. For one thing, is it really necessary to store the letter <code>E</code> along with the number, or could you just make <code>empid</code> a column of type <code>INT</code> and add the <code>E</code> programmatically? If you never actually display the IDs anywhere, consider using e.g. GUIDs (see the documentation of the .NET type <code>System.Guid</code>), as they are guaranteed to be unique (for most practical purposes). Or simply use the auto-increment feature of your DB on the <code>empid</code> column.</p></li> </ul> <p><strong>If this <em>is</em> homework:</strong></p> <ul> <li><p>Can IDs of deleted records be recycled (reused) with records added later on?</p></li> <li><p>Try to locate a method in the .NET framework that formats an integer number with any number of leading zeroes; e.g. <code>1</code> &rarr; <code>"001"</code>.<br/> Try also to locate a method in the .NET framework that does the opposite, e.g. <code>"001"</code> &rarr; <code>1</code>.<br /> Use these two functions to convert between your <code>Ennn</code> IDs and their numerical parts.</p></li> <li><p>Retrieve all existing IDs from the DB table:<br/> If you cannot recycle IDs, get them in descending order, tell the DB to only return the first entry from that sorted result set, and determine the next larger ID from that returned ID value.<br/> If you can recycle IDs, get them preferable in ascending order and find the first gap (i.e. when the difference between the numerical parts of two adjacent IDs is greater than 1). Build a new ID so that it falls into this gap.</p></li> </ul> <hr> <h2>Some example code snippets:</h2> <p><strong>Format an integer number as an <code>E...</code> string:</strong></p> <pre><code>int numericPart = 123; // &lt;-- example input string empid = numericPart.ToString("'E'000"); </code></pre> <p><strong>Extract the numeric part from an <code>E...</code> string</strong><br/> (using regular expressions, which is actually overkill in your case):</p> <pre><code>using System.Text.RegularExpressions; ... string empid = "E123"; // &lt;-- example input var empidPattern = new Regex(@"^E(?&lt;NumericPart&gt;\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) } </code></pre> <p><strong>Load existing keys from database table into a collection:</strong></p> <pre><code>using System.Data; // create a new collection that will hold all empid keys in emp var empidList = new List&lt;string&gt;(); // 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)); } } } </code></pre> <p>I'll leave it to yourself to put these pieces together, add the actual logic I described above, and further improve the code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload