針對 C#+mysql數據模型+dataGridview 進行的 : 增加 刪除 修改 查詢 (狗血劇情)

1.查詢

查詢有簡單的單表查詢和多表查詢,大概瞭解了下多表查詢有多種方法目前還在學習中。。。。。

單表查詢

 private void button4_Click(object sender, EventArgs e)//根據表中某個字段進行查詢
        {

               //對搜索框輸入信息是否爲空進行判斷,也可根據要搜索的條件進行判斷  

               //(m_DB.mytable.Where(a=>a.name.Contains(textBox1.Text) || a.Numb.Contains(textBox1.Text)).Any())
            if (txtStuId.Text != null)

            {

                 //根據某個字段進行搜索,也可以進行模糊查詢Contains

                //List<mytable> querymsg = m_DB.mytable.Where(a => a.name.Contains(textBox1.Text) || a.Numb.Contains(textBox1.Text)).ToList();

                var sList = db.students.Where(a => a.StuId == txtStuId.Text.Trim()).ToList();
                bs.DataSource = sList;
            }
            else
            {
                MessageBox.Show("請輸入學號!");
            }


2.增加、修改

這倆功能一般會在一起,通過判斷數據是否爲新錄入來辨別是增加還是修改;

注意:新增時要進行實例化處理;修改時候要聲明一個空的變量就行修改。

 private void btnSave_Click(object sender, EventArgs e)//保存
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)//1.遍歷所有行
            {
                //2.判斷這一行的是新增的還是修改的
                int id = int.Parse(dataGridView1.Rows[i].Cells["ColId"].Value.ToString());//獲得id   如果是空或者0那就是新增的,如果不是就是修改
                if (id > 0)//修改
                {
                    //找到這個對應的一條信息,然後分別把表格裏的信息重新給他們賦值一遍
                    m_students = db.students.Where(a => a.Id == id).FirstOrDefault();
                    if (dataGridView1.Rows[i].Cells["ColStuId"].Value != null)
                        m_students.StuId = dataGridView1.Rows[i].Cells["ColStuId"].Value.ToString();
                    if (dataGridView1.Rows[i].Cells["ColStuName"].Value != null)
                        m_students.StuName = dataGridView1.Rows[i].Cells["ColStuName"].Value.ToString();
                    if (dataGridView1.Rows[i].Cells["ColSex"].Value != null)
                        m_students.Sex = dataGridView1.Rows[i].Cells["ColSex"].Value.ToString();
                    db.SaveChanges();//保存
                }
                else//新增
                {
                    students m_students = new students();//實例化一個新的對象,因爲是新增的
                    m_students.Id = db.students.Count() <= 0 ? 1 : db.students.OrderByDescending(a => a.Id).Select(a => a.Id).FirstOrDefault() + 1;//判斷是否有數據,有就倒序加1作爲ID
                    if (dataGridView1.Rows[i].Cells["ColStuId"].Value != null)
                        m_students.StuId = dataGridView1.Rows[i].Cells["ColStuId"].Value.ToString();//和修改一樣
                    if (dataGridView1.Rows[i].Cells["ColStuName"].Value != null)
                        m_students.StuName = dataGridView1.Rows[i].Cells["ColStuName"].Value.ToString();//和修改一樣
                    if (dataGridView1.Rows[i].Cells["ColSex"].Value != null)
                        m_students.Sex = dataGridView1.Rows[i].Cells["ColSex"].Value.ToString();//和修改一樣
                    db.students.Add(m_students);//把這個實例化的新的放到添加方法
                    db.SaveChanges();//保存
                }

            }
            MessageBox.Show("保存成功!");
        }

3.刪除

在刪除操作過程中要記得數據自動刷新


 private void button3_Click(object sender, EventArgs e)
        {
            int index = dataGridView1.CurrentCell.RowIndex;//獲取當前活動單元格的行
            if (index >= 0)//原有的數據刪除
            {
                if (int.Parse(dataGridView1.Rows[index].Cells["ColId"].Value.ToString()) > 0)//id大於0,說有這一行數據數據庫裏面有,現在做刪除
                {


                    //如果點擊確定,那就直接刪除  
                    //1.首先根據id找到這條數據 2.刪除這條數據  3. 保存,刷新當前表格
                    int id = int.Parse(dataGridView1.Rows[index].Cells["ColId"].Value.ToString());
                    m_students = db.students.Where(a => a.Id == id).First();
                    db.students.Remove(m_students);
                    db.SaveChanges();
                    MessageBox.Show("刪除成功!");
                    button1_Click(null, null);//重新加載數據  (var stuList = db.students.AsNoTracking().ToList(); bs.DataSource = stuList;)
         

                }
                else//id=0,說明是剛剛增加的,還沒有保存,就直接移除
                {
                    dataGridView1.Rows.RemoveAt(index);
                    return;
                }
            }
        }








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章