.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Provider Agonostic Code

Posted By:BangaruBabuPureti       Posted Date: May 13, 2010    Points: 10    Category: ADO.NET    URL: http://www.dotnetspark.com  

Code Work For Independent Of Database Providers

protected void btnSubmit_Click(object sender, EventArgs e)


        //Getting Provider Name & Connection String From Web.Config File

        string providerName = ConfigurationManager.ConnectionStrings["GenericDBConnectionString"].ProviderName;

        string ConString = ConfigurationManager.ConnectionStrings["GenericDBConnectionString"].ConnectionString;

        //Step-1: Create Factory object

        DbProviderFactory facObj = DbProviderFactories.GetFactory(providerName);

        //Step-2: Create Connection For Factory

        DbConnection cn = facObj.CreateConnection();

        //Step-3: Assign Connection String To Connection Object

        cn.ConnectionString = ConString;

        //Step-4: Create Command For Database Manipulations with ConnectionString

        DbCommand cmd = cn.CreateCommand();

        //Step-5: Bind The Properties For Command Object

        cmd.CommandType = CommandType.Text;

        cmd.CommandText = "insert into tbl_profile(Name,PhoneNo,Address)values('" + txtname.Text + "','" + txtphoneno.Text + "','" + txtaddress.Text + "')";

        //Step-6: Open Connection -> Execute Query -> Close Connection




        Page.ClientScript.RegisterStartupScript(typeof(string), "popupalert", "");


    catch (Exception ex)

        Page.ClientScript.RegisterStartupScript(typeof(string), "popupalert", "");






Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend