Why Does A Sqlexception Thrown By Sqlcommand.executenonquery Contain All The Prints As Errors?
When I run the following snippet try { using (SqlConnection conn = new SqlConnection('I'm shy')) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.Comm
Solution 1:
No you can't avoid this behavior. Its the result of the way TdsParser.ThrowExceptionAndWarning() is written
particularly this line
bool breakConnection = this.AddSqlErrorToCollection(ref temp, ref this._errors) | this.AddSqlErrorToCollection(ref temp, ref this._attentionErrors);
breakConnection |= this.AddSqlErrorToCollection(ref temp, ref this._warnings);
breakConnection |= this.AddSqlErrorToCollection(ref temp, ref this._attentionWarnings);
My guess is that for whatever reason one of the collection _error or _attentionErrors is empty for ExecuteScaler and its not for ExecuteNonQuery.
I'm sure if you poked around enough you could probably find out why.
In any case you seem to have the workaround already. Only use the first item in SQLExecption.Error
Solution 2:
ExecuteNonQuery normally returns a recordset while ExecuteScalar returns the first row + first column.
Post a Comment for "Why Does A Sqlexception Thrown By Sqlcommand.executenonquery Contain All The Prints As Errors?"