0

I have the following code in a project I'm working on:

foreach(DataTable myTable in myDataSet.Tables)
{
string sSQL = "SELECT `Name` FROM  _Columns WHERE `Table` = "'" + myTable.TableName + "'"
MessageBox.Show(sSQL);
}

For some reason, the second apostrophe (') is not getting added to the string. In fact, anything I try to append after myTable.TableName, doesn't get appended. If I replace myTable.TableName with the name of a table, it works! If I use a variable, and set that to a table name, it works too!

Can anyone tell me what I'm doing wrong here?

Thanks for any information!

5
  • hey, why the downvote? it's a perfctly reasonable question :) Commented Aug 6, 2011 at 22:46
  • Did you even try to compile this code? 'Cause it won't! Commented Aug 6, 2011 at 22:48
  • Sorry - you're right, the line should read: Commented Aug 6, 2011 at 22:51
  • string sSQL = "SELECT Name FROM _Columns WHERE Table = " + "'" + myTable.TableName + "'"; Commented Aug 6, 2011 at 22:52
  • By which you mean "SELECT `Name` FROM _Columns WHERE `Table` = " + "'" + myTable.TableName + "'";? (Little more readable this way) Commented Aug 6, 2011 at 22:57

10 Answers 10

2

That won't even compile, you are ending the string before the first apostrophe.

string sSQL = "SELECT `Name` FROM  _Columns WHERE `Table` = '" + myTable.TableName + "'"
Sign up to request clarification or add additional context in comments.

1 Comment

It must be something in my project that's causing the issue. I created a new project and tried everything that was suggested here, and they all worked! But when I try in my project, none of them do. Thanks for the help all...
1

try

string sSQL = "SELECT `Name` FROM  _Columns WHERE `Table` = '" + myTable.TableName + "'";

BEWARE that this code is wide open to SQL injection which is a massive security problem...
Better use Queries with parameters instead!

Comments

1

To put a " inside a string you need to add an escape char before: \"

Comments

1

you have one too many "

string sSQL = "SELECT `Name` FROM  _Columns WHERE `Table` = "'" + myTable.TableName + "'"

should be

string sSQL = "SELECT `Name` FROM  _Columns WHERE `Table` = '" + myTable.TableName + "'"

Also why aren't you using an entity framework like LINQ?

2 Comments

Thanks for replying. I tried that as well, but I'm still getting the same result.
Re: LINQ: to use LINQ, do I need some kind of database 'provider' layer that would handle the connection to the database? I'm trying to work with windows installer databases, rather than the standard SQL server, Sybase etc..
0

Your string should look like this:

string sSQL = "SELECT `Name` FROM  _Columns WHERE `Table` = '" + myTable.TableName + "'";

Comments

0

Too many double quotes.

You might try this for easier reading:

string sSQL = string.Format("SELECT `Name` FROM _Columns WHERE `Table` = '{0}'", myTable.TableName)

Should accomplish the same thing.

Comments

0

it's not being added because you're ending the string just before it.

take a look at string.Format as an alternative way of constructing strings.

as other(s) have said tat code shouldn't even compile.

Comments

0

Others have answered this, but on top of those answers, for this it's easier to use string.Format

var sSQL = string.Format("Select 'Name' FROM _Columns WHERE 'Table' = '{0}'", myTable.TableName);

Comments

0

In my opinion, you should avoid building concatenated SQL strings in your code. It's simply evil doing things like that.

However, if you must, try something like this:

foreach(DataTable myTable in myDataSet.Tables)
{
    string sSQL = "SELECT `Name` FROM  _Columns WHERE `Table` = '" + 
                       myTable.TableName.Replace("'", "''") + "'";
    MessageBox.Show(sSQL);
}

Comments

0

Try this.

string sSQL = "SELECT `Name` FROM  _Columns WHERE `Table` = '" + myTable.TableName + "'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.