c#winform学生管理系统(思路)

1.主体思路

  • 增加学生信息:

        首先将数据库中的数据填充到表(为完成能够再改的时候在dataGridView1中实时表示),然后使用sql代码(insert into 表(属性1,属性2....)values(值1,值2,.......))进行添加。

  • 删除学生信息:

        使用sql代码(Delete from 表 where 条件)删除指定信息。

  • 更改学生信息:

        首先利用二维数组将textBox中的Text与laber进行匹配(一.是为了判断哪些没写哪些写了,二.是为了跟好的准确锁定)然后利用sql代码(update 表 set 属性=值,..... where 条件 ),这里需要注意  字符串需要加双引号(在第一个双引号的里面) 而数字(哪怕是字符串类型数字)不需要加双引号。

  • 查找学生信息

        使用sql代码读取所有数据库中的数据,利用OleDbDateReader read.HasRows判断是否有行,然后依次将属性列全部填充到表,再然后利用read.Read()来定位到每一行将每一行的信息都填充到表。然后for循环依次判断是否是我要找的。

操作:

1. 用C#与数据库交互,添加,删除,更改,查找。

(1 删除

​
OleDbConnection oleDb = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\access\StudentManage.mdb");//连接代码
            OleDbCommand com = oleDb.CreateCommand();
            oleDb.Open();
            if(tag != null)
            {
                com.CommandText = "Delete from student where 学生编号 = " + tag;//tag是存放编号的变量
                com.ExecuteReader();//运行sql命令
                com.Dispose();//释放
                oleDb.Close();//关闭
                MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                data.Clear();
                this.dataGridView1.DataSource = data;
            }
            else
            {
                MessageBox.Show("删除失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
           

​

(2 添加

int count = 0;
            OleDbConnection oleDb = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\access\StudentManage.mdb");
            OleDbCommand com = oleDb.CreateCommand();
            oleDb.Open();
            com.CommandText = "Select * from student";
            OleDbDataReader read = com.ExecuteReader();
            while (read.Read())
            {
                count = (int)read[0];
            }            
            count++;
            com.Dispose();
            oleDb.Close();
            oleDb.Open();
            OleDbCommand com1 = oleDb.CreateCommand();
            com1.CommandText = "insert into student(学生编号,姓名,性别,年龄,语文,数学,英语,计算机网络,电话号码,身份证号)values('" + count + "','" + this.textBox1.Text +"','" + this.comboBox1.Text +"','"+ (this.textBox2.Text) + "','"+( this.textBox7.Text) +"','"+ ( this.textBox3.Text) +"','"+ ( this.textBox8.Text) +"','"+ ( this.textBox4.Text )+"','"+ this.textBox9.Text +"','"+ this.textBox5.Text +"')";
            com1.ExecuteReader();
            MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
           

(3 更改

//更改
        DataColumn column;
        DataRow row1;
        private void button2_Click(object sender, EventArgs e)
        {
            int[] value = new int[10];
            int p = 0;
            //属性与值匹配
            string[,] tag = new string[2, 9]
            {
                { this.textBox1.Text,
                this.comboBox2.Text,
                this.textBox4.Text,
                this.textBox5.Text,
                this.textBox6.Text,
                this.textBox7.Text,
                this.textBox8.Text,
                this.textBox9.Text,
                this.textBox3.Text
                },
                {
                    this.label2.Text,
                    this.label4.Text,
                    this.label5.Text,
                    this.label6.Text,
                    this.label7.Text,
                    this.label8.Text,
                    this.label9.Text,
                    this.label10.Text,
                    this.label3.Text
                }
            };
            //更改: update 表 set 属性 = 值 where(可加也可不加) 条件
            for (int i = 0; i < 9;i++)
            {
                if(tag[0,i] != "")
                {
                    //存放值
                    value[p++] = i;
                }
            }
            if(p != 0)
            {
                for (int i = 0; i < data.Rows.Count; i++)//遍历表
                {
                    for (int j = 0; j < 9; j++)//遍历属性
                    {
                        column = data.Columns[j];
                        for (int k = 0; k < p; k++)//遍历有更改的属性
                        {
                            if (column.ColumnName == tag[1, value[k]])//判断是否相等,并更改
                            {
                                DataRow row = data.Rows[i];
                                row[j] = tag[0, value[k]];
                            }
                        }
                    }
                }
                this.dataGridView1.DataSource = data;
                using (OleDbConnection OleDb = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\access\StudentManage.mdb"))
                {
                    OleDbCommand com = OleDb.CreateCommand();
                    OleDb.Open();
                    for (int j = 0; j < data.Rows.Count; j++)
                    {
                        for (int i = 0; i < p; i++)//遍历有多少要改的属性 
                        {
                            row1 = data.Rows[j];
                            string str1 = tag[1, value[i]];
                            string str2 = tag[0, value[i]];
                            com.CommandText = "update student set " + str1 + "=" + "'" + str2 + "'" + " where 学生编号=" + row1[0];
                            com.Connection = OleDb;
                            OleDbDataReader read = com.ExecuteReader();
                            read.Close();
                            com.Dispose();
                        }
                    }
                    MessageBox.Show("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请输入修改的值!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            

(4 查找

 Button button = sender as Button;
            OleDbConnection oled = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\access\StudentManage.mdb");
            OleDbCommand com = oled.CreateCommand();
            oled.Open();
            com.CommandText = "Select * from student";
            OleDbDataReader read = com.ExecuteReader();
            DataTable data = new DataTable();
            if(read.HasRows)
            {
                for(int i = 0; i < read.FieldCount;i++)
                {
                    data.Columns.Add(read.GetName(i));
                }
            }
            switch (button.Text)//依次判断有没有填
            {
                case "姓名":
                    {
                        while (read.Read())
                        {
                            if(read[1].ToString() == this.textBox1.Text)
                            {
                                DataRow row = data.NewRow();
                                for(int i = 0;i < read.FieldCount;i++)
                                {
                                    row[i] = read[i];
                                }
                                data.Rows.Add(row);
                            }
                        }
                        this.dataGridView1.DataSource = data;
                        break;
                    }
                case "年龄":
                    {
                        while (read.Read())
                        {
                            if (read[3].ToString() == this.textBox2.Text)
                            {
                                DataRow row = data.NewRow();
                                for (int i = 0; i < read.FieldCount; i++)
                                {
                                    row[i] = read[i];
                                }
                                data.Rows.Add(row);
                            }
                        }
                        this.dataGridView1.DataSource = data;
                        break;
                    }
                case "性别":
                    {
                        while (read.Read())
                        {
                            if (read[2].ToString() == this.comboBox1.Text)
                            {
                                DataRow row = data.NewRow();
                                for (int i = 0; i < read.FieldCount; i++)
                                {
                                    row[i] = read[i];
                                }
                                data.Rows.Add(row);
                            }
                        }
                        this.dataGridView1.DataSource = data;
                        break;
                    }
                case "电话号码":
                    {
                        while (read.Read())
                        {
                            if (read[8].ToString() == this.textBox3.Text)
                            {
                                DataRow row = data.NewRow();
                                for (int i = 0; i < read.FieldCount; i++)
                                {
                                    row[i] = read[i];
                                }
                                data.Rows.Add(row);
                            }
                        }
                        this.dataGridView1.DataSource = data;
                        break;
                    }
                case "身份证号":
                    {
                        while (read.Read())
                        {
                            if (read[9].ToString() == this.textBox4.Text)
                            {
                                DataRow row = data.NewRow();
                                for (int i = 0; i < read.FieldCount; i++)
                                {
                                    row[i] = read[i];
                                }
                                data.Rows.Add(row);
                            }
                        }
                        this.dataGridView1.DataSource = data;
                        break;
                    }
                
            }
            com.Dispose();
            oled.Close();
        }
    

2.所遇问题

为了将MessBox信息窗口定时消失(遇到的问题且想了很久)    

[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        private extern static IntPtr FindWindow(string Lp, string Ip);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int PostMessage(IntPtr hw, int msg, IntPtr wp, IntPtr ip);
        public const int WM_CLOSE = 0x10;

//后面的代码需要先调用start 然后起连锁反应

private void start()
        {
            Timer time = new Timer();
            time.Interval = 1000;
            time.Tick += new EventHandler(Timer_Tick);//时间到了之后触发
            time.Start();//开始计时
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            KillMessageBox();//时间到时将其删去
            ((Timer)sender).Stop();//停止计时
        }

        private void KillMessageBox()
        {
            IntPtr ptr = FindWindow(null, "提示");//找到窗口句柄
            if(ptr != IntPtr.Zero)
            {
                PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);//删除
            }
        }

                                                                                                                                   惟愿如初

上一篇:使用pymysql读取mysql游标数据集------对结果集进行行列显示,并左右对齐显示


下一篇:杨辉三角--蓝桥杯,C