SqlDataAdapter from web.config

P

pmz

Dear Friends,

I'm wondering whether it's possible to build a SqlDataAdapter using
stored configurations (including, connection string, defined
previously) in web.config file (same method you use to build
SqlConnection from ConnectionString stored in web.config)? (VS2010
with .NET 4.0 frm.)

Thank you and all the best,
Przemek M .Zawada
 
C

Cubaman

Dear Friends,

I'm wondering whether it's possible to build a SqlDataAdapter using
stored configurations (including, connection string, defined
previously) in web.config file (same method you use to build
SqlConnection from ConnectionString stored in web.config)? (VS2010
with .NET 4.0 frm.)

Thank you and all the best,
Przemek M .Zawada

Yes. Is not only possible, also recomended, in case you need to switch
to a different ddbb provider.
The code:

string connectionString =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string providerName =
ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;

System.Data.Common.DbProviderFactory factory =
System.Data.Common.DbProviderFactories.GetFactory(providerName);
System.Data.Common.DbConnection con = factory.CreateConnection();
con.ConnectionString = connectionString;
System.Data.Common.DbCommand cmd = factory.CreateCommand();
cmd.CommandText = "Update Products set UnitsInStock=UnitsInStock+10″;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();


In App.Config
<add name="ConnectionString" connectionString="server=.;Initial
Catalog=Northwind;..." providerName="System.Data.SqlClient" />

Best regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top