I\'ve a connection string saved to Properties.Settings.Default, i need to use an sql connection in all classes without having to declare it everytime, so how should
I've have something similar set up in an old project like so.
It's worth noting that you should always be using a new SqlConnection for all your operations, because of connection pooling.
public static class SqlConnectionUtil
{
public static string DefaultConnectionString { get; private set; }
static SqlConnectionUtil()
{
SqlConnectionUril.DefaultConnectionString =
Properties.Settings.Default.TheConnectionString;
}
public static SqlConnection Create()
{
return new SqlConnection(SqlConnectionUtil.DefaultConnectionString);
}
}
You would then use it like this.
using (var connection = SqlConnectionUtil.Create())
{
using (var command = connection.CreateCommand())
{
// do things.
}
}