|
-
November 8th, 2011, 05:56 PM
#1
Connections SQL Server 2008 R2 (open and close)
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);
}
}
}
}
-
November 9th, 2011, 07:54 AM
#2
Re: Connections SQL Server 2008 R2 (open and close)
What errors are you getting? And exactly what is it that isn't working?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|