Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace QLHS
{
class clsProvider
{

public static SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;


Initial Catalog=QLHOCSINH; Integrated Security=True");

public static DataTable ExecuteQuery(string query, object[] values=null)


{
DataTable dt = new DataTable() ;
con.Open();

SqlCommand cmd = new SqlCommand(query, con);


if(values!=null)
{
string[] listpara = query.Split(' ');
int i = 0;
foreach(string para in listpara)
{
if(para.Contains('@'))
{
cmd.Parameters.AddWithValue(para, values[i++]);
}
}
}

SqlDataAdapter da = new SqlDataAdapter(cmd);


da.Fill(dt);
con.Close();
return dt;
}

public static int ExecuteNonQuery(string query, object[]values=null)


{
int data = 0;
con.Open();

SqlCommand cmd = new SqlCommand(query, con);


if(values!=null)
{
string[] listpara = query.Split(' ');
int i = 0;
foreach(string para in listpara)
{
if(para.Contains('@'))
{
cmd.Parameters.AddWithValue(para, values[i++]);
}
}
}

data = cmd.ExecuteNonQuery();
con.Close();

return data;
}
}
}

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

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 QLHS
{
public partial class frmLop_p : Form
{
//string sSelect = @"Select * FROM Lop Where Tenlop = @Tenlop";
string sSelect = @"Select * FROM Lop";
bool isSua = false;
public frmLop_p()
{
InitializeComponent();
}

private void frmLop_p_Load(object sender, EventArgs e)


{

// dgvLop.DataSource = clsProvider.ExecuteQuery(sSelect, new


object[] {"Lop 01"});
dgvLop.DataSource = clsProvider.ExecuteQuery(sSelect);

dgvLop.Columns[0].HeaderText = "Mã số";


dgvLop.Columns[1].HeaderText = "Tên lớp";
dgvLop.Columns[2].HeaderText = "Sĩ số";

dgvLop.Columns[0].Width = 100;
dgvLop.Columns[1].Width = 300;
dgvLop.Columns[2].Width = 100;

//DataGridViewRow r = dgvLop.SelectedRows[0];
//txtMalop.Text = r.Cells["MALOP"].Value.ToString();
//txtTenlop.Text = r.Cells["TENLOP"].Value.ToString();
//txtSiso.Text = r.Cells["SISO"].Value.ToString();
btnThem.Enabled = true;
btnXoa.Enabled = true;
btnSua.Enabled = true;
btnHuy.Enabled = false;
btnLuu.Enabled = false;

private void btnThem_Click(object sender, EventArgs e)


{
txtMalop.Text = "";
txtTenlop.Text = "";
txtSiso.Text = "";

btnThem.Enabled = false;
btnXoa.Enabled = false;
btnSua.Enabled = false;
btnHuy.Enabled = true;
btnLuu.Enabled = true;

txtMalop.ReadOnly = false;
txtTenlop.ReadOnly = false;
txtSiso.ReadOnly = false;
}

private void btnXoa_Click(object sender, EventArgs e)


{
if(MessageBox.Show("Ban có chắc xóa không?")== DialogResult.OK)
{
string sDelete = "Delete from Lop Where Malop = @Malop";

clsProvider.ExecuteNonQuery(sDelete, new object[] { txtMalop.Text });

LoadData();
}
}

private void btnSua_Click(object sender, EventArgs e)


{
isSua = true;
txtMalop.ReadOnly = false;
txtTenlop.ReadOnly = false;
txtSiso.ReadOnly = false;

btnThem.Enabled = false;
btnXoa.Enabled = false;
btnSua.Enabled = false;
btnHuy.Enabled = true;
btnLuu.Enabled = true;
}

private void btnHuy_Click(object sender, EventArgs e)


{
SelectData();

btnThem.Enabled = true;
btnXoa.Enabled = true;
btnSua.Enabled = true;
btnHuy.Enabled = false;
btnLuu.Enabled = false;

txtMalop.ReadOnly = true;
txtTenlop.ReadOnly = true;
txtSiso.ReadOnly = true;
}

private void btnLuu_Click(object sender, EventArgs e)


{
//string sInsert = "Insert into Hocsinh values ('" + txtMahs.Text + "', N'" +
txtTenhs.Text + "',N'" + cboGioitinh.Text + "','" + dtpNgaySinh.Text + "','" +
txtDiachi.Text + "','" + cbLop.SelectedValue.ToString() + "')";
int data=0;
if (!isSua)
{
string sInsert = "Insert into Lop values ( @Malop , @Tenlop , @Siso )";
data = clsProvider.ExecuteNonQuery(sInsert, new object[] { txtMalop.Text,
txtTenlop.Text, int.Parse(txtSiso.Text) });
}else
{
//string sUpdate = "Insert into Lop values ( @Malop , @Tenlop , @Siso )";
string sUpdate = "Update Lop set Tenlop = @Tenlop , Siso = @Siso Where
Malop = @Malop" ;
data = clsProvider.ExecuteNonQuery(sUpdate, new object[] {
txtTenlop.Text, int.Parse(txtSiso.Text), txtMalop.Text });
}

if (data > 0)
{
MessageBox.Show("Lưu thành công");
LoadData();
}
else MessageBox.Show("Lưu thất bại");

btnThem.Enabled = true;
btnXoa.Enabled = true;
btnSua.Enabled = true;
btnHuy.Enabled = false;
btnLuu.Enabled = false;

txtMalop.ReadOnly = true;
txtTenlop.ReadOnly = true;
txtSiso.ReadOnly = true;

private void LoadData()


{
dgvLop.DataSource = clsProvider.ExecuteQuery(sSelect);

//dgvLop.Rows[dgvLop.Rows.Count - 1].Selected = true;


SelectData();
}

private void SelectData()


{
if (dgvLop.RowCount < 0) return;
DataGridViewRow r = dgvLop.Rows[dgvLop.CurrentRow.Index];
txtMalop.Text = r.Cells["MALOP"].Value.ToString();
txtTenlop.Text = r.Cells["TENLOP"].Value.ToString();
txtSiso.Text = r.Cells["SISO"].Value.ToString();
}
private void btnThoat_Click(object sender, EventArgs e)
{
this.Close();
}

private void dgvLop_CellEnter(object sender, DataGridViewCellEventArgs e)


{
// SelectData();
int i = dgvLop.CurrentRow.Index;
txtMalop.Text = dgvLop.Rows[i].Cells[0].Value.ToString();
txtTenlop.Text = dgvLop.Rows[i].Cells[1].Value.ToString();
txtSiso.Text = dgvLop.Rows[i].Cells[2].Value.ToString();
}

private void dgvLop_RowEnter(object sender, DataGridViewCellEventArgs e)


{
// SelectData();
}

private void dgvLop_CellContentClick(object sender, DataGridViewCellEventArgs e)


{
int i = dgvLop.CurrentRow.Index;
txtMalop.Text = dgvLop.Rows[i].Cells[0].Value.ToString();
txtTenlop.Text = dgvLop.Rows[i].Cells[1].Value.ToString();
txtSiso.Text = dgvLop.Rows[i].Cells[2].Value.ToString();
}
}
}

You might also like