Serving Information Simply

Saturday 2 March 2013

SQL Server Connection Strings In ASP.NET Web Applications for different data providers.


A connection string provides the information that a provider needs to communicate with a particular database. The Connection String includes parameters such as the name of the driver, Server name and Database name , as well as security information such as user name and password.

An ADO.NET Data Provider is a class that can communicate with a specific type of database or data store. Usually Data Providers use a connection string containing a collection of parameters to establish the connection with the database through applications. The .NET Framework provides mainly three data providers, they are

  Microsoft SQL Server
  OLEDB
  ODBC

Here you can see how to make a connection string to the following ADO.NET Data Providers.

with ms-ACCESS 2007

using System.Data.OleDb; 
//2.
    OleDbConnection x; //Create connection
    OleDbCommand y; //to run sql command
    OleDbDataReader z; //To hold data
    private void button1_Click(object sender, EventArgs e)
        {
            //3
            x = new OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=d:\\database1.accdb");
            x.Open();
            //4   
       y = new OleDbCommand("select * from emp", x);
   //5
      z = y.ExecuteReader();
            //6
            while (z.Read())
            {
                //MessageBox.Show(z["empno"].ToString() + " " + z["ename"].ToString() + " " + z["sal"].ToString());    
                MessageBox.Show(z[0].ToString() + " " + z[1].ToString() + " " + z[2].ToString());    
            }
            //7
            x.Close();
      }

ACCESS --2003

   x = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=d:\\database1.mdb");

How to connect with Excel:-

  OleDbConnection x;
  OleDbCommand y;
  OleDbDataReader z;
        private void button1_Click(object sender, EventArgs e)
        {
            x = new OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=c:\\Book1.xlsx;extended properties=excel 12.0");
            x.Open();

            y = new OleDbCommand("select * from [sheet1$]", x);
            z = y.ExecuteReader();
            while (z.Read())
            {
      MessageBox.Show(z[0].ToString() + " " + z[1].ToString() + " " +     z[2].ToString());    
            }
            x.Close();
        }  
        
How to connect with Oracle

        OleDbConnection x;
        OleDbCommand y;
        OleDbDataReader z;
        private void button1_Click(object sender, EventArgs e)
        {
            x = new OleDbConnection("provider=msdaora;user id=scott;password=tiger");
            x.Open();

            y = new OleDbCommand("select * from emp", x);
            z = y.ExecuteReader();
            while (z.Read())
            {
 
                MessageBox.Show(z[0].ToString() + " " + z[1].ToString() + " " + z[2].ToString());    
            }
            x.Close();
        }

how to make connectvitiy with oracle using Oracleclient:-

..Add reference :- system.data.oracleclient

using System.Data.OracleClient;

        OracleConnection x;
        OracleCommand y;
        OracleDataReader z;
        private void button1_Click(object sender, EventArgs e)
        {
            x = new OracleConnection("user id=scott;password=tiger");
            x.Open();

            y = new OracleCommand("select * from emp", x);
            z = y.ExecuteReader();
            while (z.Read())
            {

                MessageBox.Show(z[0].ToString() + " " + z[1].ToString() + " " + z[2].ToString());
            }
            x.Close();
        }
-----------------------------------------------------------------------------------------

Thank you guys... keep visiting ….:)
If you like this post then please join this site and like it’s Facebook page for getting daily updates..   

No comments:

Post a Comment