Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# 2005: Getting the "message" from an SQL statement?

Status
Not open for further replies.

TheElephantMan

Programmer
Jun 18, 2004
42
US
Hi, all.

I know that when I run OdbcCommand.executeNonQuery(), it returns the number of rows affected. However, is there a way to also get the error message that the database on the other end is broadcasting?

For example, if I attempt to insert a record with a primary key that already exists, I know that my rows affected will be zero. If I try to do this directly into MS Query analyzer, it tells me that I can't do it:

"Violation of PRIMARY KEY constraint '<key name>'. Cannot insert duplicate key in object '<table name'.
The statement has been terminated."

Is there a way to get messages like these through the ODBC interface in C#?

Thanks in advance,
TEM
 
Have you tested running the bad query in your program? If i am not mistaken, it may throw an exception back with the Sql message. We created an API for our table, and it will throw exceptions when the query is bad, doing a command from the code should too...
 
Code:
try
{
   OdbcCommand.executeNonQuery()
}
catch(sqlexception ex)
{
   //do something with exception}
}
here are the sqlexception members

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi, all.

That's exactly what I was looking for. It's been a while since the last time I've done DB programming, so there's plenty of rust. I should have known that. :-/

Thanks for the help!
TEM

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top