0% found this document useful (0 votes)
45 views17 pages

21-Tô Thành Hậu - 22110319

The document contains source code for a C# program that allows a user to input, view, sort, and search phone store inventory information stored in a SQL database. The program includes functions for adding new phone records, displaying all records, sorting records by year or price, and searching records by selected fields. The code connects to a SQL database, executes queries to insert, select, and order phone store records, and displays results to the user.

Uploaded by

tthau2004bd77
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)
45 views17 pages

21-Tô Thành Hậu - 22110319

The document contains source code for a C# program that allows a user to input, view, sort, and search phone store inventory information stored in a SQL database. The program includes functions for adding new phone records, displaying all records, sorting records by year or price, and searching records by selected fields. The code connects to a SQL database, executes queries to insert, select, and order phone store records, and displays results to the user.

Uploaded by

tthau2004bd77
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/ 17

Kiểm tra Cột 3

28/11/2023
Income

Outcome
Lợi nhuận

Source Code
cuaHang.cs using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BaiSo3
{

public partial class cuaHang1 : Form


{
string strCon = @"Data Source=DESKTOP-S33O38P;Initial Catalog=BaiSo3;Integrated Security=True;Encr
SqlConnection conn = null;
int n;
int i = 0;
string id;

public cuaHang1(string id)


{
InitializeComponent();
this.id = id;
}

private void button_in_Click(object sender, EventArgs e)


{
button_nhap.Enabled = true;
panel_input.Enabled = true;
n = (Int32)numericUpDown1.Value;
//n = Int32.Parse(textBox_soluong.Text);
status_intput.Text += 1;
label_conlai.Text = "Số máy còn lại: " + (n - 1);
}

private void button_nhap_Click(object sender, EventArgs e)


{
{

PhoneStore temp = new PhoneStore("","","",0, "","","", 0,0);


if (i == n - 1)
{
button_nhap.Enabled = false;
panel_input.Enabled = false;
}
else
status_intput.Text = "Nhập thông tin của máy số: " + (i + 2);
//status_intput.Visible = false;
temp.Name = textBox_name.Text;
temp.Serie =textBox_serie.Text;
temp.Year = Int32.Parse(textBox_year.Text);
temp.Producer = textBox_hang.Text;
temp.Color = textBox_color.Text;
temp.Price = Int32.Parse(textBox_price.Text);
temp.Address = textBox_address.Text;
temp.ContactNumber = Int32.Parse(textBox_number.Text);
i++;
if (conn == null)
{
conn = new SqlConnection(strCon);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into StorePhone values('"+id.ToString()
+"',N'"+temp.Name+"',N'"+temp.Producer+"',N'"+temp.Color+"',N'"+temp.Address+"','"+temp.Year+"','"+te

cmd.Connection = conn;

if ((n - i - 1) == -1)
{
label_conlai.Text = "Số máy còn lại: 0";
}
else
{
label_conlai.Text = "Số máy còn lại: " + (n - i - 1);
}
textBox_color.Text = "";
textBox_name.Text = "";
textBox_serie.Text = "";
textBox_year.Text = "";
textBox_hang.Text = "";
textBox_price.Text = "";
textBox_address.Text = "";
textBox_number.Text = "";
}
}

private void button_show_Click(object sender, EventArgs e)


{

string OutPut = "";


if (conn == null)
{
conn = new SqlConnection(strCon);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from StorePhone where id='"+id+"'";

cmd.Connection = conn;

SqlDataReader reader = cmd.ExecuteReader();


while (reader.Read())
{

string id = reader.GetString(0).Trim();
string ten = reader.GetString(1).Trim();
string hang=reader.GetString(2).Trim();
string mau=reader.GetString(3).Trim();
string diaChi = reader.GetString(4).Trim();
string nam = reader.GetInt32(5).ToString().Trim();
string serie = reader.GetString(6).Trim();
string gia = reader.GetInt64(7).ToString().Trim();
string soDT = reader.GetInt64(8).ToString().Trim();
OutPut+=id+" "+ten+" "+hang+" "+" "+mau+" "+diaChi+" "+" "+nam + " " + serie + " " + gia + " " + so
}
reader.Close();
MessageBox.Show(OutPut, "Thông tin điện thoại của cửa hàng");
}

private void button_sapxepNam_Click(object sender, EventArgs e)


{
List<PhoneStore> phoneStores = new List<PhoneStore>();

if (conn == null)
{
conn = new SqlConnection(strCon);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from StorePhone where id='" + id + "'";

cmd.Connection = conn;

SqlDataReader reader = cmd.ExecuteReader();


while (reader.Read())
{

string id = reader.GetString(0).Trim();
string ten = reader.GetString(1).Trim();
string hang=reader.GetString(2).Trim();
string mau=reader.GetString(3).Trim();
string diaChi = reader.GetString(4).Trim();
string nam = reader.GetInt32(5).ToString().Trim();
string serie = reader.GetString(6).Trim();
string gia = reader.GetInt64(7).ToString().Trim();
string soDT = reader.GetInt64(8).ToString().Trim();
PhoneStore phone = new PhoneStore(id, ten, hang, Convert.ToInt16(nam), mau, serie, diaChi, Conv
phoneStores.Add(phone);
}
reader.Close();
phoneStores.Sort(
(p1, p2) =>
{
if (p1.Year == p2.Year) return 0;
if (p1.Year < p2.Year) return 1;
return -1;
});
string Output = "";
foreach (PhoneStore item in phoneStores)
{
Output += item.ID+" "+item.Name+" "+item.Producer+" "+item.Color+" "+item.Address+" "+item.Ye
"+item.Price.ToString()+" "+item.ContactNumber.ToString()+"\n---------------------\n";
}
MessageBox.Show(Output, "Thông tin điện thoại của cửa hàng");
}

private void button_sapxepGia_Click(object sender, EventArgs e)


{
List<PhoneStore> phoneStores = new List<PhoneStore>();

if (conn == null)
{
conn = new SqlConnection(strCon);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from StorePhone where id='" + id + "'";

cmd.Connection = conn;

SqlDataReader reader = cmd.ExecuteReader();


while (reader.Read())
{

string id = reader.GetString(0).Trim();
string ten = reader.GetString(1).Trim();
string hang = reader.GetString(2).Trim();
string mau = reader.GetString(3).Trim();
string diaChi = reader.GetString(4).Trim();
string nam = reader.GetInt32(5).ToString().Trim();
string serie = reader.GetString(6).Trim();
string gia = reader.GetInt64(7).ToString().Trim();
string soDT = reader.GetInt64(8).ToString().Trim();
PhoneStore phone = new PhoneStore(id, ten, hang, Convert.ToInt16(nam), mau, serie, diaChi, Conv
phoneStores.Add(phone);
}
reader.Close();
phoneStores.Sort((p1, p2) =>
{
if (p1.Price == p2.Price) return 0;
if (p1.Price < p2.Price) return 1;
return -1;
});
string Output = "";
foreach (PhoneStore item in phoneStores)
{
Output += item.ID + " " + item.Name + " " + item.Producer + " " + item.Color + " " + item.Address +
+ " " + item.Price.ToString() + " " + item.ContactNumber.ToString() + "\n---------------------\n";
}
MessageBox.Show(Output, "Thông tin điện thoại của cửa hàng");
phoneStores.Sort((p1, p2) =>
{
if (p1.Price == p2.Price) return 0;
if (p1.Price < p2.Price) return 1;
return -1;
});
}

private void button1_Click(object sender, EventArgs e)


{

if (conn == null)
{
conn = new SqlConnection(strCon);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from StorePhone where "+comboBox_find.Text+"='" + textBox_find.Tex

cmd.Connection = conn;

SqlDataReader reader = cmd.ExecuteReader();


string Output = "";
while (reader.Read())
{

string id = reader.GetString(0).Trim();
string ten = reader.GetString(1).Trim();
string hang = reader.GetString(2).Trim();
string mau = reader.GetString(3).Trim();
string diaChi = reader.GetString(4).Trim();
string nam = reader.GetInt16(5).ToString().Trim();
string serie = reader.GetString(6).Trim();
string gia = reader.GetInt64(7).ToString().Trim();
string soDT = reader.GetInt64(8).ToString().Trim();
Output += id + " " + ten + " " + hang + " " + " " + mau + " " + diaChi + " " + " " + nam + " " + serie + " "
}
reader.Close();
MessageBox.Show(Output, "Thông tin điện thoại của cửa hàng");

private void panel_search_Paint(object sender, PaintEventArgs e)


{

private void cuaHang1_Load(object sender, EventArgs e)


{

private void textBox_find_TextChanged(object sender, EventArgs e)


{

}
}
}

Form1.cs using System;


using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BaiSo3
{

public partial class Form1 : Form


{
string strCon = @"Data Source=DESKTOP-S33O38P;Initial Catalog=BaiSo3;Integrated Security=True;Encrypt=False";
SqlConnection conn = null;
public Form1()
{
InitializeComponent();
}

private void cácCửaHàngToolStripMenuItem_Click(object sender, EventArgs e)


{

private void nhânViênToolStripMenuItem_Click(object sender, EventArgs e)


{
NhanVien nv = new NhanVien();
nv.Show();
}

private void cửaHàng1ToolStripMenuItem_Click(object sender, EventArgs e)


{
cuaHang1 c1 = new cuaHang1("1");
c1.Show();
}

private void cửaHàng2ToolStripMenuItem_Click(object sender, EventArgs e)


{
cuaHang1 c2 = new cuaHang1("2");
c2.Show();
}

private void inComeToolStripMenuItem_Click(object sender, EventArgs e)


{
if (conn == null)
{
conn = new SqlConnection(strCon);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from StorePhone where id='1'";

cmd.Connection = conn;

SqlDataReader reader = cmd.ExecuteReader();

string path = @"D:\dulieu\Income.txt";


TextWriter wrt = new StreamWriter(path);
wrt.WriteLine("Cua hang 1");
wrt.WriteLine("nam \t gia \t");

while (reader.Read())
{
string nam = reader.GetInt32(5).ToString().Trim();
string price = reader.GetInt64(7).ToString().Trim();
wrt.Write("\t" + nam + "\t" + price);
wrt.WriteLine("");
wrt.WriteLine("==================================================================");
}
reader.Close();

if (conn == null)
{
conn = new SqlConnection(strCon);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd2 = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from StorePhone where id='2'";

cmd.Connection = conn;

SqlDataReader reader2 = cmd.ExecuteReader();

wrt.WriteLine("Cua hang 2");


wrt.WriteLine("Nam \t gia \t");

while (reader2.Read())
{
string nam = reader2.GetInt32(5).ToString().Trim();
string price = (reader2.GetInt32(5)).ToString().Trim();
wrt.Write("\t" + nam + "\t" + price);
wrt.WriteLine("");
wrt.WriteLine("==================================================================");
}

reader2.Close();

wrt.Close();
}
private void outComeToolStripMenuItem_Click(object sender, EventArgs e)
{

if (conn == null)
{
conn = new SqlConnection(strCon);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from qlNVien";

cmd.Connection = conn;

SqlDataReader reader = cmd.ExecuteReader();

string path = @"D:\dulieu\OutCome.txt";


TextWriter wrt = new StreamWriter(path);
wrt.WriteLine("ID \t Tên \t Luong");

while (reader.Read())
{
string id = reader.GetString(0).Trim();
string ten = reader.GetString(1).Trim();
string luong = (reader.GetInt64(3)*12).ToString().Trim();
wrt.WriteLine(id + " " + ten + " " + luong);
wrt.WriteLine("");
wrt.WriteLine("==================================================================");
}
wrt.Close();
reader.Close();
}

private void lợiNhuậnToolStripMenuItem_Click(object sender, EventArgs e)


{
Int64 luongNV = 0;
Int64 DoanhThu = 0;
if (conn == null)
{
conn = new SqlConnection(strCon);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from qlNVien";

cmd.Connection = conn;

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{

Int64 luong = reader.GetInt64(3);


luongNV += luong;
}

reader.Close();

cmd.CommandText = "select * from StorePhone";

cmd.Connection = conn;

SqlDataReader reader2 = cmd.ExecuteReader();


while (reader2.Read())
{
Int64 gia=reader2.GetInt64(7);
DoanhThu += gia;
}

TextWriter wrt = new StreamWriter(@"D:\dulieu\DoanhThu.txt");


wrt.WriteLine("Vậy tổng lợi nhuận là "+((DoanhThu - luongNV)*12).ToString());
wrt.Close();
}

private void Form1_Load(object sender, EventArgs e)


{

}
}
}

Nhanvien.cs 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;

namespace BaiSo3
{
public partial class NhanVien : Form
{
qlNVien nv = new qlNVien();
nvTableDataContext db = new nvTableDataContext("Data Source=DESKTOP-S33O38P;Initial Catalog=BaiSo3;Integra
public NhanVien()
{
InitializeComponent();
}

private void NhanVien_Load(object sender, EventArgs e)


{
var list = (from s in db.qlNViens select s).ToList();
dataGridView1.DataSource = list;

txt_id.DataBindings.Clear();
txt_ten.DataBindings.Clear();
txt_phong.DataBindings.Clear();
txt_luong.DataBindings.Clear();
txt_tuoi.DataBindings.Clear();
// TODO: This line of code loads data into the 'baiSo3DataSet2.qlNVien' table. You can move, or remove it, as needed
this.qlNVienTableAdapter.Fill(this.baiSo3DataSet2.qlNVien);

private void btn_save_Click(object sender, EventArgs e)


{
nv = new qlNVien();
nv.id = txt_id.Text;
nv.ten = txt_ten.Text;
nv.phong = txt_phong.Text;
nv.luong = Convert.ToInt64(txt_luong.Text);
nv.tuoi = Convert.ToInt16(txt_tuoi.Text);

db.qlNViens.InsertOnSubmit(nv);
db.SubmitChanges();
NhanVien_Load(sender, e);
}

private void btn_edit_Click(object sender, EventArgs e)


{
nv = db.qlNViens.Where(s => s.id == txt_id.Text).Single();
nv.ten = txt_ten.Text;
nv.phong = txt_phong.Text;
nv.luong = Convert.ToInt64(txt_luong.Text);
nv.tuoi = Convert.ToInt16(txt_tuoi.Text);
db.SubmitChanges();
NhanVien_Load(sender, e);
}

private void button1_Click(object sender, EventArgs e)


{

nv = db.qlNViens.Where(s => s.id == txt_id.Text).Single();


nv.ten = txt_ten.Text;
nv.tuoi = Convert.ToInt16(txt_tuoi.Text);
nv.phong = txt_phong.Text;
nv.luong = Convert.ToInt64(txt_luong.Text);
db.qlNViens.DeleteOnSubmit(nv);
db.SubmitChanges();
NhanVien_Load(sender, e);
}

private void button2_Click(object sender, EventArgs e)


{
NhanVienForm2 nv2 = new NhanVienForm2();
nv2.Show();
}
}
}

NhanvienForm2. using System;


cs using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BaiSo3
{
public partial class NhanVienForm2 : Form
{
qlNVien nv = new qlNVien();
nvTableDataContext db = new nvTableDataContext("Data Source=DESKTOP-S33O38P;Initial Catalog=BaiSo3;Integra
public NhanVienForm2()
{
InitializeComponent();
}

private void NhanVienForm2_Load(object sender, EventArgs e)


{
var list = (from s in db.qlNViens select s).ToList();
dataGridView1.DataSource = list;
// TODO: This line of code loads data into the 'baiSo3DataSet1.qlNVien' table. You can move, or remove it, as needed
this.qlNVienTableAdapter.Fill(this.baiSo3DataSet1.qlNVien);

private void button2_Click(object sender, EventArgs e)


{
string res = "";
if (rbIT.Checked)
{
res = "IT";
}
else if(rbHR.Checked)
{
res = "HR";
}else if (rbBA.Checked)
{
res = "BA";
}
var list = (from s in db.qlNViens where s.phong.Trim().Contains(res) select s).ToList();
dataGridView1.DataSource = list;
}

private void button1_Click(object sender, EventArgs e)


{
var list = (from s in db.qlNViens where s.luong>Convert.ToInt64(txt_search.Text.Trim()) select s).ToList();
dataGridView1.DataSource = list;
}

private void button3_Click(object sender, EventArgs e)


{
dataGridView1.AllowUserToAddRows = false;
string path = @"D:\dulieu\Exercise.txt";
TextWriter wrt = new StreamWriter(path);

wrt.WriteLine("ID \t Tên \t Phong \t Luong \t Tuoi ");


for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for(int j = 0; j < dataGridView1.Columns.Count; j++)
{

wrt.Write(dataGridView1.Rows[i].Cells[j].Value.ToString()+"\t");

}
wrt.WriteLine("");
wrt.WriteLine("==================================================================");
}
wrt.Close();
}
}
}

You might also like