Global database connection string in Windows Form Application
-
Is there a way to have one connection string, in this case an OleDbConnection to an Access database, that can be accessed globally from more than one class? Is it possible to have one connection string that covers an entire application? Thanks for any help. Kyle
-
Is there a way to have one connection string, in this case an OleDbConnection to an Access database, that can be accessed globally from more than one class? Is it possible to have one connection string that covers an entire application? Thanks for any help. Kyle
I use a static method to retrieve my database connection:
using System;
using System.Data.SqlClient;namespace DbUtils {
public class DataTools
{
public static String username = null;
public static String password = null;public static SqlConnection GetConnection() { String connection = "database=mydb; network address=myserver; network library=dbnmpntw; " if(username != null) connection += "user id=\\"" + username + "\\"; "; if(password != null) connection += "password=\\"" + password + "\\"; "; return new SqlConnection(connection); } }
}
And then elsewhere in your app you just get a connection like:
SqlConnection conn = DbUtils.DataTools.GetConnection();
-- Peter Stephens
-
I use a static method to retrieve my database connection:
using System;
using System.Data.SqlClient;namespace DbUtils {
public class DataTools
{
public static String username = null;
public static String password = null;public static SqlConnection GetConnection() { String connection = "database=mydb; network address=myserver; network library=dbnmpntw; " if(username != null) connection += "user id=\\"" + username + "\\"; "; if(password != null) connection += "password=\\"" + password + "\\"; "; return new SqlConnection(connection); } }
}
And then elsewhere in your app you just get a connection like:
SqlConnection conn = DbUtils.DataTools.GetConnection();
-- Peter Stephens