Hi
I have created below Class. I want connection should be called once for all forms. I am using Windows Form Application
C#
class DBConnection
{
public SqlConnection con;
public void Connection()
{
con = new SqlConnection("Data Source = .;Initial Catalog = Pl_Live; Integrated Security=true;");
}
}Thanks

Vijay Pratap SinghRank #381 · 6.6K reputationPosted Apr 18, 2024, 8:31 PM EDT
Hello Ramco,
it seems like you're missing a constructor to initialize the connection.
using System.Data.SqlClient; class DBConnection { public SqlConnection con; // Constructor to initialize the connection public DBConnection() { con = new SqlConnection("Data Source = .;Initial Catalog = Pl_Live; Integrated Security=true;"); } // Method to open the connection public void OpenConnection() { if (con.State != System.Data.ConnectionState.Open) { con.Open(); } } // Method to close the connection public void CloseConnection() { if (con.State != System.Data.ConnectionState.Closed) { con.Close(); } } }With this modified class, you can now create an instance of
DBConnectionand call theOpenConnection()method whenever you need to use the connection.using System; using System.Windows.Forms; namespace YourNamespace { public partial class YourForm : Form { private DBConnection dbConnection; public YourForm() { InitializeComponent(); // Initialize the database connection dbConnection = new DBConnection(); dbConnection.OpenConnection(); // Open the connection } // Make sure to close the connection when the form is closed protected override void OnFormClosed(FormClosedEventArgs e) { base.OnFormClosed(e); // Close the connection dbConnection.CloseConnection(); } // Now you can use dbConnection.con to access the SqlConnection object // and perform your database operations. } }Hope this will help.
Thanks
Ashutosh SinghRank #549 · 4.4K reputationPosted Apr 19, 2024, 7:10 AM EDT
To ensure that the database connection is established once for all forms in your Windows Forms application, you can use a singleton pattern for your
DBConnectionclass. This pattern ensures that only one instance of the class is created and shared across all forms. Here's how you can modify your class:using System.Data.SqlClient; class DBConnection { private static readonly Lazy instance = new Lazy(() => new DBConnection()); public static DBConnection Instance => instance.Value; public SqlConnection Connection { get; } private DBConnection() { Connection = new SqlConnection("Data Source = .;Initial Catalog = Pl_Live; Integrated Security=true;"); } }With this setup:
DBConnectionclass has a private constructor to prevent direct instantiation.Instanceproperty is provided to access the single instance of the class.Connectionproperty provides access to theSqlConnectioninstance.Connectionis initialized only once when the class is first accessed.Now, you can use
DBConnection.Instance.Connectionto access the shared connection instance across all forms in your application. For example:using System.Windows.Forms; public class YourForm : Form { public YourForm() { InitializeComponent(); // Access the shared database connection instance SqlConnection connection = DBConnection.Instance.Connection; // Use the connection as needed } }This way, you ensure that there's only one database connection instance shared across all forms, and it's initialized only once when first accessed.