MTech DBMS Lab Programs1 (1)

Click here to load reader

  • date post

    27-Oct-2015
  • Category

    Documents

  • view

    542
  • download

    5

Embed Size (px)

Transcript of MTech DBMS Lab Programs1 (1)

Advances in Database Management Laboratory 2012-2013

Program No. 1Develop a database application to demonstrate storing and retrieving of BLOB and CLOB objectusing System;using System.Collections.Generic;using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Data.SqlClient;

using System.Windows.Forms;

using System.IO;

namespace BLOB

{

public partial class Form1 : Form

{

Image curImage;

String curFilename;

String connectionString = "server=localhost;database=college;user id=sa;password=password";

String savedImageName = "";

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

OpenFileDialog openDlg = new OpenFileDialog();

if (openDlg.ShowDialog() == DialogResult.OK)

{

curFilename = openDlg.FileName;

textBox3.Text = curFilename;

}

}

private void button2_Click(object sender, EventArgs e)

{

if (textBox3.Text != "")

{

FileStream file = new FileStream(curFilename, FileMode.OpenOrCreate, FileAccess.Read);

byte[] rawdata = new byte[file.Length];

file.Read(rawdata, 0, System.Convert.ToInt32(file.Length));

file.Close();

String sql = "SELECT * FROM EMPLOYEES";

SqlConnection con = new SqlConnection();

con.ConnectionString = connectionString;

con.Open();

SqlDataAdapter adapter = new SqlDataAdapter(sql, con);

SqlCommandBuilder cmb = new SqlCommandBuilder(adapter);

DataSet ds = new DataSet("EMPLOYEES");

adapter.Fill(ds, "EMPLOYEES");

DataRow row = ds.Tables["EMPLOYEES"].NewRow();

row["empcode"] = textBox1.Text;

row["ename"] = textBox2.Text;

row["photo"] = rawdata;

ds.Tables["EMPLOYEES"].Rows.Add(row);

adapter.Update(ds, "EMPLOYEES");

con.Close();

MessageBox.Show("Image Saved");

}

}

private void button3_Click(object sender, EventArgs e)

{

if (textBox1.Text != "")

{

String sql = "Select * from employees where empcode=" + textBox1.Text;

SqlConnection con = new SqlConnection();

con.ConnectionString = connectionString;

con.Open();

FileStream file;

BinaryWriter bw;

int buffersize = 100;

byte[] outbyte = new byte[buffersize];

long retval;

long startindex = 0;

SqlCommand command = new SqlCommand(sql, con);

SqlDataReader myReader = command.ExecuteReader(CommandBehavior.SequentialAccess);

savedImageName = textBox3.Text;

while (myReader.Read())

{

file = new FileStream(savedImageName, FileMode.OpenOrCreate, FileAccess.Write );

bw = new BinaryWriter(file);

startindex = 0;

retval = myReader.GetBytes(2, startindex, outbyte, 0, buffersize);

while (retval == buffersize)

{

bw.Write(outbyte);

bw.Flush();

startindex += buffersize;

retval = myReader.GetBytes(2, startindex, outbyte, 0, buffersize);

}

bw.Write(outbyte, 0, (int)retval - 1);

bw.Flush();

bw.Close();

file.Close();

}

con.Close();

curImage = Image.FromFile(savedImageName);

pictureBox1.Image = curImage;

pictureBox1.Invalidate();

con.Close();

}

}

}

}Output:

Program No. 2

Develop a database application to demonstrate the representation of multivalued attributes, and use of nested tables to represent complex objects. Write suitable queries to demonstrate it.SQL> CREATE TYPE t_experience AS OBJECT (CompanyName varchar2(20),Position varchar2(20),NoOfYears number(2));

Type created.

SQL> CREATE TYPE t_experience_tbl AS TABLE OF t_experience;

Type created.

SQL> CREATE TABLE employees ( Name varchar2(20),Experiences t_experience_tbl)NESTED TABLE Experiences STORE AS Experiences_tab;

Table created.

SQL> insert into employees values( 'jag', t_experience_tbl

( t_experience('abc company','Software Engineer',3),

t_experience('xyz company','System Analyst',2),

t_experience('mnp company','Research fellow',4)

));

1 row created.

SQL> insert into employees values

( 'gaj', t_experience_tbl ( t_experience('git','ai',1),

t_experience('git','l',3)

));

1 row created.

Program No. 3

Design and develop a suitable Student Database application. One of the attributes to me maintained is the attendance of a student in each subject for which he/she has enrolled. Using TRIGGERS, we write active rules to do the following:

A. Whenever attendance is updated, check if the attendance is less than 85%; if so notify the Head of Department concerned.

B. Whenever the marks in the Internal Assessment Test are entered, check if the marks are less than 40%; if so, notify the Head of the Department concerned.PART A:SQL> create table attendance(

usn varchar2(10) primary key,

att1 integer,

att2 integer,

att3 integer,

att4 integer,

att5 integer,

att6 integer

);

Table created.

SQL> insert into attendance values('2AE05cs001', 10,10,10,10,10,10);

1 row created.

SQL> insert into attendance values('2AE05cs002', 11,11,11,11,11,11);

1 row created.

SQL> insert into attendance values('2AE05cs003', 10,11,8,7,10,11);

1 row created.

SQL> select * from attendance;

USN ATT1 ATT2 ATT3 ATT4 ATT5 ATT6

---------- ---------- ---------- ---------- ---------- ---------- ----------

2AE05cs001 10 10 10 10 10 10

2AE05cs002 11 11 11 11 11 11

2AE05cs003 10 11 8 7 10 11

SQL> create or replace trigger atrigger

after update on attendance

for each row

declare

attn_exception1 exception;

attn_exception2 exception;

attn_exception3 exception;

attn_exception4 exception;

attn_exception5 exception;

attn_exception6 exception;

total_classes integer;

begin

total_classes:=&total_classes;

if :new.att1/total_classes*100 insert into marks values('2AE05cs018', 20,11,18,17,10,11);

1 row created.

SQL> select * from marks;

USN M1 M2 M3 M4 M5 M6

---------- ---------- ---------- ---------- ---------- ---------- ----------

2gi05cs012 10 10 10 10 10 10

2gi05cs002 11 11 11 11 11 11

2gi05cs018 20 11 18 17 10 11

SQL> create or replace trigger mtrigger

after insert or update on marksfor each row

declare

marks_exception1 exception;marks_exception2 exception;

marks_exception3 exception;

marks_exception4 exception;

marks_exception5 exception;

marks_exception6 exception;

maximum_marks integer;

begin

maximum_marks:=&maximum_marks;

if :new.m1/maximum_marks*100