How to connect database through oleDb in ADO.NET

By
In this tutorial,i am going to implement 'How to connect database(sql,mysql,oracal) through oleDb'.we can connect any database through oleDb but we can connect only sql database using Sql .For oleDb uses oleDbConnection but for Sql  uses SqlConnection.We can use Disconnected Architecture in oleDb connection also.
  • For oleDb we use following Namespace which are given below:-
         using System.Data.OleDb;
  • For Sql we use following Namespace which are given below:-
           using System.Data.SqlClient;
We can connect any database through oleDb using c# in Ado.Net.I have implement both concepts on same table (students) in master database of sql server.
There are some steps to implement this concept on windowsFormsApplication(use website also) which are given below:-

Step 1:- First open your visual studio -->File-->New -->Project-->Select WindowsFormsApplication --> OK-->Drag and Drop comboBox and Button controls from toolBox as shown below:-


form
Step 2:- Create a students table in your sql server with three column such as sid ,name,age .I have already created in sql server.

Step 3:- Now write the c# codes on Submit 1 and Submit 2 Button as given below:-
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection("provider=sqloleDb; data source = Ramashanker-PC;INTEGRATED SECURITY = sspi;database=master");
            con.Open();
            OleDbCommand cmd = new OleDbCommand("select name from students", con);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                comboBox1.Items.Add(dr[0].ToString());
            }
            con.Close();
        }
        private void button2_Click(object sender, EventArgs e)
        {
           SqlConnection con = new SqlConnection("data source=Ramashanker-PC;Integrated Security=Yes;Database=master");
            con.Open();
            SqlCommand cmd = new SqlCommand("select name from students", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                comboBox1.Items.Add(dr[0].ToString());
            }
            con.Close();
        }
    }
}

Step 4:- Now Run the Application(press F5)-->Press any Submit Button (Submit 1 ,Submit 2)-->you will see following output as shown below:-


win_output
For More...
  1. Ado.Net Real application
  2. Connection strings in sql server
  3. Connection Architecture in Ado.Net
  4. Views in sql server
  5. Transaction in sql server
  6. Web services in asp.net
  7. Joins in sql server

Download Attached file from below
             Download

0 comments:

Post a Comment

Powered by Blogger.