.NET Framework - Comment récupérer une 'connection string' depuis app.config ?
Asked By Yan
20-Nov-09 06:30 AM
Bonjour à toutes et à tous,
En WinForm sous VS 2008, je cherche en vain depuis quelque temps déjà, à
récupérer une chaine de connexion stockée dans mon fichier "app.config",
j'ai trouvé quelques exemples mais je dois mal m'y prendre, car je n'arrive
pas à les faire fonctionner.
Voici mes tentatives :
ConnectionStringSettings settings =
ConfigurationManager.ConnectionStrings["ma_chaine_de_connexion"];
Il ne connait pas ni "ConnectionStringSettings", ni "ConfigurationManager".
Alors dans l'exemple de MS, ils indiquent que l'on doit faire une référence
à "System.Configuration.dll" mais quand je tape "using
System.Configuration." il ne me propose pas ".dll" est-ce là mon problème ?
Merci d'avance pour votre aide.
Yan
ConfigurationManager.ConnectionStrings
(1)
IIS
(1)
ConnectionStringSettings
(1)
ConfigurationManager
(1)
WinForm
(1)
EAme
(1)
EEne
(1)
Voici
(1)
Yan replied to Yan
Ah ! ça y est j'ai trouvé, il fallait que j'ajoute une référence au fichier
faire l'importation :
using System.Configuration;
A+
Yan
-----------------------------
Gloops replied to Yan
Yan a =E9crit, le 20/11/2009 12:30 :
, =E0=20
onfig",=20
arrive=20
er".
=E9rence=20
l=E8me ?
Je dirais qu'=E0 cela j'ai ajout=E9 une question subsidiaire il y a quelq=
ues=20
semaines : est-ce vraiment raisonnable de mettre un site en ligne en=20
laissant cette cha=EEne en clair dans le web.config ?
(Je sais, si tu attaques par app.config, nous ne parlons pas vraiment de =
la m=EAme chose.)
La r=E9ponse =E9tait non, et sur comment faire, =E7a s'est jou=E9 dans le=
=20
newsgroup iis. Ah oui mais alors si apr=E8s l'h=E9bergeur tra=EEne la jam=
be=20
pour donner acc=E8s =E0 iis, =E7a ne nous met pas =E0 jour. Et comme solu=
tion je=20
ne vois plus gu=E8re que ce que quelqu'un a appel=E9 "le cryptage par=20
l'obscurantisme", ce qui a des petits c=F4t=E9s pas tr=E8s orthodoxes.
Jusqu'=E0 ce que l'h=E9bergeur finisse par se rappeler que si il a mis su=
r=20
sa page d'accueil une mention d'IIS, =E7a ferait bon effet qu'il y ait=20
quelque chose derri=E8re.
How can ASPNET access the Northwind database .NET Framework Hello!! I use IIS 5 and SQL Server Express 2005 Acccount ASPNET is used when running IIS to access the SQL Server database. This account ASPNET get access to the database server the web.config file protected void Page_Load(object sender, EventArgs e) { SqlConnection sqlConn = null; try { ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"]; string connString = cs.ConnectionString; sqlConn = new SqlConnection(connString); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandText understand how ASPNET can access the Nortwind database. / / Tony ASP.NET Discussions SQL Server (1) ConfigurationManager.ConnectionStrings (1) SqlConnection (1) SqlCommand (1) EventArgs (1) ASP.NET (1) IIS (1) Response.Write (1) Integrated Security = True meaning your login information on the machine you
System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection sqlConn = null; try { ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["MyConnectionString"]; string connString = cs.ConnectionString; sqlConn = new SqlConnection(connString); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandText Integrated Security = True" providerName = "System.Data.SqlClient" / > TEST NUMBER 2 using the Northwind database running IIS Was the same as Test 1 except that I run from IIS so the result back was HEMPC \ ASPNET If I in this Test 2 did not Instance = True" providerName = "System.Data.SqlClient" / > TEST NUMBER 4 using the ASPNETDB.MDF running from IIS Was the same as Test number 3 except that I run from IIS When I run this I got this error message. Cannot open user default database. Login error message Cannot open user default database. Login failed. ASP.NET Discussions SQL Server (1) ConfigurationManager.ConnectionStrings (1) SqlConnection (1) SqlCommand (1) EventArgs (1) ASP.NET (1) IIS (1) Response.Write
have some kind information that give a solution to my problem why I cannot use IIS and forms authentication when having the database ASPNETDB.MDF ? / / Tony ASP.NET Discussions SQL Server (1) ConfigurationManager.ConnectionStrings (1) SqlConnection (1) SqlCommand (1) EventArgs (1) ASP.NET (1) IIS (1) XP (1) For now it seems just that the ASPNET account (under which IIS 5 runs by default) is not allowed to access the ASPNETDB database. Have you already Cannot open user default database. Login failed. Login failed for user 'HEMPC \ ASPNET'. when using IIS which is using account ASPNET to access ASPNETDB.MDF / / Tony I do have SQL Server a bit more on ASP.NET security, such as how to configure the ASP.NET, IIS and which account is running the ASP.NET. In your case, when you run the your computer), so, you do not have a problem. When you run the app with IIS, it is complicated issue: the account used to run your ASP.NET app could be
NET Framework Hi How to get connection string from web.config? SF ASP.NET Discussions ConfigurationManager.ConnectionStrings (1) ASP.NET (1) VB (1) Subscribe (1) ConnStringName (1) Windows (1) Beamer (1) Safer (1) strConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString; - - Mark Rae ASP.NET MVP http: / / www.markrae.net / / C# string connectionString ConfigurationManager.ConnectionStrings["connStringName"].ToString(); 'VB Dim connectionString as String = _ ConfigurationManager.ConnectionStrings("connStringName").ToString() - - Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA Subscribe to my blog