By GokiSoft.com| 09:41 01/11/2021|
C Sharp

[Assignment] Bài tập ôn luyên tổng quát C# - Lập Trình C# - Lập Trình C Sharp

Đề 1: Tạo ứng dụng calculator bằng giao diện (Winform Application) gồm các chức năng sau

- Tạo máy tính gồm các chức năng cơ bản

- Lưu lại toàn bộ lịch sử đã thực hiện các phép tính vào file hoặc CSDL

Đề 2 : Tạo ứng dụng quản lý khách sạn (Dùng console hoặc Winform -> tuỳ chọn)

Note : Form sẽ đc nhiều điểm hơn

Gồm các chức năng

- Thêm/Sửa/Xoá KS

- Thêm sửa xoá khách hàng

- Quản lý đặt phòng

- Tìm kiếm.


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Luong Dinh Dai [T1907A]
Luong Dinh Dai

2020-06-03 02:23:33


#Form1.cs


using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace WindowsForms
{
    public partial class Form1 : Form
    {
        Double resultValue = 0;
        String operationPerformed = "";
        bool ischeck = false;
        List<string> list = new List<string>();
        List<string> listAll = new List<string>();
        string data;
        string nameFile = "demo.txt";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button_click(object sender, EventArgs e)
        {
            if ((tbShownumber.Text == "0") || ischeck)
            {
                tbShownumber.Clear();
            }
            ischeck = false;
            if (!tbShownumber.Text.Contains("."))
            {
                Button button = (Button)sender;
                list.Add(button.Text);
                tbShownumber.Text = tbShownumber.Text + button.Text;

            }
            else
            {
                Button button = (Button)sender;
                if (!button.Text.Equals("."))
                    tbShownumber.Text = tbShownumber.Text + button.Text;
                list.Add(button.Text);
            }



        }

        private void operator_click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            if (resultValue != 0)
            {
                button16.PerformClick();
                operationPerformed = button.Text;
                list.Add(button.Text);
                resultValue = Convert.ToDouble(tbShownumber.Text);

                ischeck = true;
            }

            operationPerformed = button.Text;
            list.Add(button.Text);
            resultValue = Convert.ToDouble(tbShownumber.Text);

            show_calculator.Text = resultValue + " " + operationPerformed;
            ischeck = true;


        }

        private void button_click5(object sender, EventArgs e)
        {
            tbShownumber.Text = "0";
            resultValue = 0;
            list.Clear();
            data = "";
            show_calculator.Text = "";
        }

        private void button_click4(object sender, EventArgs e)
        {
            tbShownumber.Text = "0";
        }

        private void button_result(object sender, EventArgs e)
        {
            show_calculator.Text = show_calculator.Text + " " + tbShownumber.Text;
            switch (operationPerformed)
            {

                case "+":
                    tbShownumber.Text = (resultValue + Convert.ToDouble(tbShownumber.Text)).ToString();

                    break;
                case "-":
                    tbShownumber.Text = (resultValue - Convert.ToDouble(tbShownumber.Text)).ToString();
                    break;
                case "*":
                    tbShownumber.Text = (resultValue * Convert.ToDouble(tbShownumber.Text)).ToString();
                    break;
                case "/":
                    tbShownumber.Text = (resultValue / Convert.ToDouble(tbShownumber.Text)).ToString();
                    break;
                default:
                    break;

            }

            list.Add("=" + tbShownumber.Text);
            foreach (var item in list)
            {
                data += item;
            }
            
            show_calculator.Text = data;
            StreamWriter writer = new StreamWriter(nameFile, true);
            writer.WriteLine(data);
            writer.Close();
            insertMysql(data);
        }
        private void insertMysql(string data)
        {
            string strConnection = "Server = localhost;Database=test;Port=3306;User ID=root;password=";
            MySqlConnection connection = new MySqlConnection(strConnection); ;
            try
            {
                
                connection.Open();

                string data_insert = "INSERT INTO calculator(result) VALUES ('"+data+"')";
                MySqlCommand command = new MySqlCommand(data_insert,connection);
                command.ExecuteNonQuery();
                Console.WriteLine("hoan thanh success");
            }
            catch (Exception ex)
            {

                Console.WriteLine("ket noi error "+ex.Message);
            }
            finally
            {
                connection.Close();
                connection.Dispose();
            }
        }
       
    }


}




