Zak Hargreaves
Zak Hargreaves

Reputation: 13

Declaring SqlConnection throughout one form

I'm looking for some advise on C# - Please bare in mind that i'm only a beginner with C# & Sql.

I am looking to design a small program that will Add/Edit/Delete and run reports all linked to Sql database.

I wish to include multiple functions and Sql queries within different areas on the same form.

Example: 1. I wish to have a combo box that does a search (Select * from dbo.table) 2. I have a button that when clicked displays all information from another dbo.table.

My question is:

Would I have to declare my Sqlconnection multiple times or can this be declared within my:

public partial class MainMenu : Form
{
    SqlConnection mmConnection = new SqlConnection("#");
    SqlCommand mmCommand = new SqlCommand();
    SqlDataReader reader;
}

then i can use:

mmConnection.Open();
mmConnection.Close();

Any advise would be fantastic. If I can declare at the top of my form it would keep my code cleaner.

Kindest Regards, Zak Hargreaves.

Upvotes: 1

Views: 1385

Answers (4)

HuorSwords
HuorSwords

Reputation: 2245

Consider to declare your SqlConnection as field of your main form (if you don't want to use it on any other form).

Note: Add reference to System.Configuration assembly in order to use ConfigurationManager class.

Example:

public partial class MainMenu : Form
{
    SqlConnection _myConnection;

    public Form1()
    {
        InitializeComponent();

        this._myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
    }

    public void ExecuteAQueryExample()
    {
        if (this._myConnection.State != ConnectionState.Open) this._myConnection.Open();

        using (var command = this._myConnection.CreateCommand())
        {
            // ...
        }

        this._myConnection.Close();
    }
}

Upvotes: 0

Udontknow
Udontknow

Reputation: 1580

Of course, you dont need to have multiple SqlConnection instances. for most application, one connection is sufficient. sqlCommand on the other hand is another thing: You might step into trouble if reusing it, but this is more a design issue than a technical thing.

I would suggest you move all your database access code to another class ("DataProvider"/"DatabaseController" or whatever name you think is sufficient) to properly encapsulate the code parts. There is no need for a form to directly handle SqlConnection or sqlCommand objects, let it indirectly happen via public methods of that controller type:

public class DataController
{
    private SQLConnection Connection {get; set;
    public void DataTable LoadDataFromDatabase()
    {
        ...
    }
    ...
 }

Upvotes: 0

Kevin Andrid
Kevin Andrid

Reputation: 1961

Its better to use single connection string,declare your connection in web.config file and call it in aspx.cs

Upvotes: 1

Pradnya Bolli
Pradnya Bolli

Reputation: 1943

Add your connection string in web.config file

<connectionStrings>
  <add name="CustomerDataConnectionString" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind"
    providerName="System.Data.SqlClient" />
</connectionStrings>

and in aspx form

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection string"].ToString());

for more information use this link

Upvotes: 3

Related Questions