0% found this document useful (0 votes)
11 views7 pages

DatabaseConnectivity(1) (1)

The document outlines a C# Windows Forms application that connects to an Oracle database, creates a table, and performs basic CRUD operations. It includes code snippets for connecting to the database, executing SQL commands to fetch and insert data, and displaying data in a DataGridView. Additionally, it describes the creation of a function to count departments and how to manage data within a DataSet for display purposes.

Uploaded by

nojodar725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

DatabaseConnectivity(1) (1)

The document outlines a C# Windows Forms application that connects to an Oracle database, creates a table, and performs basic CRUD operations. It includes code snippets for connecting to the database, executing SQL commands to fetch and insert data, and displaying data in a DataGridView. Additionally, it describes the creation of a function to count departments and how to manage data within a DataSet for display purposes.

Uploaded by

nojodar725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Toolsconnect to database

Click on Test Connection


SQL> create table person01( name varchar(6), ID varchar(3), address
varchar(10));

Table created.

SQL> desc person01


Name Null? Type
----------------------------------------- --------
----------------------------
NAME VARCHAR2(6)
ID VARCHAR2(3)
ADDRESS VARCHAR2(10)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Oracle.DataAccess.Client;

namespace WindowsFormsApplication3
{

public partial class Form1 : Form


{
OracleConnection conn;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

public void ConnectDB()


{
conn = new OracleConnection("DATA SOURCE=172.16.54.24:1521/ictorcl;USER
ID=USER1;PASSWORD=student");
try
{
conn.Open();
MessageBox.Show("Connected");
}
catch (Exception e1)
{
}
}

private void button1_Click(object sender, EventArgs e)


{
// to fetch data from student;
ConnectDB();

OracleCommand command1 = conn.CreateCommand();

command1.CommandText = "select name,id,address from


person_033";

command1.CommandType = CommandType.Text;
OracleDataReader dr = command1.ExecuteReader();
dr.Read();
textBox1.Text = dr.GetString(0);
textBox2.Text = dr.GetString(1);
textBox3.Text = dr.GetString(2);
command1.Dispose();

conn.Close();
}

private void button2_Click(object sender, EventArgs e)


{
ConnectDB();

OracleCommand command1 = conn.CreateCommand();


command1.CommandText = "insert into person01 values('" + textBox1.Text + "','"
+ textBox2.Text + "','" + textBox3.Text + "')";
command1.CommandType = CommandType.Text;
command1.ExecuteNonQuery();
MessageBox.Show("Inserted");
command1.Dispose();
conn.Close();
}

}
}
Next step: go to solution explorer-> references-> right click-> add references

Build configuration manager

C:\Users\STUDENT\Documents\Visual Studio 2012\Projects\


WindowsFormsApplication1\WindowsFormsApplication1\

For executing the sql scripts


private void button5_Click(object sender, EventArgs e)
{
ConnectDB();
OracleCommand command1 = conn.CreateCommand();
command1.CommandText = "@C:/Users/STUDENT/Desktop/lab6.sql";
command1.CommandType = CommandType.Text;
command1.Dispose();
OracleCommand command2 = conn.CreateCommand();
command2.CommandText = "select deptcount(department) from instructor
group by(department)";
command2.CommandType = CommandType.Text;
OracleDataReader dr = command2.ExecuteReader();
dr.Read();
label8.Text = label8.Text+dr.GetInt32(0).ToString();

command2.Dispose();
conn.Close();
}
}

create or replace function deptcount(dept varchar)


return integer is
d_count integer;
begin
select count(*) into d_count
from instructor
where instructor.department = dept;
return d_count;
end;
/
To add data grids:

To display data in a table:

Drag and drop DataGridView from toolbox on the form. Right click on
dataGridView->add column (number of columns based on your requirement)->
Change the header text (Eg: deptname, name of instructor) as you want it to appear
in the form.

connect();

comm1 = new OracleCommand();

comm1.Connection = conn;

comm1.CommandText = "select * from instructor where deptname="+"'ict'";


comm1.CommandType = CommandType.Text;

OracleDataAdapter da = new OracleDataAdapter(comm1.CommandText, conn);

// An OracleDataAdapter object represents a data provider object that populates the


DataSet and updates changes in the DataSet to the Oracle database

DataSet ds = new DataSet(); // This is a collection of DataTables. We use the


DataSet type to store many DataTables in a single collection

da.Fill(ds, "instructor"); // DataAdapter.Fill (ds) here fetches the data from User
and fills in the DataSet ds.

dataGridView1.DataSource = ds.Tables[0]; // will access data at the 0th position.


Similarly you can have one more query, one more data adaptor object, fill data set
with some other relation’s data on which the query has to run ( in which case,
ds.table[1] will have that queries result which should be assigned to data grid view)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy