0% found this document useful (0 votes)
72 views

Qlsach Qlsach Nhaxb Manxb 6 Tennxb 20 Diachi 30

The document contains code to create a database called QLSach with tables Sach and NhaXB to manage books and publishers. The Sach table stores book information including title, publisher code, and quantity. The NhaXB table stores publisher information including code, name, and address. Additional code defines classes for books, publishers and data access. User interface code allows loading, adding, editing and deleting books from the database.

Uploaded by

ChinhDat
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)
72 views

Qlsach Qlsach Nhaxb Manxb 6 Tennxb 20 Diachi 30

The document contains code to create a database called QLSach with tables Sach and NhaXB to manage books and publishers. The Sach table stores book information including title, publisher code, and quantity. The NhaXB table stores publisher information including code, name, and address. Additional code defines classes for books, publishers and data access. User interface code allows loading, adding, editing and deleting books from the database.

Uploaded by

ChinhDat
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/ 7

QLSach.

sql
create database QLSach
go
use QLSach
go
create table NhaXB
(
MaNXB varchar(6) primary key,
TenNXB nvarchar(20) not null,
DiaChi nvarchar(30) null
)
go
insert into NhaXB values('NXB001',N'NXB Kim ng',null)
insert into NhaXB values('NXB002',N'NXB Gio Dc',null)
insert into NhaXB values('NXB003',N'NXB Tr',null)
go
create table Sach
(
MaSach int identity(1,1) primary key,
TenSach nvarchar(20) not null,
MaNXB varchar(6) foreign key references NhaXB(MaNXB),
SoLuong int not null
)
go
insert into Sach values(N'Lp trnh C#','NXB002',50)
insert into Sach values(N'Doraemon','NXB001',30)
insert into Sach values(N'Lp trnh Java','NXB003',20)
go
Select MaSach,TenSach,TenNXB,SoLuong from Sach,NhaXB where Sach.MaNXB=NhaXB.MaNXB
go
Select MaNXB from NhaXB where TenNXB=N'NXB Tr'
go

App.config (Add References: System.configuration)


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="QLS" connectionString="server=9DAT;uid=sa;pwd=sa123;database=QLSach"/>
</connectionStrings>
</configuration>

