I have the following type of code in my data layer that can be called from a console application, a Windows application, etc., while the correct connection string is read from the corresponding calling App.Config file:
public static udsDataset GetDataset(int datasetID) { string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; string sql = @"select * from Dataset WHERE DatasetID=@datasetID "; using (SqlConnection conn = new SqlConnection(connectionString)) {
Now I want to call the same code from the SQLCLR stored procedure (in the database where these tables exist), where the contextual connection is usually used:
using(SqlConnection connection = new SqlConnection("context connection=true")) { connection.Open();
The most obvious approach that comes to mind is function overloading:
public static udsDataset GetDataset(int datasetID) { string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectionString)) { return GetDataset(datasetID, conn); } } public static udsDataset GetDataset(int datasetID, SqlConnection conn) {
Thus, applications with App.Config can invoke a version without connecting, and SQLCLR can invoke a version that requires SqlConnection.
This โsounds good,โ but to write the same overload style for each similar function, this seems wrong.
source share