【數據庫實驗】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();                   //關閉當前窗體
        }
    }
}

 

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