Escaping a single quote in DataTable select expresssion

G

Guest

I am adding data to a DateTable in a DataSet and need to verify if a row
already exists with a specific column value:

string aName = "O'Connor";
string filter = "Name='" + aName + "'";
DataRow [] drs = MyDataset.MyTable.Select(filter);
if( drs.Length == 0 ) {
// Add a row for this name
}

Note the name (O'Connor) contains a single quote.

How should I escape the aName value so the filter works?

Disclaimers:
1. This is a select on a DataTable not a database select -- so no
parameterized queries.
2. The table MyDataset.MyTable already has a primary key on another field,
so I can't do a MyTable.FindByXXX
 
G

Guest

Indeed it does. Thank you sir!

Strange this isn't mentioned in the DataColumn.Expression Property
documentation (at least, if it is, I can't find it) -- seems like pretty
fundamental information. All the information about escaping seems to relate
to how to escape wierd column names.

Any doc-person at Microsoft listening?

Val Mazur said:
Hi,

You need to double it and it should work

--
Val Mazur
Microsoft MVP


Sir Phillip Sydney said:
I am adding data to a DateTable in a DataSet and need to verify if a row
already exists with a specific column value:

string aName = "O'Connor";
string filter = "Name='" + aName + "'";
DataRow [] drs = MyDataset.MyTable.Select(filter);
if( drs.Length == 0 ) {
// Add a row for this name
}

Note the name (O'Connor) contains a single quote.

How should I escape the aName value so the filter works?

Disclaimers:
1. This is a select on a DataTable not a database select -- so no
parameterized queries.
2. The table MyDataset.MyTable already has a primary key on another field,
so I can't do a MyTable.FindByXXX
 
V

Val Mazur

It is just some sort of standard. It was exact same way in ADO and it works
exact same way in a SQL statements. This is probably why Microsoft does not
mention it in documentation

--
Val Mazur
Microsoft MVP


Sir Phillip Sydney said:
Indeed it does. Thank you sir!

Strange this isn't mentioned in the DataColumn.Expression Property
documentation (at least, if it is, I can't find it) -- seems like pretty
fundamental information. All the information about escaping seems to
relate
to how to escape wierd column names.

Any doc-person at Microsoft listening?

Val Mazur said:
Hi,

You need to double it and it should work

--
Val Mazur
Microsoft MVP


in
message news:[email protected]...
I am adding data to a DateTable in a DataSet and need to verify if a row
already exists with a specific column value:

string aName = "O'Connor";
string filter = "Name='" + aName + "'";
DataRow [] drs = MyDataset.MyTable.Select(filter);
if( drs.Length == 0 ) {
// Add a row for this name
}

Note the name (O'Connor) contains a single quote.

How should I escape the aName value so the filter works?

Disclaimers:
1. This is a select on a DataTable not a database select -- so no
parameterized queries.
2. The table MyDataset.MyTable already has a primary key on another
field,
so I can't do a MyTable.FindByXXX
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top