#Form1.Designer.cs


namespace WindowsForms
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.button7 = new System.Windows.Forms.Button();
            this.button8 = new System.Windows.Forms.Button();
            this.button9 = new System.Windows.Forms.Button();
            this.button10 = new System.Windows.Forms.Button();
            this.button12 = new System.Windows.Forms.Button();
            this.button13 = new System.Windows.Forms.Button();
            this.button14 = new System.Windows.Forms.Button();
            this.button16 = new System.Windows.Forms.Button();
            this.button17 = new System.Windows.Forms.Button();
            this.button18 = new System.Windows.Forms.Button();
            this.button20 = new System.Windows.Forms.Button();
            this.button15 = new System.Windows.Forms.Button();
            this.tbShownumber = new System.Windows.Forms.TextBox();
            this.show_calculator = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button1.Location = new System.Drawing.Point(27, 88);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(54, 39);
            this.button1.TabIndex = 0;
            this.button1.Text = "7";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button_click);
            // 
            // button2
            // 
            this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button2.Location = new System.Drawing.Point(87, 88);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(54, 39);
            this.button2.TabIndex = 1;
            this.button2.Text = "8";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button_click);
            // 
            // button3
            // 
            this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button3.Location = new System.Drawing.Point(147, 88);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(54, 39);
            this.button3.TabIndex = 2;
            this.button3.Text = "9";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button_click);
            // 
            // button4
            // 
            this.button4.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button4.Location = new System.Drawing.Point(207, 88);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(54, 39);
            this.button4.TabIndex = 3;
            this.button4.Text = "/";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.operator_click);
            // 
            // button5
            // 
            this.button5.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button5.Location = new System.Drawing.Point(267, 88);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(54, 39);
            this.button5.TabIndex = 4;
            this.button5.Text = "CE";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button_click4);
            // 
            // button6
            // 
            this.button6.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button6.Location = new System.Drawing.Point(267, 133);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(54, 39);
            this.button6.TabIndex = 9;
            this.button6.Text = "C";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button_click5);
            // 
            // button7
            // 
            this.button7.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button7.Location = new System.Drawing.Point(207, 133);
            this.button7.Name = "button7";
            this.button7.Size = new System.Drawing.Size(54, 39);
            this.button7.TabIndex = 8;
            this.button7.Text = "*";
            this.button7.UseVisualStyleBackColor = true;
            this.button7.Click += new System.EventHandler(this.operator_click);
            // 
            // button8
            // 
            this.button8.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button8.Location = new System.Drawing.Point(147, 133);
            this.button8.Name = "button8";
            this.button8.Size = new System.Drawing.Size(54, 39);
            this.button8.TabIndex = 7;
            this.button8.Text = "6";
            this.button8.UseVisualStyleBackColor = true;
            this.button8.Click += new System.EventHandler(this.button_click);
            // 
            // button9
            // 
            this.button9.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button9.Location = new System.Drawing.Point(87, 133);
            this.button9.Name = "button9";
            this.button9.Size = new System.Drawing.Size(54, 39);
            this.button9.TabIndex = 6;
            this.button9.Text = "5";
            this.button9.UseVisualStyleBackColor = true;
            this.button9.Click += new System.EventHandler(this.button_click);
            // 
            // button10
            // 
            this.button10.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button10.Location = new System.Drawing.Point(27, 133);
            this.button10.Name = "button10";
            this.button10.Size = new System.Drawing.Size(54, 39);
            this.button10.TabIndex = 5;
            this.button10.Text = "4";
            this.button10.UseVisualStyleBackColor = true;
            this.button10.Click += new System.EventHandler(this.button_click);
            // 
            // button12
            // 
            this.button12.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button12.Location = new System.Drawing.Point(207, 178);
            this.button12.Name = "button12";
            this.button12.Size = new System.Drawing.Size(54, 39);
            this.button12.TabIndex = 13;
            this.button12.Text = "-";
            this.button12.UseVisualStyleBackColor = true;
            this.button12.Click += new System.EventHandler(this.operator_click);
            // 
            // button13
            // 
            this.button13.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button13.Location = new System.Drawing.Point(147, 178);
            this.button13.Name = "button13";
            this.button13.Size = new System.Drawing.Size(54, 39);
            this.button13.TabIndex = 12;
            this.button13.Text = "3";
            this.button13.UseVisualStyleBackColor = true;
            this.button13.Click += new System.EventHandler(this.button_click);
            // 
            // button14
            // 
            this.button14.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button14.Location = new System.Drawing.Point(87, 178);
            this.button14.Name = "button14";
            this.button14.Size = new System.Drawing.Size(54, 39);
            this.button14.TabIndex = 11;
            this.button14.Text = "2";
            this.button14.UseVisualStyleBackColor = true;
            this.button14.Click += new System.EventHandler(this.button_click);
            // 
            // button16
            // 
            this.button16.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button16.Location = new System.Drawing.Point(267, 178);
            this.button16.Name = "button16";
            this.button16.Size = new System.Drawing.Size(54, 84);
            this.button16.TabIndex = 19;
            this.button16.Text = "=";
            this.button16.UseVisualStyleBackColor = true;
            this.button16.Click += new System.EventHandler(this.button_result);
            // 
            // button17
            // 
            this.button17.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button17.Location = new System.Drawing.Point(207, 223);
            this.button17.Name = "button17";
            this.button17.Size = new System.Drawing.Size(54, 39);
            this.button17.TabIndex = 18;
            this.button17.Text = "+";
            this.button17.UseVisualStyleBackColor = true;
            this.button17.Click += new System.EventHandler(this.operator_click);
            // 
            // button18
            // 
            this.button18.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button18.Location = new System.Drawing.Point(147, 223);
            this.button18.Name = "button18";
            this.button18.Size = new System.Drawing.Size(54, 39);
            this.button18.TabIndex = 17;
            this.button18.Text = ".";
            this.button18.UseVisualStyleBackColor = true;
            this.button18.Click += new System.EventHandler(this.button_click);
            // 
            // button20
            // 
            this.button20.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button20.Location = new System.Drawing.Point(27, 223);
            this.button20.Name = "button20";
            this.button20.Size = new System.Drawing.Size(114, 39);
            this.button20.TabIndex = 15;
            this.button20.Text = "0";
            this.button20.UseVisualStyleBackColor = true;
            this.button20.Click += new System.EventHandler(this.button_click);
            // 
            // button15
            // 
            this.button15.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button15.Location = new System.Drawing.Point(27, 178);
            this.button15.Name = "button15";
            this.button15.Size = new System.Drawing.Size(54, 39);
            this.button15.TabIndex = 10;
            this.button15.Text = "1";
            this.button15.UseVisualStyleBackColor = true;
            this.button15.Click += new System.EventHandler(this.button_click);
            // 
            // tbShownumber
            // 
            this.tbShownumber.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbShownumber.Location = new System.Drawing.Point(27, 44);
            this.tbShownumber.Multiline = true;
            this.tbShownumber.Name = "tbShownumber";
            this.tbShownumber.Size = new System.Drawing.Size(294, 30);
            this.tbShownumber.TabIndex = 20;
            this.tbShownumber.Text = "0";
            this.tbShownumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // show_calculator
            // 
            this.show_calculator.AccessibleName = "";
            this.show_calculator.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.show_calculator.ForeColor = System.Drawing.SystemColors.ControlDark;
            this.show_calculator.Location = new System.Drawing.Point(24, 9);
            this.show_calculator.Name = "show_calculator";
            this.show_calculator.Size = new System.Drawing.Size(196, 30);
            this.show_calculator.TabIndex = 21;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(354, 283);
            this.Controls.Add(this.show_calculator);
            this.Controls.Add(this.tbShownumber);
            this.Controls.Add(this.button16);
            this.Controls.Add(this.button17);
            this.Controls.Add(this.button18);
            this.Controls.Add(this.button20);
            this.Controls.Add(this.button12);
            this.Controls.Add(this.button13);
            this.Controls.Add(this.button14);
            this.Controls.Add(this.button15);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button7);
            this.Controls.Add(this.button8);
            this.Controls.Add(this.button9);
            this.Controls.Add(this.button10);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.Button button7;
        private System.Windows.Forms.Button button8;
        private System.Windows.Forms.Button button9;
        private System.Windows.Forms.Button button10;
        private System.Windows.Forms.Button button12;
        private System.Windows.Forms.Button button13;
        private System.Windows.Forms.Button button14;
        private System.Windows.Forms.Button button16;
        private System.Windows.Forms.Button button17;
        private System.Windows.Forms.Button button18;
        private System.Windows.Forms.Button button20;
        private System.Windows.Forms.Button button15;
        private System.Windows.Forms.TextBox tbShownumber;
        private System.Windows.Forms.Label show_calculator;
    }
}



#Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}



Phí Văn Long [T1907A]
Phí Văn Long

2020-06-02 16:46:33



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;
using System.ComponentModel.Design;
using System.Configuration;

namespace QLKSan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();        
        }
        SqlConnection con;
        private void btnReset_Click(object sender, EventArgs e)
        {
            txtIdRoom.Text = "";
            txtNameRoom.Text = "";
            txtPrice.Text = "";
            txtNameUser.Text = "";
            cbxLoaiPhong.Text = "";
            cbxTinhTrang.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string conString = ConfigurationManager.ConnectionStrings["QLKS"].ConnectionString.ToString();
            con = new SqlConnection(conString);
            con.Open();
            Display();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            con.Close();
        }
        public void Display()
        {
            string sqlSELECT = "SELECT * FROM KSan";
            SqlCommand cmd = new SqlCommand(sqlSELECT, con);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            tblRoom.DataSource = dt;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string SqlINSERT = "INSERT INTO KSan VALUES (@MaPhong, @TenPhong,@Gia,@TenNguoiDat,@LoaiPhong,@TinhTrang)";
            SqlCommand cmd = new SqlCommand(SqlINSERT,con);
            cmd.Parameters.AddWithValue("MaPhong", txtIdRoom.Text);
            cmd.Parameters.AddWithValue("TenPhong", txtNameRoom.Text);
            cmd.Parameters.AddWithValue("Gia", txtPrice.Text);
            cmd.Parameters.AddWithValue("TenNguoiDat", txtNameUser.Text);
            cmd.Parameters.AddWithValue("LoaiPhong", cbxLoaiPhong.Text);
            cmd.Parameters.AddWithValue("TinhTrang", cbxTinhTrang.Text);
            cmd.ExecuteNonQuery();
            Display();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string SqlUpdate = "UPDATE KSan SET TenPhong = @TenPhong , Gia = @Gia, TenNguoiDat = @TenNguoiDat,LoaiPhong = @LoaiPhong,TinhTrang = @TinhTrang WHERE MaPhong = @MaPhong ";
            SqlCommand cmd = new SqlCommand(SqlUpdate, con);
            cmd.Parameters.AddWithValue("MaPhong", txtIdRoom.Text);
            cmd.Parameters.AddWithValue("TenPhong", txtNameRoom.Text);
            cmd.Parameters.AddWithValue("Gia", txtPrice.Text);
            cmd.Parameters.AddWithValue("TenNguoiDat", txtNameUser.Text);
            cmd.Parameters.AddWithValue("LoaiPhong", cbxLoaiPhong.Text);
            cmd.Parameters.AddWithValue("TinhTrang", cbxTinhTrang.Text);
            cmd.ExecuteNonQuery();
            Display();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string SqlDelete = "DELETE FROM KSan WHERE MaPhong = @MaPhong";
            SqlCommand cmd = new SqlCommand(SqlDelete, con);
            cmd.Parameters.AddWithValue("MaPhong", txtIdRoom.Text);
            cmd.Parameters.AddWithValue("TenPhong", txtNameRoom.Text);
            cmd.Parameters.AddWithValue("Gia", txtPrice.Text);
            cmd.Parameters.AddWithValue("TenNguoiDat", txtNameUser.Text);
            cmd.Parameters.AddWithValue("LoaiPhong", cbxLoaiPhong.Text);
            cmd.Parameters.AddWithValue("TinhTrang", cbxTinhTrang.Text);
            cmd.ExecuteNonQuery();
            Display();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string SqlSearch = "SELECT * FROM KSan WHERE MaPhong = @MaPhong";
            SqlCommand cmd = new SqlCommand(SqlSearch, con);
            cmd.Parameters.AddWithValue("MaPhong", txtIdRoom.Text);
            cmd.Parameters.AddWithValue("TenPhong", txtNameRoom.Text);
            cmd.Parameters.AddWithValue("Gia", txtPrice.Text);
            cmd.Parameters.AddWithValue("TenNguoiDat", txtNameUser.Text);
            cmd.Parameters.AddWithValue("LoaiPhong", cbxLoaiPhong.Text);
            cmd.Parameters.AddWithValue("TinhTrang", cbxTinhTrang.Text);
            cmd.ExecuteNonQuery();
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            tblRoom.DataSource = dt;
        }
    }
}



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;
using System.ComponentModel.Design;
using System.Configuration;

