Issue with SqlCommandBuilder (Insert) when using columns names with spaces

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

I'm using columns names with spaces. OK, I could change them, but...
Other code seems to work fine.

However the SQLCommandBuilder can't handle the spaces by default. If I look
at the generated insert sql statement I can see that it didn't do any
special treatment for the spaces.

I'm sure there must be a solution?
 
OK, found the solution:

SqlCommandBuilder custCB = new SqlCommandBuilder(myAdapter);

custCB.QuotePrefix = "["; // straight bracket for handling spaces in column
name

custCB.QuoteSuffix = "]";
 
Hi Alex:


Alex said:
I'm using columns names with spaces. OK, I could change them, but...
Other code seems to work fine.

However the SQLCommandBuilder can't handle the spaces by default. If I look
at the generated insert sql statement I can see that it didn't do any
special treatment for the spaces.

I'm sure there must be a solution?

I see you found the solution but I'd seriuosly recommend changing the column
names. Not trying to preach but I've been burned by this way too bad. I
worked in a place where a guy who worked there for about a year loved using
spaces, special chars and reserved words for field names. So we used to
escape them and it took a lot of work to code around it. He would point out
that it's not that much work to use the braces and technically he was right.
But people constantly forgot to use the braces and we had a good amount of
turnover (ie new programmers) and contractors. It honestly got to the poitn
where we would spend probably 2 hours a week total on new queries that blew
up, figuring out why, fixing it etc. Those of us that worked there for a
while did it the least, but when you're in a hurry, it's easy to do and
since only the tables this guy (one person) created did this, you were used
to not having to do it and it was easy to make. Every new programmer would
make the mistake 5 or 6 times, then misdiagnose the problem, and someone
would have to look into it.

Anyway, I'll admit this was pretty extreme but it lasted for about 4 months
until the guy quit. The next day everyone unanimously agreed to work
overtime to change the fieldnames and modify the code. It took about 5
hours to make all of the changes (there was a ton of code) and test
correctly, but it was the best time investment I've ever seen. It paid for
itself in one week. To that end, I'm totally biased on this subject and
highly encourage others not to do it...hopefully I can save some other
people all those headaches.

Cheers,

Bill
 
Interesting story. Well, I'm not a space person either.
Just got excited that I could use it after programming software for years
not using the spaces.
I'm doing a little training on ADO.NET and wanted to solve this.

I will have many more questions...

Thanks,
Alex
 
Hi Alex:
Alex said:
Interesting story. Well, I'm not a space person either.
Just got excited that I could use it after programming software for years
not using the spaces.
Yep, it's nice to know there's a work around , I just mention the anecdote
b/c it can become a problem. Realistically, very few people go out of their
way to use reserved words and columsn with spaces, but it sure was a pain.
I'm doing a little training on ADO.NET and wanted to solve this.
Absolutely, and I know many times people need a solution not a lecture,
particularly when they have a deadline, I just figured I'd mention it for
future reference.
I will have many more questions...
Ask away, I'll do my best ;-)
 
Back
Top