Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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