0% found this document useful (0 votes)
34 views3 pages

INTEPRO Reviewer1: Using Using Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

This document contains C# code for a Windows Forms application that performs CRUD (create, read, update, delete) operations on a SQL database table. It defines methods for loading data from the table, inserting a new record, updating an existing record, and deleting a record based on the primary key ID. The data is bound to a data grid view for display. Connection strings and SQL commands with parameters are used to interface with the database.

Uploaded by

Xiu
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)
34 views3 pages

INTEPRO Reviewer1: Using Using Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

This document contains C# code for a Windows Forms application that performs CRUD (create, read, update, delete) operations on a SQL database table. It defines methods for loading data from the table, inserting a new record, updating an existing record, and deleting a record based on the primary key ID. The data is bound to a data grid view for display. Connection strings and SQL commands with parameters are used to interface with the database.

Uploaded by

Xiu
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/ 3

INTEPRO Reviewer1

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 CRUD
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

string cs = "SERVER = TAFT-CL304;" + "DATABASE = Database_Name_TI101; UID = sa;


PWD = benilde";

private void Form1_Load(object sender, EventArgs e)


{
button4_Click(sender, e);
}

private void button4_Click(object sender, EventArgs e)


{
//SqlConnection
SqlConnection con = new SqlConnection();
con.ConnectionString = cs;

con.Open();

//SqlCommand
SqlCommand com = new SqlCommand();
com.CommandText = "SELECT * FROM Table";
com.Connection = con;

//SqlDataAdapter
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;

//Data Table
DataTable dt = new DataTable();
da.Fill(dt);

//Display in dgv
dgv.DataSource = dt;
con.Close();
}

private void button1_Click(object sender, EventArgs e)


{
//SqlConnection
SqlConnection con = new SqlConnection();
con.ConnectionString = cs;

con.Open();

//SqlCommand
SqlCommand com = new SqlCommand();
com.CommandText = "INSERT INTO Table VALUES (@id, @n, @d, @a)";
com.Parameters.AddWithValue("@id", txtID.Text);
com.Parameters.AddWithValue("@n", txtN.Text);
com.Parameters.AddWithValue("@d", txtD.Text);
com.Parameters.AddWithValue("@a", txtA.Text);
com.Connection = con;
com.ExecuteNonQuery();

con.Close();
MessageBox.Show("added successfully");

//Clean up
txtID.Clear(); txtN.Clear(); txtD.Clear(); txtA.Clear();
button4_Click(sender, e);
txtID.Select();
}

private void button2_Click(object sender, EventArgs e)


{
//SqlConnection
SqlConnection con = new SqlConnection();
con.ConnectionString = cs;

con.Open();

//SqlCommand
SqlCommand com = new SqlCommand();
com.CommandText = "UPDATE Table SET Name = @n," + "Description = @d," + "Age
= @a WHERE ID = @id";
com.Parameters.AddWithValue("@id", txtID.Text);
com.Parameters.AddWithValue("@n", txtN.Text);
com.Parameters.AddWithValue("@d", txtD.Text);
com.Parameters.AddWithValue("@a", txtA.Text);
com.Connection = con;
com.ExecuteNonQuery();

con.Close();
MessageBox.Show("edited successfully!");

//Clean up
txtID.Clear(); txtN.Clear(); txtD.Clear(); txtA.Clear();
button4_Click(sender, e);
txtID.Select();
}

private void button3_Click(object sender, EventArgs e)


{
//SqlConnection
SqlConnection con = new SqlConnection();
con.ConnectionString = cs;
con.Open();

//SqlCommand
SqlCommand com = new SqlCommand();
com.CommandText = "DELETE FROM Table WHERE ID = @id";
com.Parameters.AddWithValue("@id", txtID.Text);
com.Connection = con;
com.ExecuteNonQuery();

con.Close();
MessageBox.Show("Deleted successfully");

//Clean up
txtID.Clear(); txtN.Clear(); txtD.Clear(); txtA.Clear();
button4_Click(sender, e);
txtID.Select();
}
}
}

You might also like