Question

I am using indexed views in my database. So client should have some session connection setting to call them. I am using ado.net connection, command to call stored procedures. Every time I have to call a stored procedure I create connection (I hope connection pool allows me to do this quickly) and execute a command to apply these settings to current connection.

// some code to create a new connection
 //...
 //... 

 if (connection.State != ConnectionState.Open)
 {
     connection.Open();
 }



    using (var cmd = connection.CreateCommand())
    {
        cmd.CommandType = CommandType.Text;
        cmd.CommandText =
                            @"
                    SET ANSI_NULLS ON
                    SET ANSI_PADDING ON
                    SET ANSI_WARNINGS ON
                    SET ARITHABORT ON
                    SET CONCAT_NULL_YIELDS_NULL ON
                    SET QUOTED_IDENTIFIER ON
                    SET NUMERIC_ROUNDABORT OFF";
        cmd.ExecuteNonQuery();
    }

Other thought: adding these settings before every call of stored procedure:

command.CommandText = 'SET....';
command.CommandText += ' EXEC MyStroredProc @...'; 

This solution causes some performance issues, isn't it?

How can I avoid so extra work if I have to create a new connection every time? How to apply these setting automatically?

SOLUTION:

ALTER DATABASE [MyDB] 
    SET 
    ANSI_NULLS ON,
    ANSI_PADDING ON,
    ANSI_WARNINGS ON,
    ARITHABORT ON,
    CONCAT_NULL_YIELDS_NULL ON,
    QUOTED_IDENTIFIER ON,
    NUMERIC_ROUNDABORT OFF
Was it helpful?

Solution

According the documentation, you can set these settings on the Database level:

Database settings set by using ALTER DATABASE are valid only at the database level and take effect only if explicitly set. Database settings override instance option settings that are set by using sp_configure.

Did you try that?

Unfortunately, this properties can't be set in connection string, so use this sql:

ALTER DATABASE dbname SET ANSI_NULLS ON
-- etc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top