其他分享
首页 > 其他分享> > 实验进度-15周

实验进度-15周

作者:互联网

登陆、注册和管理员操作都已经做好,个别细节正在完善。只剩学生个人信息操作没有做完。

登陆界面

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace Test
{
    public partial class Form1 : Form
    {
        private string VerifyCode = "";
        public Form1()
        {
            InitializeComponent();
            VerifyCode = MakeCode(5);
            pictureBox1.Image = CreateCodeImg(VerifyCode);
        }
        

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormRegister formRegister = new FormRegister();
            formRegister.Show();
        }

        private void button2_Click(object sender, EventArgs e)  //登陆
        {
                string username = textBoxUserName.Text.Trim();  //取出账号
                string password = EncryptWithMD5(textBoxPassWord.Text.Trim());  //取出密码

            
                //string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ToString(); //读取连接字符串
                string myConnString = "Data Source=.;Initial Catalog=Text;Persist Security Info=True;User ID=sa;Password=110023";  //连接字符串 Data source为服务器的名字  Text为连接的库

                SqlConnection sqlConnection = new SqlConnection(myConnString);  //实例化连接对象
                sqlConnection.Open();

                string sql = "select UserID,UserPassword from UserMessage where UserID = '" + username + "' and UserPassword = '" + password + "'";                                            //编写SQL命令
               
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

            if (sqlDataReader.HasRows && radioButton1.Checked)        //学生成功登录
            {
                MessageBox.Show("欢迎使用!");             //登录成功
                FormStudent formstudent = new FormStudent();
                formstudent.Show();
                //this.Hide();
            }
            if (sqlDataReader.HasRows && radioButton2.Checked)      //管理员成功登录
            {
                MessageBox.Show("欢迎使用!");             //登录成功
                FormAdmini formAdmini = new FormAdmini();
                formAdmini.Show();
                // this.Hide();
            }
            //验证码
            if (string.IsNullOrEmpty(txtVerifyCode.Text))
            {
                MessageBox.Show("请输入验证码!");
                return;
            }
            if (sqlDataReader.HasRows)
                {
                    if (txtVerifyCode.Text.Equals(VerifyCode, StringComparison.OrdinalIgnoreCase))
                    {
                        //MessageBox.Show("WELCOME!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);             //登录成功
                        label1.Text = "Log in :" + username;
                        //Form2 form2 = new Form2();     //打开新的窗体
                        //form2.Show();
                        //this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("验证码错误!请重新输入!");
                    }
                }
                else
                {
                    MessageBox.Show("密码输入错误!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                sqlConnection.Close();
        }
        private string MakeCode(int codeLen)
        {
            if (codeLen < 1)
            {
                return string.Empty;
            }

            int number;
            string checkCode = string.Empty;
            Random random = new Random();

            for (int index = 0; index < codeLen; index++)
            {

                number = random.Next();

                if (number % 2 == 0)
                {
                    checkCode += (char)('0' + (char)(number % 10));     //生成数字
                }
                else
                {
                    checkCode += (char)('A' + (char)(number % 26));     //生成字母
                }
            }

            return checkCode;
        }
        private Image CreateCodeImg(string checkCode)
        {
            if (string.IsNullOrEmpty(checkCode))
            {
                return null;
            }

            Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);

            Graphics graphic = Graphics.FromImage(image);

            try
            {
                Random random = new Random();

                graphic.Clear(Color.White);

                int x1 = 0, y1 = 0, x2 = 0, y2 = 0;

                for (int index = 0; index < 25; index++)
                {
                    x1 = random.Next(image.Width);
                    x2 = random.Next(image.Width);
                    y1 = random.Next(image.Height);
                    y2 = random.Next(image.Height);

                    graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }

                Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
                graphic.DrawString(checkCode, font, brush, 2, 2);

                int x = 0;
                int y = 0;

                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    x = random.Next(image.Width);
                    y = random.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }

                //画图片的边框线
                graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                return image;

            }
            finally
            {
                graphic.Dispose();
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            VerifyCode = MakeCode(5);
            pictureBox1.Image = CreateCodeImg(VerifyCode);
        }

        private void button3_Click(object sender, EventArgs e)//关闭
        {
            this.Close();
        }
        public static string EncryptWithMD5(string source)      //MD5加密
        {
            byte[] sor = Encoding.UTF8.GetBytes(source);
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(sor);
            StringBuilder strbul = new StringBuilder(40);
            for (int i = 0; i < result.Length; i++)
            {
                strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
            }
            return strbul.ToString();
        }
        
    }
}

注册
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class FormRegister : Form
    {
        public FormRegister()
        {
            InitializeComponent();
        }
        public Byte[] mybyte = new byte[0];
        internal static void show()
        {
            throw new NotImplementedException();
        }

        

        private void buttonphoto_Click(object sender, EventArgs e)//上传照片
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.ShowDialog();
            string picturePath = openFileDialog.FileName;//获取图片路径
            //创建FileStream对象
            FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
            //声明Byte数组
            mybyte = new byte[fs.Length];
            //读取数据
            fs.Read(mybyte, 0, mybyte.Length);
            Image image = Image.FromStream(fs);
            userphoto.Image = image;
            fs.Close();
        }

        private void buttoncancel_Click(object sender, EventArgs e)//取消
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)//注册成功
        {
            try
            {
                string connString = "Data Source =.; Initial Catalog = Text;  Persist Security Info = True;User ID = sa; Password =110023";//数据库连接字符串
                SqlConnection connection = new SqlConnection(connString);//创建connection对象
                string sql = "insert into UserMessage (UserName, UserID ,   UserPassword, UserIdentity ,UserSex, UserNumber ,UserBirthday ,UserPhoto ) " +
                                                        "values (@username, @userid, @userpassword,@useridentity,@usersex,@usernumber,@userbirthday,@userphoto)";
                SqlCommand command = new SqlCommand(sql, connection);

                
                SqlParameter sqlParameter = new SqlParameter("@userid", textBoxNum.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@username", textBoxName.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(textBoxPassword.Text));
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@usersex", comboBoxsex.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@usernumber", textBoxNumber.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@userbirthday", dateTimePickerbirth.Value);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@useridentity", comboBoxidentity.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@userphoto", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
                command.Parameters.Add(sqlParameter);
               
                //打开数据库连接
                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
                MessageBox.Show("注册成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.Close();
        }
        public static string EncryptWithMD5(string source)//MD5加密
        {
            byte[] sor = Encoding.UTF8.GetBytes(source);
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(sor);
            StringBuilder strbul = new StringBuilder(40);
            for (int i = 0; i < result.Length; i++)
            {
                strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
            }
            return strbul.ToString();
        }

        private void textBoxPassword_Leave(object sender, EventArgs e)//对密码的设置
        {
            if (textBoxPassword.Text.Trim() != "")
            {
                //使用regex(正则表达式)进行格式设置 至少有数字、大写字母各一个。最少3个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9])(?=.*[A-Z]).{3,20}");

                if (regex.IsMatch(textBoxPassword.Text))//判断格式是否符合要求
                {
                    //MessageBox.Show("输入密码格式正确!");
                }
                else
                {
                    MessageBox.Show("至少一个大写字母,最少3个字符、最长20个字符!");
                    textBoxPassword.Focus();
                }
            }
            else
            {
                MessageBox.Show("密码不能为空!");
            }

        }

        private void textBoxNum_Leave(object sender, EventArgs e)//对学号/工号的设置
        {
            if (textBoxNum.Text.Trim() != "")
            {
                //使用regex(正则表达式)进行格式设置 至少有数字最少7个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9]).{7,20}");

                if (regex.IsMatch(textBoxNum.Text))//判断格式是否符合要求
                {
                    //MessageBox.Show("输入密码格式正确!");
                }
                else
                {
                    MessageBox.Show("至少有7个字符、最长20个字符!");
                    textBoxNum.Focus();
                }
            }
            else
            {
                MessageBox.Show("用户名不能为空!");
            }

        }

        private void textBoxNumber_TextChanged(object sender, EventArgs e)//联系方式设置
        {

        }

        private void textBoxNumber_Leave(object sender, EventArgs e)//联系方式设置
        {
            if (textBoxNumber.Text.Trim() != "")
            {
                //使用regex(正则表达式)进行格式设置 至少有数字最少7个字符、最长20个字符。
                Regex regex = new Regex(@"(?=.*[0-9]).{7,20}");

                if (regex.IsMatch(textBoxNumber.Text))//判断格式是否符合要求
                {
                    //MessageBox.Show("输入联系方式格式正确!");
                }
                else
                {
                    MessageBox.Show("至少有7个字符、最长20个字符!");
                    textBoxNumber.Focus();
                }
            }
            else
            {
                //MessageBox.Show("联系方式不能为空!");
            }
        }

    }
}

管理员操作界面
在这里插入图片描述

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;

namespace Test
{
    public partial class FormAdmini : Form
    {
        public FormAdmini()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            StuInfor stuinfor = new StuInfor();
            stuinfor.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StuGrade stuGrade = new StuGrade();
            stuGrade.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            StuCourse stuCourse = new StuCourse();
            stuCourse.Show();
        }
    }
}