namespace QLKSan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();        
        }
        SqlConnection con;
        private void btnReset_Click(object sender, EventArgs e)
        {
            txtIdRoom.Text = "";
            txtNameRoom.Text = "";
            txtPrice.Text = "";
            txtNameUser.Text = "";
            cbxLoaiPhong.Text = "";
            cbxTinhTrang.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string conString = ConfigurationManager.ConnectionStrings["QLKS"].ConnectionString.ToString();
            con = new SqlConnection(conString);
            con.Open();
            Display();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            con.Close();
        }
        public void Display()
        {
            string sqlSELECT = "SELECT * FROM KSan";
            SqlCommand cmd = new SqlCommand(sqlSELECT, con);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            tblRoom.DataSource = dt;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string SqlINSERT = "INSERT INTO KSan VALUES (@MaPhong, @TenPhong,@Gia,@TenNguoiDat,@LoaiPhong,@TinhTrang)";
            SqlCommand cmd = new SqlCommand(SqlINSERT,con);
            cmd.Parameters.AddWithValue("MaPhong", txtIdRoom.Text);
            cmd.Parameters.AddWithValue("TenPhong", txtNameRoom.Text);
            cmd.Parameters.AddWithValue("Gia", txtPrice.Text);
            cmd.Parameters.AddWithValue("TenNguoiDat", txtNameUser.Text);
            cmd.Parameters.AddWithValue("LoaiPhong", cbxLoaiPhong.Text);
            cmd.Parameters.AddWithValue("TinhTrang", cbxTinhTrang.Text);
            cmd.ExecuteNonQuery();
            Display();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string SqlUpdate = "UPDATE KSan SET TenPhong = @TenPhong , Gia = @Gia, TenNguoiDat = @TenNguoiDat,LoaiPhong = @LoaiPhong,TinhTrang = @TinhTrang WHERE MaPhong = @MaPhong ";
            SqlCommand cmd = new SqlCommand(SqlUpdate, con);
            cmd.Parameters.AddWithValue("MaPhong", txtIdRoom.Text);
            cmd.Parameters.AddWithValue("TenPhong", txtNameRoom.Text);
            cmd.Parameters.AddWithValue("Gia", txtPrice.Text);
            cmd.Parameters.AddWithValue("TenNguoiDat", txtNameUser.Text);
            cmd.Parameters.AddWithValue("LoaiPhong", cbxLoaiPhong.Text);
            cmd.Parameters.AddWithValue("TinhTrang", cbxTinhTrang.Text);
            cmd.ExecuteNonQuery();
            Display();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string SqlDelete = "DELETE FROM KSan WHERE MaPhong = @MaPhong";
            SqlCommand cmd = new SqlCommand(SqlDelete, con);
            cmd.Parameters.AddWithValue("MaPhong", txtIdRoom.Text);
            cmd.Parameters.AddWithValue("TenPhong", txtNameRoom.Text);
            cmd.Parameters.AddWithValue("Gia", txtPrice.Text);
            cmd.Parameters.AddWithValue("TenNguoiDat", txtNameUser.Text);
            cmd.Parameters.AddWithValue("LoaiPhong", cbxLoaiPhong.Text);
            cmd.Parameters.AddWithValue("TinhTrang", cbxTinhTrang.Text);
            cmd.ExecuteNonQuery();
            Display();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string SqlSearch = "SELECT * FROM KSan WHERE MaPhong = @MaPhong";
            SqlCommand cmd = new SqlCommand(SqlSearch, con);
            cmd.Parameters.AddWithValue("MaPhong", txtIdRoom.Text);
            cmd.Parameters.AddWithValue("TenPhong", txtNameRoom.Text);
            cmd.Parameters.AddWithValue("Gia", txtPrice.Text);
            cmd.Parameters.AddWithValue("TenNguoiDat", txtNameUser.Text);
            cmd.Parameters.AddWithValue("LoaiPhong", cbxLoaiPhong.Text);
            cmd.Parameters.AddWithValue("TinhTrang", cbxTinhTrang.Text);
            cmd.ExecuteNonQuery();
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            tblRoom.DataSource = dt;
        }
    }
}



