MIT-Curricular/DBMS/FullStack/Week8/WindowsFormsApplication1/WindowsFormsApplication1/Form1.cs
2025-03-18 17:31:27 +05:30

175 lines
6.4 KiB
C#

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;
using Oracle.DataAccess.Types;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
OracleConnection conn;
OracleCommand comm;
OracleDataAdapter da;
DataSet ds;
DataTable dt;
DataRow dr;
int i = 0;
public Form1()
{
InitializeComponent();
}
public void connect()
{
string constring = "DATA SOURCE=172.16.54.24:1521/ICTORCL;USER ID=CCE230953344;PASSWORD=student";
if (conn == null)
conn = new OracleConnection(constring); // Assuming 'constring' is properly defined
if (conn.State != ConnectionState.Open)
conn.Open();
}
private void Form1_Load(object sender, EventArgs e)
{
connect();
}
private void button1_Click(object sender, EventArgs e)
{
connect();
MessageBox.Show("CONNECTION ESTABLISHED.");
}
// SHOW TABLE ELEMENTS
private void button2_Click(object sender, EventArgs e)
{
comm = new OracleCommand();
comm.CommandText = "SELECT * FROM accident";
comm.CommandType = CommandType.Text;
ds = new DataSet();
da = new OracleDataAdapter(comm.CommandText, conn);
da.Fill(ds, "accident");
dt = ds.Tables["accident"];
int t = dt.Rows.Count;
MessageBox.Show(t.ToString());
dr = dt.Rows[i];
textBox1.Text = dr["REPORT_NUMBER"].ToString();
textBox2.Text = dr["ACCD_DATE"].ToString();
textBox3.Text = dr["LOCATION"].ToString();
textBox4.Text = dr["TOTAL_DAMAGE"].ToString();
conn.Close();
}
// NEXT
private void button3_Click(object sender, EventArgs e)
{
i++;
if (i >= dt.Rows.Count) i = 0;
dr = dt.Rows[i];
textBox1.Text = dr["REPORT_NUMBER"].ToString();
textBox2.Text = dr["ACCD_DATE"].ToString();
textBox3.Text = dr["LOCATION"].ToString();
textBox4.Text = dr["TOTAL_DAMAGE"].ToString();
}
// PREVIOUS
private void button4_Click(object sender, EventArgs e)
{
i--;
if (i < 0) i = dt.Rows.Count - 1;
dr = dt.Rows[i];
textBox1.Text = dr["REPORT_NUMBER"].ToString();
textBox2.Text = dr["ACCD_DATE"].ToString();
textBox3.Text = dr["LOCATION"].ToString();
textBox4.Text = dr["TOTAL_DAMAGE"].ToString();
}
// INSERT INTO TABLE ACCIDENT
private void button5_Click(object sender, EventArgs e)
{
// Parse inputs to match database column types
int reportNumber = int.Parse(textBox1.Text);
DateTime accDate = DateTime.Parse(textBox2.Text);
int totalDamage = int.Parse(textBox4.Text);
try
{
if (conn == null || conn.State != ConnectionState.Open)
connect();
OracleCommand cm = new OracleCommand();
cm.Connection = conn;
cm.CommandText = "INSERT INTO accident (REPORT_NUMBER, ACCD_DATE, LOCATION, TOTAL_DAMAGE) " +
"VALUES (:reportNumber, :accDate, :location, :totalDamage)";
cm.Parameters.Add(new OracleParameter("reportNumber", OracleDbType.Int32) { Value = reportNumber });
cm.Parameters.Add(new OracleParameter("accDate", OracleDbType.Date) { Value = accDate });
cm.Parameters.Add(new OracleParameter("location", textBox3.Text));
cm.Parameters.Add(new OracleParameter("totalDamage", OracleDbType.Int32) { Value = totalDamage });
cm.ExecuteNonQuery();
MessageBox.Show("Record inserted successfully!");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
// UPDATE CLICK
private void button6_Click(object sender, EventArgs e)
{
try
{
if (conn == null || conn.State != ConnectionState.Open)
connect();
int reportNumber = int.Parse(textBox1.Text);
DateTime accDate = DateTime.Parse(textBox2.Text); // Ensure this parses correctly
string location = textBox3.Text;
int totalDamage = int.Parse(textBox4.Text);
OracleCommand cm = new OracleCommand();
cm.Connection = conn;
cm.CommandText = "UPDATE accident SET ACCD_DATE = :edate, " +
"LOCATION = :eloc, TOTAL_DAMAGE = :edamage " +
"WHERE REPORT_NUMBER = :erep";
// Add parameters with explicit OracleDbType
cm.Parameters.Add(new OracleParameter("erep", OracleDbType.Int32) { Value = reportNumber });
cm.Parameters.Add(new OracleParameter("edate", OracleDbType.Date) { Value = accDate }); // Ensure correct type
cm.Parameters.Add(new OracleParameter("eloc", OracleDbType.Varchar2) { Value = location });
cm.Parameters.Add(new OracleParameter("edamage", OracleDbType.Int32) { Value = totalDamage });
cm.ExecuteNonQuery();
MessageBox.Show("Record updated successfully!");
}
catch (FormatException ex)
{
MessageBox.Show("Invalid input format: " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
finally
{
if (conn != null && conn.State == ConnectionState.Open)
conn.Close();
}
}
}
}