Hi ,

I am using the connection string as below in my c # .net .. i will give the connection string in all the forms and i will give con.open in the form load.

is there any other shortest way is there to specify the connection at only once.

bcoz if i want to change the server name in future means i cant change it in all the forms right ?

any one can help me...

thanks in advance ....

SqlConnection conn = new SqlConnection("server=Servername ; database=databasename;uid=sa;pwd=********");

Make a config file with your connection string in, create a class called something like DBConnections, in that class create a method that established a connection to the database by referencing the connection string in the config file.

When you need to connect to the database for say an SQL command you simply call the method from the DBConnections class.

If you do a bit of googleing or binging you should find some good examples

You can store your connection string in a dll and link it to your program.
You would only need to change your main program if the database brand changes.

You can store your connection string in a dll and link it to your program.
You would only need to change your main program if the database brand changes.

Why a dll?

A registry entry or a config file (xml or ini) would be more robust. This way, a human can change the connection and the dll doesn't have to be recompiled on 'brand changes'

The DLLs I use for this purpose grab details from the registry.
When I need a particular DB, I just add the reference to the DLL containing that DB's connect string.

1. config file with your connection string
2. create a class with connection Method.
3. also create dll of the class for the security purpose also

hey hope this helps

SqlConnection conn = new SqlConnection();
conn.ConnectionString = Properties.Settings.Default.(your database connection string);

this will automatically get the connection string!

In windows application you can use app.config file and in web application you can use web.config file.
Their is another way you can use connection string in your application by creating new class file suppose Connect.cs is class file write the below code in that

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data ;
using System.Windows.Forms;

class Connect
    {
        public static SqlConnection GetConnection()
        {  
            return new SqlConnection("server=Servername ; database=databasename;uid=sa;pwd=********");
        }
}

you can call this by calling Connect.GetConnection();
Hope this will help you

I structure my DLLs like this:

using System.Data.SqlClient;
//
namespace DB_SqlLister
{
   using GetPass;
   public class CDB_SqlLister
   {
      public static SqlConnectionStringBuilder csb;

      static CDB_SqlLister()
      {
         CGetPass pass = new CGetPass("AD_ID");
         csb = new SqlConnectionStringBuilder();
         csb.UserID = pass.strUser;
         csb.DataSource = @"machine.domain.com\SQLEXPRESS";
         csb.IntegratedSecurity = true;
      }
   }
}

...that way, I can change the DLL without having to change the internals.
I use one per database per machine, but that's configurable, too.

I structure my DLLs like this:

using System.Data.SqlClient;
//
namespace DB_SqlLister
{
   using GetPass;
   public class CDB_SqlLister
   {
      public static SqlConnectionStringBuilder csb;

      static CDB_SqlLister()
      {
         CGetPass pass = new CGetPass("AD_ID");
         csb = new SqlConnectionStringBuilder();
         csb.UserID = pass.strUser;
         csb.DataSource = @"machine.domain.com\SQLEXPRESS";
         csb.IntegratedSecurity = true;
      }
   }
}

...that way, I can change the DLL without having to change the internals.
I use one per database per machine, but that's configurable, too.

What benefit does this have over using a settings file/registry entry? It's only marginally more secure (plaintext strings like the datasource, uid, etc can be easily extracted from the dll). And it requires recompiling to change provider - ie a tech in the field without a dev machine can't correct issues with it as they could with an xml file/registry entry.

It's convenient and there are lots of options.
I have not yet found a perfect solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.