SqlDataAdapter from web.config

  • Thread starter Thread starter pmz
  • Start date Start date
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
 
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
 
Back
Top