thienphu [T1907A]
thienphu

2020-06-02 14:54:38

https://github.com/ThienPhuDev97/T1907A_Java/tree/master/QuanLiKhachSan


hoangduyminh [T1907A]
hoangduyminh

2020-05-30 03:02:07



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 TesT01
{

    public partial class Form1 : Form
    {      
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection conn;
        private void btnThem_Click(object sender, EventArgs e)
        {
            
            string sqlthem = "INSERT INTO tblHotel VALUES (@maks, @nameks, @price, @tinhtrang)";
            SqlCommand cmd = new SqlCommand(sqlthem, conn);
            cmd.Parameters.AddWithValue("maks", txtMaKS.Text);
            cmd.Parameters.AddWithValue("nameks", txtNameKS.Text);
            cmd.Parameters.AddWithValue("price", txtPrice.Text);
            cmd.Parameters.AddWithValue("tinhtrang", cbTinhTrang.Text);
            cmd.ExecuteNonQuery();
            input();


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection("Data Source=DESKTOP-4NVNJ2G;Initial Catalog=KhachSan;Integrated Security=TrueData Source=DESKTOP-4NVNJ2G;Initial Catalog=KhachSan;Integrated Security=True");
            conn.Open();
            input();
        }
        public void input()
        {
            string sqlInsert = "select * from tblHotel";
            SqlCommand cmd = new SqlCommand(sqlInsert, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            tblHotel.DataSource = dt;
        }   

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            conn.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string sqlxoa = "DELETE FROM tblHotel WHERE maks = @maks";
            SqlCommand cmd = new SqlCommand(sqlxoa, conn);
            cmd.Parameters.AddWithValue("maks", txtMaKS.Text);
            cmd.Parameters.AddWithValue("nameks", txtNameKS.Text);
            cmd.Parameters.AddWithValue("price", txtPrice.Text);
            cmd.Parameters.AddWithValue("tinhtrang", cbTinhTrang.Text);
            cmd.ExecuteNonQuery();
            input();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            string sqlsua = "UPDATE tblHotel SET maks = @maks, nameks = @nameks, price = @price, tinhtrang = @tinhtrang Where maks = @maks";
            SqlCommand cmd = new SqlCommand(sqlsua, conn);
            cmd.Parameters.AddWithValue("maks", txtMaKS.Text);
            cmd.Parameters.AddWithValue("nameks", txtNameKS.Text);
            cmd.Parameters.AddWithValue("price", txtPrice.Text);
            cmd.Parameters.AddWithValue("tinhtrang", cbTinhTrang.Text);
            cmd.ExecuteNonQuery();
            input();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            string sqltim = "SELECT * FROM tblHotel WHERE maks = @maks";
            SqlCommand cmd = new SqlCommand(sqltim, conn);
            cmd.Parameters.AddWithValue("maks", txtMaKS.Text);
            cmd.Parameters.AddWithValue("nameks", txtNameKS.Text);
            cmd.Parameters.AddWithValue("price", txtPrice.Text);
            cmd.Parameters.AddWithValue("tinhtrang", cbTinhTrang.Text);
            cmd.ExecuteNonQuery();
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            tblHotel.DataSource = dt;
        }

        private void bntTimKiem_Click(object sender, EventArgs e)
        {
            string sqltimkiem = "SELECT * FROM tblHotel WHERE nameks = @nameks";
            SqlCommand cmd = new SqlCommand(sqltimkiem, conn);
            cmd.Parameters.AddWithValue("maks", txtMaKS.Text);
            cmd.Parameters.AddWithValue("nameks", txtNameKS.Text);
            cmd.Parameters.AddWithValue("price", txtPrice.Text);
            cmd.Parameters.AddWithValue("tinhtrang", cbTinhTrang.Text);
            cmd.ExecuteNonQuery();
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            tblHotel.DataSource = dt;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtMaKS.Text = "";
            txtNameKS.Text = "";
            txtPrice.Text = "";
            cbTinhTrang.Text = "";
        }
    }
}