Click here to Skip to main content
15,924,581 members
Home / Discussions / C#
   

C#

 
GeneralRe: window.showmodal Pin
Heath Stewart21-May-04 8:46
protectorHeath Stewart21-May-04 8:46 
GeneralRe: window.showmodal Pin
rbarzallo21-May-04 8:57
rbarzallo21-May-04 8:57 
GeneralTreeviews and Imagelists Pin
Kryptonic21-May-04 5:41
Kryptonic21-May-04 5:41 
GeneralRe: Treeviews and Imagelists Pin
Heath Stewart21-May-04 5:46
protectorHeath Stewart21-May-04 5:46 
Generalmsn profile tool Pin
hardboy11121-May-04 5:39
hardboy11121-May-04 5:39 
GeneralRe: msn profile tool Pin
Heath Stewart21-May-04 5:41
protectorHeath Stewart21-May-04 5:41 
GeneralShared DBConnections and Dynamic Connection Strings Pin
redrover_nc21-May-04 5:02
redrover_nc21-May-04 5:02 
GeneralRe: Shared DBConnections and Dynamic Connection Strings Pin
Heath Stewart21-May-04 5:37
protectorHeath Stewart21-May-04 5:37 
One common method is to have a static collection property on some class that all your code can access like so:
public sealed class DB
{
  private static SqlConnection connection;
  private static object syncRoot = new object();
  public static SqlConnection Connection
  {
    get
    {
      if (connection == null)
        lock (syncRoot)
          if (connection == null)
            connection = new SqlConnection(
              ConfigurationSettings.AppSettings["ConnectionString"]);
      return connection;
    }
  }
}
The caller is still responsible for opening and closing the connection, but you should leave it that way so you don't leave connections open longer than you have to. This can degrade performance (and limit connections too much) on your database server. Also note that I use SqlConnection only as an example. You could use any connection class, or even the ADO.NET interface IDbConnection if you wanted to. The latter idea would fit nicely with using a provider pattern.

I also used an example where the connection string is placed in the appSettings section of your app's .config file, which is also another good idea.

 

Microsoft MVP, Visual C#
My Articles
GeneralAppSpecific Pin
Huy Pham21-May-04 4:55
Huy Pham21-May-04 4:55 
Questionhow to use rs232/ rs485 in .net compact framework Pin
jelleo21-May-04 3:33
jelleo21-May-04 3:33 
AnswerRe: how to use rs232/ rs485 in .net compact framework Pin
Dave Kreskowiak21-May-04 3:59
mveDave Kreskowiak21-May-04 3:59 
QuestionHow to hide taskbar in C# compact .net framework Pin
jelleo21-May-04 3:31
jelleo21-May-04 3:31 
AnswerRe: How to hide taskbar in C# compact .net framework Pin
Dave Kreskowiak21-May-04 4:13
mveDave Kreskowiak21-May-04 4:13 
Generalc+ instead of c# Pin
jelleo21-May-04 5:26
jelleo21-May-04 5:26 
GeneralRe: c+ instead of c# Pin
Judah Gabriel Himango21-May-04 5:52
sponsorJudah Gabriel Himango21-May-04 5:52 
GeneralRe: c+ instead of c# Pin
jelleo21-May-04 6:44
jelleo21-May-04 6:44 
GeneralRe: c+ instead of c# Pin
Dave Kreskowiak21-May-04 8:54
mveDave Kreskowiak21-May-04 8:54 
GeneralPinvoke ddlimport Pin
jelleo21-May-04 9:21
jelleo21-May-04 9:21 
GeneralRe: Pinvoke ddlimport Pin
Dave Kreskowiak21-May-04 9:51
mveDave Kreskowiak21-May-04 9:51 
GeneralHandle to form Pin
jelleo21-May-04 10:11
jelleo21-May-04 10:11 
GeneralCannot Call Invoke or InvokeAsync Pin
GostWryter21-May-04 1:25
GostWryter21-May-04 1:25 
GeneralRe: Cannot Call Invoke or InvokeAsync Pin
Dave Kreskowiak21-May-04 3:08
mveDave Kreskowiak21-May-04 3:08 
GeneralRe: Cannot Call Invoke or InvokeAsync Pin
Heath Stewart21-May-04 4:23
protectorHeath Stewart21-May-04 4:23 
GeneralRe: Cannot Call Invoke or InvokeAsync Pin
Grimolfr21-May-04 3:57
Grimolfr21-May-04 3:57 
GeneralRe: Cannot Call Invoke or InvokeAsync Pin
Heath Stewart21-May-04 4:24
protectorHeath Stewart21-May-04 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.