code
code
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;
using System.Globalization;
using System.IO;
using System.Net.NetworkInformation;
namespace Quanlibanquanao
SqlConnection conn;
public staff()
InitializeComponent();
createConnection();
}
private void createConnection()
try
try
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
da.Fill(data);
dgvproduct.DataSource = data;
MessageBox.Show("Successful");
conn.Close();
finally
if (conn.State == ConnectionState.Open)
conn.Close();
DisplayDataProduct();
}
try
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
string sql = "SELECT productId, productName, category, size, color, price, stockQuantity,
description, imageData FROM product WHERE productId = @productId";
cmd.CommandText = sql;
cmd.Parameters.Add("@productId", SqlDbType.Int);
cmd.Parameters["@productId"].Value = productId;
if (reader.Read())
pictureBoxProduct.Image = Image.FromStream(ms);
else
conn.Close();
MessageBox.Show("Lỗi khi lấy thông tin Product theo ProductId: " + ex.Message);
}
SearchProduct(productId);
DisplayDataProduct();
if (conn.State == ConnectionState.Closed)
conn.Open();
if (!string.IsNullOrEmpty(category))
if (minPrice.HasValue)
if (maxPrice.HasValue)
{
query.Append(" AND price <= @maxPrice");
if (!string.IsNullOrEmpty(category))
if (minPrice.HasValue)
if (maxPrice.HasValue)
try
adapter.Fill(dt);
}
catch (Exception ex)
finally
conn.Close();
return dt;
if (filteredProducts.Rows.Count == 0)
MessageBox.Show("Không tìm thấy sản phẩm nào phù hợp với tiêu chí lọc.");
dgvproduct.DataSource = null;
else
dgvproduct.DataSource = filteredProducts;
if (result == DialogResult.Yes)
Application.Exit();
else
return;
// Toàn bộ code của phần customer bao gồm (Create , Edit, Delete, Search).
try
{
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
da.Fill(data);
dgvcustomer.DataSource = data;
MessageBox.Show("Successful");
conn.Close();
finally
if (conn.State == ConnectionState.Open)
conn.Close();
}
DisplayDataCustomer();
try
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
String sql = " INSERT INTO customer (fullName, email, phoneNumber, address) VALUES
(@fullName, @email, @phoneNumber, @address)";
cmd.Parameters.Add("@fullName", SqlDbType.VarChar);
cmd.Parameters["@fullName"].Value = txtfullNamec.Text.ToString();
cmd.Parameters.Add("@email", SqlDbType.VarChar);
cmd.Parameters["@email"].Value = txtemailc.Text.ToString();
cmd.Parameters.Add("@phoneNumber", SqlDbType.VarChar);
cmd.Parameters["@phoneNumber"].Value = txtphoneNumberc.Text.ToString();
cmd.Parameters.Add("@address", SqlDbType.Text);
cmd.Parameters["@address"].Value = txtaddressc.Text;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
conn.Close();
DisplayDataCustomer();
CreateCustomer();
try
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters["@CustomerId"].Value = Convert.ToInt32(txtcustomerId.Text.ToString());
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
conn.Close();
if (re == DialogResult.Yes)
DisplayDataCustomer();
DeleteCustomer();
DisplayDataCustomer();
DeleteCustomer();
}
try
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
string sql = "UPDATE customer SET fullName = @fullName, email = @email, phoneNumber =
@phoneNumber, address = @address WHERE customerId = @customerId";
cmd.Parameters.Add("@customerId", SqlDbType.Int);
cmd.Parameters["@customerId"].Value = Convert.ToInt32(txtcustomerId.Text.ToString());
cmd.Parameters.Add("@fullName", SqlDbType.VarChar);
cmd.Parameters["@fullName"].Value = txtfullNamec.Text.ToString();
cmd.Parameters.Add("@email", SqlDbType.VarChar);
cmd.Parameters["@email"].Value = txtemailc.Text.ToString();
cmd.Parameters.Add("@phoneNumber", SqlDbType.VarChar);
cmd.Parameters["@phoneNumber"].Value = txtphoneNumberc.Text.ToString();
cmd.Parameters.Add("@address", SqlDbType.Text);
cmd.Parameters["@address"].Value = txtaddressc.Text;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
conn.Close();
}
DisplayDataCustomer();
EditCustomer();
try
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
string sql = "SELECT customerId, fullName, email, phoneNumber, address FROM customer
WHERE customerId = @customerId";
cmd.CommandText = sql;
cmd.Parameters.Add("@customerId", SqlDbType.Int);
cmd.Parameters["@customerId"].Value = customerId;
else
MessageBox.Show("Successful Search");
conn.Close();
}
}
SearchCustomer(customerId);
DisplayDataCustomer();
if (result == DialogResult.Yes)
Application.Exit();
else
return;
Application.Exit();
else
return;
try
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
da.Fill(data);
dgvorder.DataSource = data;
MessageBox.Show("Successfull");
conn.Close();
finally
if (conn.State == ConnectionState.Open)
conn.Close();
DisplayDataOrder();
try
{
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.Add("@orderDate", SqlDbType.Date);
cmd.Parameters["@orderDate"].Value = orderDate;
else
cmd.Parameters.Add("@totalAmount", SqlDbType.Decimal);
cmd.Parameters["@totalAmount"].Value = txttotalAmount.Text.ToString();
cmd.Parameters.Add("@staffId", SqlDbType.Int);
cmd.Parameters["@staffId"].Value = Convert.ToInt32(txtstaffIdo.Text.ToString());
cmd.Parameters.Add("@customerId", SqlDbType.Int);
cmd.Parameters["@customerId"].Value = Convert.ToInt32(txtcustomerIdo.Text.ToString());
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
MessageBox.Show("Successful Create");
}
catch (Exception ex)
finally
if (conn.State == System.Data.ConnectionState.Open)
conn.Close();
DisplayDataOrder();
CreateOrder();
try
conn.Open();
cmd.Parameters.Add("@orderId", SqlDbType.Int);
cmd.Parameters["@orderId"].Value = Convert.ToInt32(txtorderId.Text.ToString());
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
conn.Close();
if (re == DialogResult.Yes)
DisplayDataOrder();
DeleteOrder();
}
DisplayDataOrder();
DeleteOrder();
try
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
string sql = "UPDATE orders SET orderdate = @orderdate, customerId = @customerId, staffId
= @staffId, totalAmount = @totalAmount WHERE orderId = @orderId";
cmd.Parameters.Add("@orderId", SqlDbType.Int);
cmd.Parameters["@orderId"].Value = Convert.ToInt32(txtorderId.Text.ToString());
cmd.Parameters.Add("@orderDate", SqlDbType.Date);
cmd.Parameters["@orderDate"].Value = orderDate;
else
cmd.Parameters.Add("@totalAmount", SqlDbType.Decimal);
cmd.Parameters["@totalAmount"].Value = txttotalAmount.Text.ToString();
cmd.Parameters.Add("@staffId", SqlDbType.Int);
cmd.Parameters["@staffId"].Value = Convert.ToInt32(txtstaffIdo.Text.ToString());
cmd.Parameters.Add("@customerId", SqlDbType.Int);
cmd.Parameters["@customerId"].Value = Convert.ToInt32(txtcustomerIdo.Text.ToString());
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Erorr Edit");
DisplayDataOrder();
EditOrder();
try
{
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
string sql = "SELECT orderId, orderDate, customerId, staffId, totalAmount FROM orders
WHERE orderId = @orderId";
cmd.CommandText = sql;
cmd.Parameters.Add("@orderId", SqlDbType.Int);
cmd.Parameters["@orderId"].Value = orderId;
if (reader.Read())
else
{
MessageBox.Show("Successful Search.");
conn.Close();
SearchCustomer(orderId);
DisplayDataCustomer();
try
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
string sql = "select * from orderDetails";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
da.Fill(data);
dgvorderDetails.DataSource = data;
MessageBox.Show("Successful");
conn.Close();
finally
if (conn.State == ConnectionState.Open)
conn.Close();
DisplayDataOrderDetail();
try
conn.Open();
cmd.CommandType = System.Data.CommandType.Text;
string sql = "SELECT orderDetailsId, orderId, productId, quantity, price FROM orders WHERE
orderDetailsId = @orderDetailsId";
cmd.CommandText = sql;
cmd.Parameters.Add("@orderDetailsId", SqlDbType.Int);
cmd.Parameters["@orderDetailsId"].Value = orderDetailsId;
if (reader.Read())
else
MessageBox.Show("Successful Search");
conn.Close();
SearchCustomer(orderDetailsId);
DisplayDataCustomer();
if (result == DialogResult.Yes)
Application.Exit();
else
return;
tại sao khi search product và customer thì đúng, mà search orders và orderDetails thì lại ra thông
tin của customer là sao phải ra thông tin của orders và orderDetails chứ