管理员-管理学生信息
(查询还没有完善,只能单条件)
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class StuInfor : Form
    {
        public StuInfor()
        {
            InitializeComponent();
        }

        private void StuInfor_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“textDataSet1.Student”中。您可以根据需要移动或删除它。
            this.studentTableAdapter.Fill(this.textDataSet1.Student);

        }
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Text;Persist Security Info=True;User ID=sa;Password=110023");

        private void buttonAdd_Click(object sender, EventArgs e)//增加
        {
            string StuSno = textBoxsno.Text.Trim();
            string StuSname = textBoxsname.Text.Trim();
            string StuSsex = textBoxsex.Text.Trim();
            string StuSage = textBoxsage.Text.Trim();
            string StuSdept = textBoxsdept.Text.Trim();
            //SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Text;  Persist Security Info = True;User ID = sa; Password = 110023"); //连接数据库
            try
            {
                con.Open();     //打开数据库
                string insertStr = "INSERT INTO Student(Sno,Sname,Ssex,Sage,Sdept)" + "VALUES('" + StuSno + "','" + StuSname + "','" + StuSsex + "'," + StuSage + ",'" + StuSdept + "')";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();      //将增加后的信息直接显示出来
            }
            catch
            {
                MessageBox.Show("输入数据违法要求!");
                if (StuSsex != "" && StuSsex != "男" && StuSsex != "女")
                    MessageBox.Show("性别输入有误,请检查后重新输入!");
            }
            finally
            {
                con.Close();      //关闭数据库
            }
            this.studentTableAdapter.Fill(this.textDataSet1.Student);
        }

        private void buttonDelete_Click(object sender, EventArgs e)//删除
        {
            try
            {
                con.Open();     //打开数据库
                string select_Sno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是Sno
                string delete_by_Sno = "DELETE FROM Student WHERE Sno='" + select_Sno + "'";//sql删除语句
                SqlCommand cmd = new SqlCommand(delete_by_Sno, con);
                cmd.ExecuteNonQuery();      //将增加后的信息直接出来
            }
            catch
            {
                MessageBox.Show("请选择正确行!");
            }
            finally
            {
                con.Close();      //关闭数据库
            }
            this.studentTableAdapter.Fill(this.textDataSet1.Student);

        }

        private void buttonModify_Click(object sender, EventArgs e)//修改
        {
            string StuSno = textBoxsno.Text.Trim();
            string StuSname = textBoxsname.Text.Trim();
            string StuSsex = textBoxsex.Text.Trim();
            string StuSage = textBoxsage.Text.Trim();
            string StuSdept = textBoxsdept.Text.Trim();
            try
            {
                string insertStr = "";
                string insertStr1 = "";
                string insertStr2 = "";
                string insertStr3 = "";
                con.Open();
                if (StuSname != "")
                {
                    insertStr = "UPDATE Student SET Sname = '" + StuSname + "' WHERE Sno = '" + StuSno + "'";    //修改名字
                    SqlCommand cmd = new SqlCommand(insertStr, con);
                    cmd.ExecuteNonQuery();
                }

                if (StuSsex != "")
                {
                    insertStr1 = "UPDATE Student SET Ssex = '" + StuSsex + "' WHERE Sno = '" + StuSno + "'";    //修改性别
                    SqlCommand cmd1 = new SqlCommand(insertStr1, con);
                    cmd1.ExecuteNonQuery();
                }
                if (StuSdept != "")
                {
                    insertStr2 = "UPDATE Student SET Sdept = '" + StuSdept + "' WHERE Sno = '" + StuSno + "'";    //修改专业
                    SqlCommand cmd2 = new SqlCommand(insertStr2, con);
                    cmd2.ExecuteNonQuery();
                }
                if (StuSage != "")
                {
                    insertStr3 = "UPDATE Student SET Sage = '" + StuSage + "' WHERE Sno = '" + StuSno + "'";    //修改年龄
                    SqlCommand cmd3 = new SqlCommand(insertStr3, con);
                    cmd3.ExecuteNonQuery();
                }
            }
            catch
            {
                MessageBox.Show("输入数据违反要求");
            }
            finally
            {
                con.Close();      //关闭数据库
            }
            this.studentTableAdapter.Fill(this.textDataSet1.Student);
        }

        private void buttonSearch_Click(object sender, EventArgs e)//查询
        {
            string StuSno = textBoxsno.Text.Trim();
            string StuSname = textBoxsname.Text.Trim();
            string StuSsex = textBoxsex.Text.Trim();
            string StuSage = textBoxsage.Text.Trim();
            string StuSdept = textBoxsdept.Text.Trim();
            String conn = "Data Source =.; Initial Catalog = Text;  Persist Security Info = True;User ID = sa; Password = 110023";
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象
            try
            {
                //只能单条件查询
                sqlconnection.Open();
                if(StuSno!="") //按学号查
                {
                    String select_by_sno = "select * from Student where Sno='" + StuSno + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_sno, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if(StuSname!="")//按姓名查
                {
                    String select_by_sname = "select * from Student where Sname='" + StuSname + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_sname, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if(StuSage!="")//按年龄查
                {
                    String select_by_sage = "select * from Student where Sage='" + StuSage + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_sage, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (StuSsex != "")//按性别查
                {
                    String select_by_ssex = "select * from Student where Ssex='" + StuSsex + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_ssex, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (StuSdept != "")//按系别查
                {
                    String select_by_sdept = "select * from Student where Sdept='" + StuSdept + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_sdept, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                //String select_by_sno = "select * from Student where Sno='" + StuSno + "'";

                //SqlCommand sqlcommand = new SqlCommand(select_by_sno, sqlconnection);
                //SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                //BindingSource bindingsource = new BindingSource();
                //bindingsource.DataSource = sqldatareader;
                // dataGridView1.DataSource = bindingsource;
                //将读出来的值赋给数据源,再将数据源给dataGridView
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

管理员-管理学生成绩
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class StuGrade : Form
    {
        public StuGrade()
        {
            InitializeComponent();
        }

        private void StuGrade_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“textDataSet2.SC”中。您可以根据需要移动或删除它。
            this.sCTableAdapter.Fill(this.textDataSet2.SC);

        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Text;Persist Security Info=True;User ID=sa;Password=110023");
        private void buttonSearch_Click(object sender, EventArgs e)//查询
        {
            string StuSno = textBoxsno.Text.Trim();
            string StuCno = textBoxcno.Text.Trim();
            string StuGrade = textBoxgrade.Text.Trim();
            String conn = "Data Source =.; Initial Catalog = Text;  Persist Security Info = True;User ID = sa; Password = 110023";
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象
            try
            {
                String select_by_id = "select * from SC where ";
                int flag = 0;  //0是单条件,1是多条件
                sqlconnection.Open();
                if (StuSno != "")
                {
                    select_by_id += "Sno='" + StuSno + "'"; flag = 1;  //按学号查询
                } 
                if (StuCno != "")
                {
                    if (flag == 0)
                    {
                        select_by_id += "Cno='" + StuCno + "'" + " ORDER BY Grade DESC";//按课程号查询
                        flag = 1;
                    }
                    else if (flag == 1)
                    { select_by_id += "And Cno='" + StuCno + "'"; } //按学号和课程号查询

                }
                SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlconnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;

            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句!");
            }
            finally
            {
                sqlconnection.Close();
            }
        }

        private void buttonModify_Click(object sender, EventArgs e)//修改分数
        {
            string StuSno = textBoxsno.Text.Trim();
            string StuCno = textBoxcno.Text.Trim();
            string StuGrade = textBoxgrade.Text.Trim();
            try
            {
                string insertStr = "";
                string insertStr1 = "";
                con.Open();
                if (StuCno != "")
                {
                    insertStr = "UPDATE SC SET Cno = '" + StuCno + "' WHERE Sno = '" + StuSno + "'" + "AND Grade='" + StuGrade + "'";    //修改课程
                    SqlCommand cmd = new SqlCommand(insertStr, con);
                    cmd.ExecuteNonQuery();
                }
                if (StuGrade != "")
                {
                    insertStr1 = "UPDATE SC SET Grade = '" + StuGrade + "' WHERE Sno = '" + StuSno + "'" + "AND Cno='" + StuCno + "'"; ;    //修改成绩
                    SqlCommand cmd1 = new SqlCommand(insertStr1, con);
                    cmd1.ExecuteNonQuery();
                }
            }
            catch
            {
                MessageBox.Show("输入数据违反要求");
            }
            finally
            {
                con.Close();      //关闭数据库
            }
            this.sCTableAdapter.Fill(this.textDataSet2.SC);
        }

        private void buttonDelete_Click(object sender, EventArgs e)//删除
        {
            try
            {
                con.Open();    //打开
                string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
                string select_cno = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//选择的当前行第2列的值,也就是cno
                string delete_by_id = "delete from SC where Sno=" + select_id + "AND Cno=" + select_cno;//sql删除语句
                SqlCommand cmd = new SqlCommand(delete_by_id, con);        //使用
                cmd.ExecuteNonQuery(); 
            }
            catch
            {
                MessageBox.Show("请选择正确行!");
            }
            finally
            {
                con.Close();      //关闭数据库
            }
            this.sCTableAdapter.Fill(this.textDataSet2.SC);
        }

        private void buttonAdd_Click(object sender, EventArgs e)//增加
        {
            string StuSno = textBoxsno.Text.Trim();
            string StuCno = textBoxcno.Text.Trim();
            string StuGrade = textBoxgrade.Text.Trim();
            try
            {
                con.Open();     //打开数据库
                string insertStr = "INSERT INTO SC(Sno,Cno,Grade)" + "VALUES('" + StuSno + "','" + StuCno + "','" + StuGrade + "')";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();      //将增加后的信息直接显示出来
            }
            catch
            {
                MessageBox.Show("输入数据违法要求!");
            }
            finally
            {
                con.Close();
            }
            this.sCTableAdapter.Fill(this.textDataSet2.SC);
        }

        
    }
}

管理员-管理学生选课

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class StuCourse : Form
    {
        public StuCourse()
        {
            InitializeComponent();
        }

        private void StuCourse_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“textDataSet3.Course”中。您可以根据需要移动或删除它。
            this.courseTableAdapter.Fill(this.textDataSet3.Course);

        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = Text;  Persist Security Info = True;User ID = sa; Password = 110023"); //连接数据库
        private void buttonAdd_Click(object sender, EventArgs e)//增加
        {
            String StuCno = textBoxcno.Text.Trim();
            String StuCname = textBoxcname.Text.Trim();
            String StuCpno = textBoxcpno.Text.Trim();
            String StuCcredit = textBoxccredit.Text.Trim();                 //读取需要插入的值
            //SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STUDENT;Persist Security Info=True;User ID=sa;Password=tangdou");
            try
            {
                con.Open();
                string insertStr = "INSERT INTO  Course (Cno,Cname,Cpno,Ccredit)    " +              //拼接字符串
                    "VALUES ('" + StuCno + "','" + StuCname + "','" + StuCpno + "','" + StuCcredit + "')";
                SqlCommand cmd = new SqlCommand(insertStr, con);
                cmd.ExecuteNonQuery();
            }
            catch
            {
                MessageBox.Show("输入数据违反要求!");
            }
            finally
            {
                con.Dispose();
            }
            this.courseTableAdapter.Fill(this.textDataSet3.Course);

        }

        private void buttonDelete_Click(object sender, EventArgs e)//删除
        {
            try
            {
                con.Open();     //打开数据库
                string select_Cno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是Sno
                string delete_by_Cno = "DELETE FROM Course WHERE Cno='" + select_Cno + "'";//sql删除语句
                SqlCommand cmd = new SqlCommand(delete_by_Cno, con);
                cmd.ExecuteNonQuery();      //将增加后的信息直接出来
            }
            catch
            {
                MessageBox.Show("请选择正确行!");
            }
            finally
            {
                con.Close();      //关闭数据库
            }
            this.courseTableAdapter.Fill(this.textDataSet3.Course);
        }

        private void buttonModify_Click(object sender, EventArgs e)//修改
        {
            string StuCno = textBoxcno.Text.Trim();
            string StuCname = textBoxcname.Text.Trim();
            string StuCpno = textBoxcpno.Text.Trim();
            string StuCcredit = textBoxccredit.Text.Trim();

            try
            {
                string insertStr = "";
                string insertStr1 = "";
                string insertStr2 = "";
               

                con.Open();
                if (StuCname != "")
                {
                    insertStr = "UPDATE Course SET Cname = '" + StuCname + "' WHERE Cno = '" + StuCno + "'";    //修改名字
                    SqlCommand cmd = new SqlCommand(insertStr, con);
                    cmd.ExecuteNonQuery();
                }

                if (StuCpno != "")
                {
                    insertStr1 = "UPDATE Course SET Cpno = '" + StuCpno + "' WHERE Cno = '" + StuCno + "'";    //修改先行课
                    SqlCommand cmd = new SqlCommand(insertStr1, con);
                    cmd.ExecuteNonQuery();
                }
                if (StuCcredit != "")
                {
                    insertStr2 = "UPDATE Course SET Ccredit = '" + StuCcredit + "' WHERE Cno = '" + StuCno + "'";    //修改学分
                    SqlCommand cmd2 = new SqlCommand(insertStr2, con);
                    cmd2.ExecuteNonQuery();
                }
            }
            catch
            {
                MessageBox.Show("输入数据违反要求");
            }
            finally
            {
                con.Close();      //关闭数据库
            }
            this.courseTableAdapter.Fill(this.textDataSet3.Course);
        }

        private void buttonSearch_Click(object sender, EventArgs e)//查询
        {
            string StuCno = textBoxcno.Text.Trim();
            string StuCname = textBoxcname.Text.Trim();

            String conn = "Data Source =.; Initial Catalog = Text;  Persist Security Info = True;User ID = sa; Password = 110023";
            SqlConnection sqlconnection = new SqlConnection(conn);//实例化连接对象
            try
            {
                sqlconnection.Open();
                if (StuCno != "") //按课程号查
                {
                    String select_by_cno = "select * from Course where Cno='" + StuCno + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_cno, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
                if (StuCname != "") //按课程名查
                {
                    String select_by_cname = "select * from Course where Cname='" + StuCname + "'";
                    SqlCommand sqlcommand = new SqlCommand(select_by_cname, sqlconnection);
                    SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
                    BindingSource bindingsource = new BindingSource();
                    bindingsource.DataSource = sqldatareader;
                    dataGridView1.DataSource = bindingsource;
                }
            }
            catch
            {
                MessageBox.Show("查询语句有误,请认真检查SQL语句");
            }
            finally
            {
                sqlconnection.Close();
            }
        
        }
    }
}

学生个人信息管理
正在写……
在这里插入图片描述

标签:15,string,Text,System,实验,进度,using,new,SqlCommand
来源: https://blog.csdn.net/Wjiahui/article/details/106455923