Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Subclassing Connections
Message
De
10/06/2005 10:47:51
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 1.1
OS:
Windows XP SP2
Database:
MS SQL Server
Divers
Thread ID:
01022145
Message ID:
01022156
Vues:
19
Bernard,

You can put your connection string in your config file. Then your Connection class might look something like this:
public class MyConnection
{
	#region Methods
	public SqlConnection GetConnection()
	{
		string cConnection = ConfigurationSettings.AppSettings["ConnectionString"];
		if (cConnection == null)
		{
			// This is here as a default, in case when developing, you don't get around 
			// to setting up a Connection string in your config. Alternatively, you might want to
			// capture the error. It's up to you
			cConnection = "server=(local);database=MyDefaultDatabase;uid=MyDefaultID;pwd=MyDefaultPwd";
		}
		SqlConnection conn = new SqlConnection(cConnection);
		return conn;
	}
	#endregion
}
The connection string in your config file would look something like this:
  <appSettings>
    <add key="ConnectionString" value="server=(local);database=MyDefaultDatabase;uid=MyDefaultID;pwd=MyDefaultPwd" />
  </appSettings>
Does this help any?

~~Bonnie



>Hi,
>
>I'm pretty new to .Net and I'm trying to work out a way of creating a connection class.
>
>I'm from a Foxpro background and in foxpro I would create a abstract class that represents a connection with all the PEMs required to represent that connection.
>
>I would then create a subclass for use in my application and specify the application specific property settings there.
>
>Now I trying to achieve the same thing in .Net without much success.
>
>Here's what I'm trying to do ;
>
>This would be my abstract class :-
>
>
>Public Class aConnection : Connection
>{
>}
>
>
>and then create a subclass of this in my application with the connection string specified at this level.
>
>
>Public Class oAppConnection : cConnection
>{
>    This.ConnectionString = "myapplicationspecificsettings"
>}
>
>
>I could then create an instance of this class and the call the open method and I have the desired connection.
>
>The idea being that I would only have to specify the connection setting in one place ie the oAppConnection class.
>
>Having a connection string floating around the place which I pass into a connections open method doesn't seem quite right to me, but I may be wrong...
>
>I would also ideally like to be able to create difference connection strings depending on different database backends. So I could have a factory class that I call and that works out which connection class to instantiate.
>
>Can anybody shed any light on this?
>
>Many Thanks, Bernard......
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform