Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3223078
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:03:55+00:00 2026-05-17T16:03:55+00:00

I’m trying to create a class to represent the value of a column in

  • 0

I’m trying to create a class to represent the value of a column in SQL. Here’s the final goal I want to reach:

public string GenerateInsertSql()
{
  StringBuilder insertSql = new StringBuilder();
  insertSql.Append("INSERT INTO " + SchemaName + "." + TableName + "\n");
  insertSql.Append("(\n");
  int counter = 0;
  foreach (ColumnValue columnValue in _insertValues)
  {
    if (counter > 0) insertSql.Append(",");
    counter++;
    insertSql.Append(columnValue.ColumnName).Append("\n");
  }
  insertSql.Append(")\n");
  insertSql.Append("VALUES\n");
  insertSql.Append("(\n");
  counter = 0;
  foreach (ColumnValue columnValue in _insertValues)
  {
    if (counter > 0) insertSql.Append(",");
    counter++;
    insertSql.Append(columnValue.SqlValue).Append("\n");
  }
  insertSql.Append(")\n");
}

So the class is the ColumnValue class mentioned above. The ColumnValue class needs a Value property which can presumably be any basic .NET type (int, string etc). The class also has a property, SqlValue, which provides a string representation of the Value property which I can use in the generated SQL statement. So if Value is an int, then SqlValue just gives Value.ToString(), whereas if Value is a string, SqlValue needs to check if the string is null in order to either return ‘NULL’ or the Value property itself.

Initially I tried to have Value as an object and SqlValue to test the type of Value in a switch statement:

public string SqlValue
{
  get
  {
    switch (Value.GetType())
    {
      ...
    }
  }
}

But then I get ‘Value of integral type expected’. So I can’t check an object’s type.

Next I tried generics. I changed the ColumnValue class to ColumnValue<T>, with the Value property as follows:

public T Value { get; set; }

And then, in the SqlValue property, another check of the type of T, but I’m not allowed to do that either.

Is there a way to achieve this in C# without simply creating separate classes to handle each possible type of column?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:03:55+00:00Added an answer on May 17, 2026 at 4:03 pm

    switch keyword can take only integers (correctly speaking, integral type (see here)). But you can always use “if” statements. For example,

    var t = value.GetType();
    if (t == typeof(string))
    {
       ..
    }
    else if (t == typeof(int))
    {
    }
    else if ...
    

    You can also use “is” keyword. For example,

    if (value is string) 
    {
      ...
    }
    else if (value is int)
    {
      ...
    }
    else if ...
    

    “is” operator will also check within inheritance hierarchy but I don’t think that will be the case in your usage.

    Lastly, I hope that whatever SqlValue implementation that you are going to do will handle escaping (for example – quotes within string values) and will protect against sql injection attacks. You may consider generating parameterized query insetad (i.e. add parameter for each column in the query and then pass actual values as parameter values – so you won’t need SqlValue property at all).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.