Hi there i hope someone can help me with this issue.
From another part of the program I want to call a class so I can:
1. Open a SQL Connection
2. Do many queries
3. Close a SQL Connection
The reason to leave the Connection open is because I want to save workload in the server.
I have the following code which is not working with CloseCnn().
Also Im not sure if conn should be a public var so the value doesnt change until I close the Connection.
Thank you for the help
Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace PixelCheckSum.Controllers { class BDController { public SqlConnection conn; public static void OpenCnn() { // 1. Instantiate the connection SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI"); try { // 2. Open the connection if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Open(); } catch(Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public static void CloseCnn() { try { //Close the connection if (conn != null) { conn.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }




Reply With Quote