Click here to Skip to main content
16,011,170 members
Home / Discussions / C#
   

C#

 
GeneralHelp me please!!! Deconvolution Pin
Angeluna2-Dec-03 23:11
sussAngeluna2-Dec-03 23:11 
GeneralRe: Help me please!!! Deconvolution Pin
leppie3-Dec-03 8:26
leppie3-Dec-03 8:26 
GeneralRe: Help me please!!! Deconvolution Pin
J. Dunlap3-Dec-03 9:07
J. Dunlap3-Dec-03 9:07 
GeneralTracking running applications Pin
lithium692-Dec-03 22:56
lithium692-Dec-03 22:56 
GeneralRe: Tracking running applications Pin
Braulio Dez3-Dec-03 2:40
Braulio Dez3-Dec-03 2:40 
Generalcreate global database connection Pin
akokini_john2-Dec-03 21:23
akokini_john2-Dec-03 21:23 
GeneralRe: create global database connection Pin
Braulio Dez3-Dec-03 2:43
Braulio Dez3-Dec-03 2:43 
GeneralRe: create global database connection Pin
Sean McCormack3-Dec-03 2:51
Sean McCormack3-Dec-03 2:51 
akokini,

There's a difference between defining the connection globally, vs. having a global connection. Defining the connection globally is good practice, and can be done either through the .config file, or through a static variable. Having a global connection is VERY bad, since it will destroy your ability to have proper connection pooling, and even worse, could cause serious problems between different classes if they're all using the same connection at the same time.

Here's an example of defining the connection through a static variable:

using System;
using System.Configuration;

namespace sean.config
{
public class Config
{

public static string DataBaseConnection = @"server=192.168.0.1;Trusted_Connection=false;uid=sa;pwd=;database=Northwind"}
}


You could then reference it anywhere in your code by doing the following:

SqlConnection conn = new SqlConnection(Config.DataBaseConnection)



You could take it a step further, and define the connection in a config file. Here's an example:

The configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="mydb" value="server=192.168.0.1;Trusted_Connection=false;uid=sa;pwd=;database=Northwind" />
</appSettings>
</configuration>


The modified config class:

using System;
using System.Configuration;

namespace sean.config
{
public class Config
{

public static string DataBaseConnection = @ConfigurationSettings.AppSettings["mydb"];
}


I hope that helps!

Sean McCormack
GeneralCreate query for excel Pin
KSEI2-Dec-03 21:01
KSEI2-Dec-03 21:01 
GeneralRe: Create query for excel Pin
Braulio Dez3-Dec-03 2:45
Braulio Dez3-Dec-03 2:45 
GeneralRe: Create query for excel Pin
Heath Stewart3-Dec-03 3:38
protectorHeath Stewart3-Dec-03 3:38 
GeneralPublicClass() is inaccessible due to its protection level Pin
CJOakwood2-Dec-03 18:33
CJOakwood2-Dec-03 18:33 
GeneralRe: PublicClass() is inaccessible due to its protection level Pin
Heath Stewart2-Dec-03 20:07
protectorHeath Stewart2-Dec-03 20:07 
GeneralRe: PublicClass() is inaccessible due to its protection level Pin
leppie3-Dec-03 6:08
leppie3-Dec-03 6:08 
Generalkeyboard accelerator c++ Pin
Anonymous2-Dec-03 18:11
Anonymous2-Dec-03 18:11 
GeneralRe: keyboard accelerator c++ Pin
Heath Stewart2-Dec-03 20:02
protectorHeath Stewart2-Dec-03 20:02 
Generaldrawing graph with c# Pin
ramesh2619742-Dec-03 16:33
ramesh2619742-Dec-03 16:33 
GeneralRe: drawing graph with c# Pin
Heath Stewart2-Dec-03 16:48
protectorHeath Stewart2-Dec-03 16:48 
GeneralRe: drawing graph with c# Pin
Christian Graus2-Dec-03 17:26
protectorChristian Graus2-Dec-03 17:26 
GeneralRe: drawing graph with c# Pin
Nick Parker2-Dec-03 17:54
protectorNick Parker2-Dec-03 17:54 
GeneralRe: drawing graph with c# Pin
Christian Graus2-Dec-03 17:56
protectorChristian Graus2-Dec-03 17:56 
GeneralRe: drawing graph with c# Pin
Heath Stewart2-Dec-03 20:11
protectorHeath Stewart2-Dec-03 20:11 
GeneralRe: drawing graph with c# Pin
Nick Parker3-Dec-03 1:45
protectorNick Parker3-Dec-03 1:45 
GeneralRe: drawing graph with c# Pin
Heath Stewart3-Dec-03 3:26
protectorHeath Stewart3-Dec-03 3:26 
GeneralRe: drawing graph with c# Pin
Heath Stewart2-Dec-03 20:00
protectorHeath Stewart2-Dec-03 20:00 

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.