1

I have the following struct in C# class

public struct Employee
        {
            public const string EMPID = "EMP_ID";
            public const string FName = "FIRST_NAME";
            public const string LNAME = "LAST_NAME";
            public const string DEPTID = "DEPT_ID";

        }

Is there an easy way to build a string as follows

const string mainquery="INSERT INTO EMP(EMP_ID,FIRST_NAME,LAST_NAME,DEPT_ID) VALUES(:EMP_ID,:FIRST_NAME,:LAST_NAME,:DEPT_ID)"

Instead of doing as follows and then concatenating it.

const string EMP_COLS=
                EMPLOYEE.EMPID + "," +
                EMPLOYEE.FNAME + "," +
        EMPLOYEE.LNAME + "," +
                EMPLOYEE.DEPTID;

            const string EMP_Values=
                EMPLOYEE.EMPID + ":" +
                EMPLOYEE.FNAME + ":" +
        EMPLOYEE.LNAME + ":" +
                EMPLOYEE.DEPTID;
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
acadia
  • 2,301
  • 10
  • 40
  • 57

6 Answers6

4

You could try something like this:

StringBuilder sb = new StringBuilder();
var fields = typeof(Employee).GetFields();
for (int i = 0; i < fields.Length; ++i)
{
    sb.Append(fields[i].GetValue(new Employee()));
    if (i < fields.Length - 1)
    {
        sb.Append(',');
    }
}

string result = sb.ToString();
// The above will be "EMP_ID,FIRST_NAME,LAST_NAME,DEPT_ID"

Edit: Note that above I'm assuming you've got using directives for both System.Reflection and System.Text.

cliss
  • 626
  • 7
  • 8
  • 1
    But will that guarantee that the order is consistent? It would be bad if for some reason the outuputted order changed and data was inserted into the wrong columns. – Walter Jun 15 '10 at 13:56
  • That's an excellent point Walter--there isn't any guarantee. That said, I believe there's an *implicit* "guarantee" that the order is as-defined order in the struct. Not a place to be rolling the dice though, to your point. – cliss Jun 15 '10 at 14:02
  • You should never depend on the order, but considering the result wanted (as seen in the mainquery declared above) I don't think the order would matter anyway, you'll just get it out once and then use the same list in both parts of the mainquery... – Hans Olsson Jun 15 '10 at 20:36
1

If the struct held readonly properties rather than public consts you could do it with Reflection. You could call Type.GetProperties, loop through them all and call them to get out the values and insert the values into a List<string> and then join them with string.Join(", ", myList); or something.
You'd then get the first part of the string as EMP_ID, FIRST_NAME, LAST_NAME, DEPT_ID and you'd need to make a copy of that and add the :s as needed and you'd have both parts.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
1

you can use the string.Format(string format, object[] arg) Method as follows:

string query = string.Format("INSERT INTO EMP({0},{1},{2},{3}) VALUES(:{0},:{1},:{2},:{3})",
                Employee.EMPID,
                Employee.FNAME,
                Employee.LNAME,
                Employee.DEPTID);

Hope that helps.

obivandamme
  • 535
  • 1
  • 5
  • 13
0

You will need some kind of mapping dictionary, like here, or you could just use some ORM tool to do the mapping for you.

Community
  • 1
  • 1
SWeko
  • 30,434
  • 10
  • 71
  • 106
0
        string[] values = new[] {
            "EMP_ID",
            "FIRST_NAME",
            "LAST_NAME",
            "DEPT_ID",
        };

        var columns = string.Join(",", values);
        var parameters = ":" + string.Join(",:", values);

        var sql = string.Format("INSERT INTO EMP({0}) VALUES({1})",
            columns,
            parameters);
simendsjo
  • 4,739
  • 2
  • 25
  • 53
0
string[] cols = typeof(Employee).GetFields()
                 .Select(f => f.GetValue(null).ToString()).ToArray();

Console.WriteLine(string.Format(
      "INSERT INTO EMP({0}) VALUES(:{1})", 
      string.Join(",", cols), 
      string.Join(",:", cols)));
Brad Rem
  • 6,036
  • 2
  • 25
  • 50
Sorin Comanescu
  • 4,829
  • 3
  • 29
  • 38