• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1480
  • Last Modified:

How can I execute a T-SQL SET-command for a SQL-server connection from my C#-application?

I need to execute two SET-commands for a SQL-server connection from my C#-application, namely:
  SET DATEFORMAT
  SET ARITHABORT

I am wondering where/how I can issue these commands. It doesn't work in the SqlCommand.CommandText. Prepending them to the normal CommandText results in an error.

An optimal way for me would be to include the SET-commands in the connection-string or execute them via a SqlConnection object. Their effect should last for the entire connection. Anyway I couldn't figure out a way to do so.

I am using Visual Studio 2005, .net framework 2.0 and SQL-server 2000.

Any help is greatly appreciated.
Thanks, Wei Xi
0
WeiXi
Asked:
WeiXi
  • 4
  • 4
3 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
no way...

you should be doing the sql code with parameters instead of relying on those stuff...
0
 
WeiXiAuthor Commented:
Arrghh. Please, don't say "no way" :(

I can live without the SET DATEFORMAT, following your advice about parameters. Lots of code has to be changed, but it can be done.

But I simply don't know how I should do without the SET ARITHABORT. I have to write to a table which is used in an indexed view. This knowledge article applies to my case:
http://support.microsoft.com/kb/305333
It says, that it is only possible to write to such a table when SET ARITHABORT ON is in effect. Removing the index from the view results in severe performance problems due to large data sets, so this is no option either.

Is it really true, that I can't use a database with indexed views from C#? I would consider this a major flaw of the C#/.net platform.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you can.

note, the priority of the setting(s) are defined like this:
- explicit SET by application
- connection string specification (*)
- ODBC DSN setting
- provider default setting
- database default setting
- server default setting

unfortunately, the SET ARITHABORT is not part of that, so (b) does not apply.
http://support.microsoft.com/kb/305333


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
WeiXiAuthor Commented:
Well, thanks a lot for the insight so far. However, I still don't know how to solve my problem.

Changing the server or database defaults is out of question, there is too much legacy code built around them. Changing the SqlConnection to OdbcConnection or some other Connection is unfeasible for the same reason.

Remains:
- explicit SET by application
- connection string specification (*)

In the above mentioned knowledge articel it says:
> To resolve this problem, add the following ADO code to your application
> after you open the connection to your database:
> MyConnection.Execute "SET ARITHABORT ON"

I was hoping that something similar could be done with C# and SqlConnection!
If that's not possible, do you have any other idea how I could write to the index-view-table?

Thanks for your help,
Wei Xi
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the reference was for adodb, indeed...

in ado.net:
SqlCommand cmd = new SqlCommand("SET ARITHABORT ON; INSERT INTO ..... ", conn);

Open in new window

0
 
WeiXiAuthor Commented:
Well, that was also my first shot. Just prepend the SET ARITHABORT to the insert statement.
Unfortunately that doesn't work, the error message ist just the same as without the SET ARITHABORT:
> INSERT failed because the following SET options have incorrect settings: 'ARITHABORT'.

I guess the reason is due to the following statement in the knowledge base article:
> To successfully insert a row into a table that is referenced in an indexed view,
> the SQL ARITHABORT configuration setting must be set to ON. Furthermore,
> the statement that applies this configuration setting must be executed in its own
> batch.

That is, the SET ARITHABORT must be concluded with a GO. However, a GO is not allowed in SqlCommand.CommandText. For the same reason the SET ARITHABORT cannot be used in a stored procedure.

So, I am still stuck. Thanks god, I am talking to a Sage here. They say, Sages always know a way... :)
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
well, I do know the way I will take in a couple of minutes ... the one that leads to my bed (which might explain why I did not yet "solve" the problem :)

anyhow, let me make you try to create a stored proc like this:
SET ARITHABORT ON
GO
CREATE PROCEDURE dbo.DoYourInsert @parameter  ...
AS
INSERT INTO ...
 
GO
 
 
and use that proc in your c#

Open in new window

0
 
WeiXiAuthor Commented:
Defining a stored procedure in a SET ARITHABORT ON environment does not do the trick. Same error.

However, your other answer about the SqlCommand inspired me to the solution that I found out just now: The insert/update/delete-SqlCommand for the indexed-view-table must be executed inside a transaction. At the beginning of the transaction a separate SqlCommand doing the SET must be executed first. See the working code below.

Thanks a lot for your help, I am happy to award you the points for guiding me along.
Wei Xi
SqlConnection connection = new SqlConnection(myConnectionString);
connection.Open();
 
SqlTransaction transaction = connection.BeginTransaction();
 
SqlCommand insert = connection.CreateCommand();
insert.Transaction = transaction;
insert.CommandText = "INSERT ...";
 
SqlCommand arithabort = connection.CreateCommand();
arithabort.Transaction = transaction;
arithabort.CommandText = "SET ARITHABORT ON";
arithabort.ExecuteNonQuery();
 
insert.ExecuteNonQuery();
 
transaction.Commit();

Open in new window

0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now