0% found this document useful (0 votes)
208 views12 pages

Visual Studio C# 2019 (Service-Based Database)

The document contains C# code for a student database application. It defines classes for connecting to a SQL database and performing CRUD operations on student records in tables. Methods are included for opening a database connection, inserting, updating, deleting records, searching records, and loading records into a datagrid on a form for display and editing.

Uploaded by

Kenneth Delfino
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
208 views12 pages

Visual Studio C# 2019 (Service-Based Database)

The document contains C# code for a student database application. It defines classes for connecting to a SQL database and performing CRUD operations on student records in tables. Methods are included for opening a database connection, inserting, updating, deleting records, searching records, and loading records into a datagrid on a form for display and editing.

Uploaded by

Kenneth Delfino
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

Connection > connection.

cs

using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsFormsApp1.connection
{
class connection
{

private static string dbconn = "Data


Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=E:\\DatabaseSystem\\WindowsFormsApp1\\WindowsFormsApp1\\Testdb.mdf;Integrated
Security=True";
public static SqlConnection conn = new SqlConnection();

public static void DB()


{
try
{
conn = new SqlConnection(dbconn);
conn.Open();
}
catch (Exception ex)
{
conn.Close();
MessageBox.Show(ex.Message);
}
}

}
Function > function.cs

using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsFormsApp1.function
{
class function : Form1
{

public function() : base() { }

public static SqlDataReader dataReader = null;


public static void datagridfill(string q, DataGridView dgv)
{
try
{
connection.connection.DB();
DataTable dt = new DataTable();
SqlDataAdapter data = null;
SqlCommand command = new SqlCommand(q, connection.connection.conn);
data = new SqlDataAdapter(command);
data.Fill(dt);
dgv.DataSource = dt;
connection.connection.conn.Close();

}
catch (Exception ex)
{
Console.Write(ex);
}
}

}
}
SUBMIT-BUTTON

private void Btnsubmit_Click(object sender, EventArgs e)

try
{
connection.connection.DB();
string gen = "Insert into stud(studid, studname)values(" + txtstudid.Text + ",'" + txtstudname.Text + "')";
SqlCommand command = new SqlCommand(gen, connection.connection.conn);
command.ExecuteNonQuery();
MessageBox.Show("Successfully Saved");
Form1_Load(sender, e);
Clear();
connection.connection.conn.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Clear()
{
txtstudid.Clear();
txtstudname.Clear();
}
EDIT-BUTTON

private void Btnedit_Click(object sender, EventArgs e)


{
try
{
connection.connection.DB();

string q = "update stud SET studname = '" + txtstudname.Text + "' where studid = " +
txtstudid.Text + "";
SqlCommand comm = new SqlCommand(q, connection.connection.conn);
comm.ExecuteNonQuery();
MessageBox.Show("Edited Successfully!");
Form1_Load(sender, e);
Clear();
connection.connection.conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}

}
DELETE-BUTTON

private void Btndelete_Click(object sender, EventArgs e)


{
try
{
connection.connection.DB();
string q = "delete from stud where studid = " + txtstudid.Text + " ";
SqlCommand comm = new SqlCommand(q, connection: connection.connection.conn);
comm.ExecuteNonQuery();
MessageBox.Show("Data Deleted Successfully!");
Form1_Load(sender, e);
Clear();
connection.connection.conn.Close();

}
catch (Exception x)
{
MessageBox.Show(x.Message);
}

}
TXTSEARCH_TEXTCHANGED

private void Txtsearch_TextChanged(object sender, EventArgs e)


{
try
{
connection.connection.DB();
string a = "Select * from stud where studid LIKE " + "'%" + txtsearch.Text + "%' or studname LIKE '%" + txtsearch.Text +
"%'";
function.function.datagridfill(a, dataGridView1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
DATAGRIDVIEW_CELLMOUSECLICK

private void DataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)


{
try
{
connection.connection.DB();
String a = "SELECT * FROM stud where studid = " + dataGridView1.CurrentRow.Cells[0].Value + "";
SqlCommand comm = new SqlCommand(a, connection: connection.connection.conn);

function.function.dataReader = comm.ExecuteReader();
if (function.function.dataReader.Read())
{
txtstudid.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
txtstudname.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
FORM1

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 System.Data.SqlClient;

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

private void Form1_Load(object sender, EventArgs e)


{
string s = "Select * from stud";
function.function.datagridfill(s, dataGridView1);
}

private void Btnsubmit_Click(object sender, EventArgs e)


{

try
{
connection.connection.DB();
string gen = "Insert into stud(studid, studname)values(" + txtstudid.Text + ",'" + txtstudname.Text + "')";
SqlCommand command = new SqlCommand(gen, connection.connection.conn);
command.ExecuteNonQuery();
MessageBox.Show("Successfully Saved");
Form1_Load(sender, e);
Clear();
connection.connection.conn.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Clear()
{
txtstudid.Clear();
txtstudname.Clear();
}

private void Btnedit_Click(object sender, EventArgs e)


{
try
{
connection.connection.DB();

string q = "update stud SET studname = '" + txtstudname.Text + "' where studid = " +
txtstudid.Text + "";
SqlCommand comm = new SqlCommand(q, connection.connection.conn);
comm.ExecuteNonQuery();
MessageBox.Show("Edited Successfully!");
Form1_Load(sender, e);
Clear();
connection.connection.conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}

private void Btndelete_Click(object sender, EventArgs e)


{
try
{
connection.connection.DB();
string q = "delete from stud where studid = " + txtstudid.Text + " ";
SqlCommand comm = new SqlCommand(q, connection: connection.connection.conn);
comm.ExecuteNonQuery();
MessageBox.Show("Data Deleted Successfully!");
Form1_Load(sender, e);
Clear();
connection.connection.conn.Close();

}
catch (Exception x)
{
MessageBox.Show(x.Message);
}

}
private ‘void Txtsearch_TextChanged(object sender, EventArgs e)
{
try
{
connection.connection.DB();
string a = "Select * from stud where studid LIKE " + "'%" + txtsearch.Text + "%' or studname LIKE '%" + txtsearch.Text +
"%'";
function.function.datagridfill(a, dataGridView1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void DataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)’


{
Try’
{
connection.connection.DB();
String a = "SELECT * FROM stud where studid = " + dataGridView1.CurrentRow.Cells[0].Value + "";
SqlCommand comm = new SqlCommand(a, connection: connection.connection.conn);

function.function.dataReader = comm.ExecuteReader();
if (function.function.dataReader.Read())
{
txtstudid.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
txtstudname.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

You might also like