2021年5月23日 星期日

WinFormTb01

https://www.youtube.com/watch?v=X-IPIfHlrKY&list=PLumjEWemDx2ytsN5ILEkDg-kFEyqh9Z9G&index=10&t=1654s

 


namespace WinFormsInformation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-KOD5HR3;Initial Catalog=IT3Student;Integrated Security=True");
        int ID = 0;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            SelectStudentTable();
        }      
        private void btnInser_Click(object sender, EventArgs e)
        {
            if (txtStuName.Text == string.Empty)
                MessageBox.Show("請填寫名子", "表格未填寫完整", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("Insert Into StudentsTb Values(@Name, @FatherName, @RollNumber, @Address, @Mobile)", con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Name",txtStuName.Text);
                cmd.Parameters.AddWithValue("@FatherName", txtFaName.Text);
                cmd.Parameters.AddWithValue("@RollNumber", txtRollNumber.Text);
                cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
                cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("DATA輸入成功", "輸入", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }          
            ResetTextBox();
            SelectStudentTable();
        }
 
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (this.ID > 0)
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("UPDATE StudentsTb SET StuName=@Name, FatherName=@FatherName, "
                + "RollNumber=@RollNumber, Address=@Address, Mobile=@Mobile WHERE StudentID=@ID", con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@ID", this.ID);
                cmd.Parameters.AddWithValue("@Name", txtStuName.Text);
                cmd.Parameters.AddWithValue("@FatherName", txtFaName.Text);
                cmd.Parameters.AddWithValue("@RollNumber", txtRollNumber.Text);
                cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
                cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("DATA更新成功", "更新", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }         
            ResetTextBox();
            SelectStudentTable();
        }
 
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.ID > 0)
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("DELETE FROM StudentsTb WHERE StudentID=@ID", con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@ID", this.ID);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("DATA刪除成功", "刪除", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            ResetTextBox();
            SelectStudentTable();
        }
 
        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetTextBox();
        }
        private void StuTableView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            ID = Convert.ToInt32(StuTableView.SelectedRows[0].Cells[0].Value.ToString());
            txtStuName.Text = StuTableView.SelectedRows[0].Cells[1].Value.ToString();
            txtFaName.Text = StuTableView.SelectedRows[0].Cells[2].Value.ToString();
            txtRollNumber.Text = StuTableView.SelectedRows[0].Cells[3].Value.ToString();
            txtAddress.Text = StuTableView.SelectedRows[0].Cells[4].Value.ToString();
            txtMobile.Text = StuTableView.SelectedRows[0].Cells[5].Value.ToString();           
        }
        private void ResetTextBox()
        {
            ID = 0;
            txtAddress.Clear();
            txtFaName.Clear();
            txtMobile.Clear();
            txtRollNumber.Clear();
            txtStuName.Clear();
        }
        private void SelectStudentTable()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM StudentsTb", con);
            SqlDataReader sdr = cmd.ExecuteReader();
            DataTable table = new System.Data.DataTable();
            table.Load(sdr);
            con.Close();
            StuTableView.DataSource = table;
        }
    }
}

沒有留言:

張貼留言

WinFormTb02

https://drive.google.com/drive/u/0/folders/1UwS9FZ3ELCOK6SAwirHrkxq3z_RSbxJt https://www.youtube.com/watch?v=k7IkIeww_U0&list=PLumjEWemD...