【数据库实验】C#代码汇总

 

按照顺序进行展示,只展示重要代码。

 

注册、登录界面:

 

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 Login
{
    public partial class Form20 : Form
    {

        public Form20()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form1 form1 = new Form1();
            form1.ShowDialog();
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form21 form21 = new Form21();
            form21.ShowDialog();
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form22 form22 = new Form22();
            form22.Show();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form23 form23 = new Form23();
            form23.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.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Login
{
    public partial class Form22 : Form
    {

        private string Rcode;
        private static char[] constant =
        {   '0','1','2','3','4','5','6','7','8','9',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
        };


        public Form22()
        {
            InitializeComponent();
            this.Randomcode();
        }

        public void Randomcode()        //随机生成验证码函数
        {
            int i;
            Rcode = "";
            Random rand = new Random();
            for (i = 0; i < 4; i++)
            {
                Rcode += constant[rand.Next(0, 57)];
            }
            textBox6.Text = Rcode;

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Randomcode();
        }

        public static string EncryptWithMD5(string source)
        {
            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 button1_Click(object sender, EventArgs e)
        {
            string code = textBox5.Text.Trim();     //验证码
            code = code.ToUpper();
            Rcode = Rcode.ToUpper();
            if (code != Rcode)
            {
                MessageBox.Show("验证码错误!");
                this.Randomcode();
            }
            else
            {
                if (label7.Text != "" || label8.Text != "")
                {
                    MessageBox.Show("用户名或密码格式错误", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Randomcode();
                }
                else
                {
                        if (textBox3.Text == "")
                        {
                            MessageBox.Show("学号不能为空");
                            this.Randomcode();
                        }
                        else
                        {
                            if (textBox4.Text == "")
                            {
                                MessageBox.Show("电话号码不能为空");
                                this.Randomcode();
                            }
                            else
                            {
                                try
                                {
                                    string connString = "Data Source=.;Initial Catalog=STU;Persist Security Info=True;User ID=sa;Password=******";//数据库连接
                                    SqlConnection connection = new SqlConnection(connString);
                                    string sql = "insert into Utable (userid, password ,Stuno, phone) " +
                                                                            "values (@userid, @password,@number,@phone)";
                                    SqlCommand command = new SqlCommand(sql, connection);

                                    SqlParameter sqlParameter = new SqlParameter("@userid", textBox1.Text);
                                    command.Parameters.Add(sqlParameter);
                                    sqlParameter = new SqlParameter("@password", EncryptWithMD5(textBox2.Text));
                                    command.Parameters.Add(sqlParameter);
                                    sqlParameter = new SqlParameter("@number", textBox3.Text);
                                    command.Parameters.Add(sqlParameter);
                                    sqlParameter = new SqlParameter("@phone", textBox4.Text);
                                    command.Parameters.Add(sqlParameter);

                                    //打开数据库连接
                                    connection.Open();
                                    command.ExecuteNonQuery();
                                    connection.Close();
                                    MessageBox.Show("注册成功");
                                    this.Close();
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                            }
                        }
                    }
                }
            }

        private void textBox1_Leave(object sender, EventArgs e)                     //查询该用户名是否已经存在
        {
            string username = textBox1.Text.Trim();
            string myConnString = "Data Source=.;Initial Catalog=STU;Persist Security Info=True;User ID=sa;Password=******";
            SqlConnection sqlConnection = new SqlConnection(myConnString);
            sqlConnection.Open();
            string sql = "select userid from Utable where userid = '" + username + "'";  //编写SQL命令
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            if (sqlDataReader.HasRows)
            {
                label7.Text = "*该用户名已经存在";
            }
            else
            {
                if (textBox1.Text.Trim() != "")
                {
                    label7.Text = "";
                }
                else
                {
                    label7.Text = "*用户名不能为空";
                }
            }
            sqlConnection.Close();
        }

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

                if (regex.IsMatch(textBox2.Text))//判断格式是否符合要求
                {
                    label8.Text = "";
                }
                else
                {
                    label8.Text = "*至少包含数字、大小写字母各一个。且不超过20个字符";
                }
            }
            else
            {
                label8.Text = "*密码不能为空";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form20 form20 = new Form20();       //实例化
            form20.ShowDialog();             //新打开窗体
            this.Close();                   //关闭当前窗体
        }
    }
}

 

选择表

同时这里有管理员可见的注册用户表的隐藏、显示代码,以此为例。

 

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 Login
{
    public partial class Form11 : Form
    {
        public Form11()
        {
            InitializeComponent();
            if (Form21.UFlag == 1)
                button5.Hide();
        }

        private void button1_Click(object sender, EventArgs e)      //学生基本信息
        {
            this.Hide();                     //隐藏当前窗体
            Form4 form4 = new Form4();       //实例化
            form4.ShowDialog();             //新打开窗体
            this.Close();                   //关闭当前窗体
        }

        private void button2_Click(object sender, EventArgs e)      //学生课程
        {
            this.Hide();                     //隐藏当前窗体
            Form9 form9 = new Form9();       //实例化
            form9.ShowDialog();             //新打开窗体
            this.Close();                   //关闭当前窗体
        }

        private void button3_Click(object sender, EventArgs e)      //学生分数
        {
            this.Hide();                     //隐藏当前窗体
            Form10 form10 = new Form10();       //实例化
            form10.ShowDialog();             //新打开窗体
            this.Close();                   //关闭当前窗体
        }

        private void button5_Click(object sender, EventArgs e)      //用户注册信息
        {
            this.Hide();                     //隐藏当前窗体
            Form24 form24 = new Form24();       //实例化
            form24.ShowDialog();             //新打开窗体
            this.Close();                   //关闭当前窗体
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void 制作名单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("澪0炎");        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("制作不易、请多包涵~");
        }

        private void Form11_Load(object sender, EventArgs e)
        {

        }
    }
}

 

Student表:

 

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 Login
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
            if(Form21.UFlag == 1)
            {
                button2.Hide();
                button3.Hide();
                button4.Hide();
            }
        }

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

        }



        private void button1_Click(object sender, EventArgs e)          //返回
        {
            this.Hide();
            Form11 form11 = new Form11();       //实例化
            form11.ShowDialog();             //新打开窗体
            this.Close();                   //关闭当前窗体
        }

        private void button6_Click(object sender, EventArgs e)      //刷新
        {
            this.studentTableAdapter.Fill(this.sTUDataSet3.Student);
            MessageBox.Show("刷新");

            SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");
            try
            {
                String select_by_id = "select * from Student";

                con.Open();
                SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }  
        }


        private void button2_Click(object sender, EventArgs e)          //增
        {
            Form5 form5 = new Form5();
            form5.Show();
        }


        private void button3_Click(object sender, EventArgs e)          //删
        {
            try
            {
                string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();     //读取改行第一列的值,即Sno主码
                Form6 form6 = new Form6(select_id);
                form6.Show();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {

            }
        }

        private void button4_Click(object sender, EventArgs e)      //改
        {
            try
            {
                string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();     //读取改行第一列的值,即Sno主码
                Form7 form7 = new Form7(select_id);
                form7.Show();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {

            }
        }

        private void button5_Click(object sender, EventArgs e)      //查
        {
            SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");

            if (Form8.flag == 0)         //按学号查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_id = "select * from Student where Sno='" + Form8.StuText + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

            else if (Form8.flag == 1)         //按姓名查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_name = "select * from Student where Sname='" + Form8.StuText + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_name, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

            else if (Form8.flag == 2)         //按性别查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_sex = "select * from Student where Ssex='" + Form8.StuSex + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_sex, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

            else if (Form8.flag == 3)         //按年龄查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_age = "select * from Student where Sage='" + Form8.StuText + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_age, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

            else if (Form8.flag == 4)         //按系别查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_dept = "select * from Student where Sdept='" + Form8.Studept + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_dept, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

        }


        private void button7_Click(object sender, EventArgs e)      //查找方式
        {
            Form8 form8 = new Form8();
            form8.Show();
        }
    }
}

 

course表:

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 Login
{
    public partial class Form9 : Form
    {
        public Form9()
        {
            InitializeComponent();
            if (Form21.UFlag == 1)
            {
                button2.Hide();
                button3.Hide();
                button4.Hide();
            }
        }



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

        }


        public static string select_id;         //全局变量

        private void button1_Click(object sender, EventArgs e)          //返回
        {
            this.Hide();
            Form11 form11 = new Form11();       //实例化
            form11.ShowDialog();             //新打开窗体
            this.Close();                   //关闭当前窗体
        }

        private void button6_Click(object sender, EventArgs e)      //刷新
        {
            this.courseTableAdapter.Fill(this.sTUDataSet1.Course);
            MessageBox.Show("刷新");

            SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");
            try
            {
                String select_by_id = "select * from Course";

                con.Open();
                SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }
        }


        private void button2_Click(object sender, EventArgs e)          //增
        {
            Form12 form12 = new Form12();
            form12.Show();
        }


        private void button3_Click(object sender, EventArgs e)          //删
        {
            try
            {
                select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();     //读取改行第一列的值,即Cno主码
                Form13 form13 = new Form13();
                form13.Show();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {

            }
        }

        private void button4_Click(object sender, EventArgs e)      //改
        {
            try
            {
                select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();     //读取改行第一列的值,即Sno主码
                Form14 form14 = new Form14();
                form14.Show();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {

            }
        }

        private void button5_Click(object sender, EventArgs e)      //查
        {
            SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");

            if (Form15.flag == 0)         //按课程号查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_id = "select * from Course where Cno='" + Form15.CouText + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

            else if (Form15.flag == 1)         //按姓名查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_name = "select * from Course where Cname='" + Form15.CouText + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_name, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

            else if (Form15.flag == 2)         //按学分查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_credit = "select * from Course where Ccredit='" + Form15.CouText + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_credit, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

        }


        private void button7_Click(object sender, EventArgs e)      //查找方式
        {
            Form15 form15 = new Form15();
            form15.Show();
        }


    }
}

 

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 Login
{
    public partial class Form10 : Form
    {
        public Form10()
        {
            InitializeComponent();
            if (Form21.UFlag == 1)
            {
                button2.Hide();
                button3.Hide();
                button4.Hide();
            }
        }

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

        }

        public static string select_id;     //全局变量
        public static string select_cno;

        private void button1_Click(object sender, EventArgs e)          //返回
        {
            this.Hide();
            Form11 form11 = new Form11();       //实例化
            form11.ShowDialog();             //新打开窗体
            this.Close();                   //关闭当前窗体
        }

        private void button6_Click(object sender, EventArgs e)      //刷新
        {
            this.sCTableAdapter.Fill(this.sTUDataSet2.SC);
            MessageBox.Show("刷新");

            SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");
            try
            {
                String select_by_id = "select * from SC";

                con.Open();
                SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = sqlDataReader;
                dataGridView1.DataSource = bindingSource;
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }
        }

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

        private void button3_Click(object sender, EventArgs e)          //删
        {
            try
            {
                select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();     //读取改行第一列的值,即Sno主码
                select_cno = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//读取改行第二列的值,即Cno主码
                Form17 form17 = new Form17();
                form17.Show();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {

            }
        }

        private void button4_Click(object sender, EventArgs e)      //改
        {
            try
            {
                select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();     //读取改行第一列的值,即Sno主码
                select_cno = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//读取改行第二列的值,即Cno主码
                Form18 form18 = new Form18();
                form18.Show();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {

            }
        }

        private void button5_Click(object sender, EventArgs e)      //查
        {
            SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");

            if (Form19.flag == 0)         //按学号查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_id = "select * from SC where Sno='" + Form19.SCText + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

            else if (Form19.flag == 1)         //按课序号查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_con = "select * from sc where Cno='" + Form19.SCText + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_con, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

            else if (Form19.flag == 2)         //按分数查询
            {
                try
                {
                    con.Open();     //打开
                    String select_by_grade = "select * from SC where Grade='" + Form19.SCText + "'";
                    SqlCommand sqlCommand = new SqlCommand(select_by_grade, con);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = sqlDataReader;
                    dataGridView1.DataSource = bindingSource;
                    MessageBox.Show("查询结束后请刷新");
                }
                catch
                {
                    MessageBox.Show("错误!请重新操作!");
                }
                finally
                {
                    con.Close();
                }
            }

        }



        private void button7_Click(object sender, EventArgs e)      //查找方式
        {
            Form19 form19 = new Form19();
            form19.Show();
        }
    }
}

 

增、删、改、查以Student表为例:

 

增:

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 Login
{
    public partial class Form5 : Form
    {
        private object studentTableAdapter;
        private object sTUDENTDataSet1;
        private object sinformationDataSet;
        private object sTUDataSet3;
        private string select_id;

        public Form5()
        {
            InitializeComponent();
        }

        public Form5(string select_id)
        {
            this.select_id = select_id;        //Sno主码
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string StuNum = textBox1.Text.Trim();
            string StuName = textBox2.Text.Trim();
            string StuSex = comboBox1.Text;
            string StuAge = textBox3.Text.Trim();
            string StuDept = comboBox2.Text;        

            SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");
            try
            {
                con.Open();     //打开
                string ComStr = "INSERT INTO Student(Sno, Sname, Ssex, Sage, Sdept)" + "VALUES ('" + StuNum + "','" + StuName + "','" + StuSex + "'," + StuAge + ",'" + StuDept + "')"; //SQL添加语句
                SqlCommand cmd = new SqlCommand(ComStr, con);
                cmd.ExecuteNonQuery();      //返回执行命令后影响的参数
                MessageBox.Show("添加成功!请点击“刷新”按钮");
                this.Close();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }

        }

        private void button2_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 Login
{
    public partial class Form6 : Form
    {
        private string select_id;

        public Form6()
        {
            InitializeComponent();
        }

        public Form6(string select_id)      //参数
        {
            this.select_id = select_id;
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
           SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=STU;Persist Security Info=True;User ID=sa;Password=******");
            try
            {
                con.Open();
                string delete_by_id = "delete from Student where Sno=" + select_id;        //SQL删除语句
                SqlCommand cmd = new SqlCommand(delete_by_id, con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("删除成功!请点击“刷新”按钮");
                this.Close();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }
        }

        private void button2_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 Login
{
    public partial class Form7 : Form
    {

        private string select_id;

        public Form7()
        {
            InitializeComponent();
        }

        public Form7(string select_id)      //参数
        {
            this.select_id = select_id;
            InitializeComponent();
            textBox1.Text = select_id;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string StuNum = textBox1.Text.Trim();
            string StuName = textBox2.Text.Trim();
            string StuSex = comboBox1.Text;
            string StuAge = textBox3.Text.Trim();
            string StuDept = comboBox2.Text;

            SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");
            try
            {
                con.Open();     //打开
                string ComStr = "UPDATE Student SET Sname='" + StuName + "',Ssex='" + StuSex + "',Sage=" + StuAge + ",Sdept='" + StuDept + "'" + " WHERE Sno='" + StuNum + "'"; //SQL添加语句
                SqlCommand cmd = new SqlCommand(ComStr, con);
                cmd.ExecuteNonQuery();      //返回执行命令后影响的参数
                MessageBox.Show("修改成功!请点击“刷新”按钮");
                this.Close();
            }
            catch
            {
                MessageBox.Show("错误!请重新操作!");
            }
            finally
            {
                con.Close();
            }
        }

        private void button2_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 Login
{
    public partial class Form8 : Form
    {
        public Form8()
        {
            InitializeComponent();
            textBox1.Hide();
            comboBox2.Hide();
            comboBox3.Hide();
            button3.Hide();
        }

        public static string StuText;
        public static string StuSex;
        public static string Studept;
        public static int flag = -1;





        private void button1_Click(object sender, EventArgs e)      //选择
        {
            string StuCombo = comboBox1.Text.Trim();
            
            if (comboBox1.Text == "学号")
            {
                flag = 0;
                textBox1.Show();
                button3.Show();
            }
            else if (comboBox1.Text == "姓名")
            {
                flag = 1;
                textBox1.Show();
                button3.Show();
            }
            else if (comboBox1.Text == "性别")
            {
                flag = 2;
                comboBox2.Show();
                button3.Show();
            }
            else if (comboBox1.Text == "年龄")
            {
                flag = 3;
                textBox1.Show();
                button3.Show();
            }
            else if (comboBox1.Text == "系别")
            {
                flag = 4;
                comboBox3.Show();
                button3.Show();
            }

            MessageBox.Show("请输入");
        }

        private void button3_Click(object sender, EventArgs e)      //输入、确定
        {
            StuText = textBox1.Text.Trim();
            StuSex = comboBox2.Text.Trim();
            Studept = comboBox3.Text.Trim();
            MessageBox.Show("请点击“查询”按钮查询");
            this.Close();
        }


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


    }
}

 

用户注册表:

 

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 Login
{
    public partial class Form24 : Form
    {
        public Form24()
        {
            InitializeComponent();
        }

        private void Form24_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“sTUDataSet6.SysLog”中。您可以根据需要移动或删除它。
            this.sysLogTableAdapter.Fill(this.sTUDataSet6.SysLog);
            // TODO: 这行代码将数据加载到表“sTUDataSet5.Usertable”中。您可以根据需要移动或删除它。
            this.usertableTableAdapter.Fill(this.sTUDataSet5.Usertable);
            // TODO: 这行代码将数据加载到表“sTUDataSet4.Utable”中。您可以根据需要移动或删除它。
            this.utableTableAdapter.Fill(this.sTUDataSet4.Utable);

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("管理员邀请码:123");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form11 form11 = new Form11();       //实例化
            form11.ShowDialog();             //新打开窗体
            this.Close();                   //关闭当前窗体
        }
    }
}

 

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值