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.
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:-
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.
- For oleDb we use following Namespace which are given below:-
- For Sql we use following Namespace which are given below:-
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:-
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:-
For More...
- Ado.Net Real application
- Connection strings in sql server
- Connection Architecture in Ado.Net
- Views in sql server
- Transaction in sql server
- Web services in asp.net
- Joins in sql server
Download Attached file from below
Download
0 comments:
Post a Comment