Sach.cs
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace QLSach
{
class Sach
{
private int masach;
private string tensach;
private string manxb;
private int soluong;
public
public
public
public
}

int MaSach { get; set; }


string TenSach { get; set; }
string MaNXB { get; set; }
int SoLuong { get; set; }

NhaXB.cs
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace QLSach
{
class NhaXB
{
private string manxb;
private string tennxb;
private string diachi;

public string MaNXB { get; set; }


public string TenNXB { get; set; }
public string DiaChi { get; set; }

DBAccess.cs
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Data.SqlClient;
System.Configuration;
System.Windows.Forms;
System.Data;

namespace QLSach.DataAcces
{
class DBAccess
{
SqlConnection con;
SqlDataAdapter da;
SqlCommand cmd;

public DBAccess()
{
KetNoi();
}
public void KetNoi()
{
String connectStr =
ConfigurationManager.ConnectionStrings["QLS"].ConnectionString;
con = new SqlConnection();
try
{
con.ConnectionString = connectStr;
//con.Open();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message + "Li");
}
}
public DataTable getDS(string sql)
{
DataTable table = new DataTable();
da = new SqlDataAdapter(sql, con);
da.Fill(table);
return table;
}
public bool ExecuteNonQueryPara(string sql, string[] parameters, object[] value)
{
int num = 0;
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand(sql, con);
SqlParameter p;
for (int i = 0; i < parameters.Length; i++)
{
p = new SqlParameter(parameters[i], value[i]);
cmd.Parameters.Add(p);
}
num = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Li: " + ex.Message);
}
if (num > 0)
return true;
else
return false;
}
public bool kiemtraTontai(string tableName, string field, string value)
{
string query = "Select count (*) from " + tableName + " where " + field +
"='" + value + "'";
cmd = new SqlCommand(query, con);
int num = 0;
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}

num = (int)cmd.ExecuteScalar();
con.Close();

}
catch (Exception ex)
{
MessageBox.Show("Loi: " + ex.Message);
}
if (num > 0)
return true;
else
return false;
}

public string getMaNXB(string tennxb)


{
string query = "Select MaNXB from NhaXB where TenNXB=N'"+tennxb+"'";
cmd = new SqlCommand(query, con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
return (string)cmd.ExecuteScalar();
con.Close();
}

SachBO.cs
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
QLSach.DataAcces;
System.Data;

namespace QLSach.BussinessLogic
{
class SachBO
{
DBAccess db = new DBAccess();
public DataTable getDSSach()
{
string query = "Select MaSach,TenSach,TenNXB,SoLuong from Sach,NhaXB where
Sach.MaNXB=NhaXB.MaNXB";
return db.getDS(query);
}
public bool kiemtraSach(string ms)
{
return db.kiemtraTontai("Sach", "MaSach", ms);
}
public bool ThemSach(Sach s)
{
string[] param={"@TenSach","@MaNXB","@SoLuong"};
object[] values={s.TenSach,s.MaNXB,s.SoLuong};
string query="Insert into Sach values(@TenSach,@MaNXB,@Soluong)";
return db.ExecuteNonQueryPara(query, param, values);
}
public bool SuaSach(Sach s)
{
string[] param = { "@MaSach", "@TenSach", "@MaNXB", "@SoLuong" };
object[] values = { s.MaSach, s.TenSach, s.MaNXB, s.SoLuong };

string query = "Update Sach set TenSach=@TenSach, MaNXB=@MaNXB,


SoLuong=@Soluong where MaSach=@MaSach";
return db.ExecuteNonQueryPara(query, param, values);
}

public bool XoaSach(Sach s)


{
string[] param = { "@MaSach" };
object[] values = { s.MaSach };
string query = "Delete from Sach where MaSach=@MaSach";
return db.ExecuteNonQueryPara(query, param, values);
}

NhaXBBO.cs
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
QLSach.DataAcces;
System.Data;

namespace QLSach.BussinessLogic
{
class NhaXBBO
{
DBAccess db = new DBAccess();
public DataTable getDSNXB()
{
return db.getDS("Select * from NhaXB");
}
}
}

Main.cs
using
using
using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Configuration;
System.Data.SqlClient;
System.Collections;
QLSach.BussinessLogic;
QLSach.DataAcces;

namespace QLSach
{
public partial class frmMain : Form
{
SachBO sBO = new SachBO();
NhaXBBO nxbBO = new NhaXBBO();
DBAccess db = new DBAccess();
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)

{
EnableEdit(true);
DataTable tableNXB = new DataTable();
tableNXB = nxbBO.getDSNXB();
cbxNXB.DataSource = tableNXB;
cbxNXB.DisplayMember = "TenNXB";
cbxNXB.ValueMember = "MaNXB";
loadData();
bindData();
}
private void clearBind()
{
txtMS.DataBindings.Clear();
txtTS.DataBindings.Clear();
txtSL.DataBindings.Clear();
cbxNXB.DataBindings.Clear();
dgv.DataBindings.Clear();
}
private void loadData()
{
DataTable tableSach = new DataTable();
tableSach = sBO.getDSSach();
dgv.DataSource = tableSach;
}
private void bindData()
{
BindingSource bindSourceSach = new BindingSource();
bindSourceSach.DataSource = sBO.getDSSach();
clearBind();
txtMS.DataBindings.Add("Text", bindSourceSach, "MaSach");
txtSL.DataBindings.Add("Text", bindSourceSach, "SoLuong");
txtTS.DataBindings.Add("Text", bindSourceSach, "TenSach");
cbxNXB.DataBindings.Add("Text", bindSourceSach, "TenNXB");
}
private void resetData()
{
txtMS.Text = "";
txtTS.Text = "";
txtSL.Text = "";
}
private void EnableEdit(bool ok)
{
btnThem.Enabled = ok;
btnSua.Enabled = ok;
btnXoa.Enabled = ok;
btnLuu.Enabled = !ok;
btnHuy.Enabled = !ok;
dgv.Enabled = ok;
}

private Sach getDataSach()


{
Sach s = new Sach();
s.MaSach = Convert.ToInt32(txtMS.Text);
s.TenSach = txtTS.Text;
s.SoLuong = Convert.ToInt32(txtSL.Text);
s.MaNXB = db.getMaNXB(cbxNXB.Text);
return s;
}

private void btnThem_Click(object sender, EventArgs e)


{
EnableEdit(false);
//resetData();
loadData();
}
private void btnSua_Click(object sender, EventArgs e)
{
EnableEdit(false);
loadData();
}
private void btnLuu_Click(object sender, EventArgs e)
{
Sach s =getDataSach();
if (sBO.kiemtraSach(txtMS.Text))
{
if (sBO.SuaSach(s))
MessageBox.Show("Sua Thanh Cong!");
else
MessageBox.Show("Loi!");
}
else
{
if (sBO.ThemSach(s))
MessageBox.Show("Them Thanh Cong!");
else
MessageBox.Show("Loi!");
}
EnableEdit(true);
loadData();
bindData();
}
private void btnHuy_Click(object sender, EventArgs e)
{
EnableEdit(true);
}
private void btnXoa_Click(object sender, EventArgs e)
{
Sach s = getDataSach();
if (MessageBox.Show("Ban co muon xoa cuon sach: " + txtTS.Text + " nay ko?",
"Xac nhan", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
if (sBO.XoaSach(s))
MessageBox.Show("Xoa Thanh Cong!");
else
MessageBox.Show("Loi!");

}
loadData();
bindData();

private void dgv_RowEnter(object sender, DataGridViewCellEventArgs e)


{
DataGridViewRow row = new DataGridViewRow();
row = dgv.Rows[e.RowIndex];
txtMS.Text = row.Cells[0].Value.ToString();
txtTS.Text = row.Cells[1].Value.ToString();
cbxNXB.Text = row.Cells[2].Value.ToString();
txtSL.Text = row.Cells[3].Value.ToString();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy