Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2009-03-30
8
1,367 Views
Last Modified: 2012-06-27
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
Comment
Question by:WeiXi
  • 4
  • 4
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24023423
no way...

you should be doing the sql code with parameters instead of relying on those stuff...
0
 
LVL 3

Author Comment

by:WeiXi
ID: 24023598
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
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24023735
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 3

Author Comment

by:WeiXi
ID: 24023915
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
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24023988
the reference was for adodb, indeed...

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

Open in new window

0
 
LVL 3

Author Comment

by:WeiXi
ID: 24024152
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24024242
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
 
LVL 3

Accepted Solution

by:
WeiXi earned 0 total points
ID: 24024493
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 32
SSIS Feed MS SQL Server Stored procedure with loop 4 37
VS 2017 18 49
Problem to Office 1 15
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question