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
According the documentation, you can set these settings on the Database level:
Did you try that?
Unfortunately, this properties can't be set in connection string, so use this sql: