.NET Framework - SqlDataAdapter from web.config

Asked By pmz on 04-May-11 02:05 PM
Dear Friends,

I am wondering whether it is 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




Cubaman replied to pmz on 11-May-11 04:30 AM
Yes. Is not only possible, also recomended, in case you need to switch
to a different ddbb provider.
The code:

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

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


In App.Config
Catalog=3DNorthwind;..." providerName=3D"System.Data.SqlClient" />

Best regards