[C#:学生情報管理システム-部品コード1]

プロジェクトのWindowsフォームアプリケーションを作成します。

DBhelp.csクラスを作成し、データベースに接続し、毎回このクラスを呼び出すだけです。これを呼び出す:DBhelp.conn.Open();

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Win9_28
{
    public static class DBhelp
    {
        static string  strconn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
        public static SqlConnection conn = new SqlConnection(strconn);
        public static SqlCommand comm = new SqlCommand();
        //public static SqlDataReader read = comm.ExecuteReader();
    }
}

1.アイテムをクリックして、アイテムを追加します:Windowsフォーム、ログインフォームを記述します:Login.cs:

最初に:ログインするフォームのTestプロパティを変更し、(Name)プロパティをfrmLoginに変更します

画像コントロールの追加PictureBoxコントロール、(名前)名前:pictureBox1、Imageプロパティ:画像のインポート、SizeModeプロパティ:StretchImageを使用して、画像全体を表示します

次の名前のラベルコントロール(名前)を追加します:lblUserName、テキスト:ユーザー名、入力ボックスコントロールTextBox、(名前):txtUserName

lblUserPwd、Text:password、input box control TextBox、(Name):txtUserPwdという名前のラベルコントロール(Name)を追加します。パスワードを表示しない場合は、プロパティPasswordChar:*を設定します。

2つのボタンコントロールを追加します。前者(名前):btnRegisterテキスト:ログイン、後者(名前):btnESCテキスト:キャンセル

スタイルは次のとおりです。

ログインボタンの方法:ログインボタンをダブルクリックします。ログインボタンイベントはバックグラウンドで自動的に生成されます。

private void btnRegister_Click(object sender, EventArgs e)
        {
            string strconn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
            SqlConnection conn = new SqlConnection(strconn);//创建SqlConnection对象,连接数据库
            string name = txtUserName.Text;//获取姓名文本框的内容
            string pwd = txtUserPwd.Text;//获取密码文本框的内容
            try
            {
                conn.Open();
                string sqlstr=string.Format("select * from tb_User where UserName='{0}' and UserPasswd='{1}'",name,pwd);
                SqlCommand comm = new SqlCommand();//创建sqlCommand对象,执行SQL语句
                comm.Connection = conn;//执行SqlCommand对象的Connection属性,设置Command对象的Connection对象
                comm.CommandText = sqlstr;//执行SqlCommand对象的的CommandText属性,设置Command对象的sql语句

                /*
                if (name == string.Empty || pwd == string.Empty)
                {
                    MessageBox.Show("用户名或密码不能为空", "系统提示");
                }*/


                if (comm.ExecuteScalar() == null)//ExecuteScalar()返回查询结果集中的第一行第一列,没有返回null
                {
                    MessageBox.Show("用户名与密码不匹配,登录失败");//提示信息
                    this.txtUserName.Clear();
                    this.txtUserPwd.Clear();
                    this.txtUserName.Focus();

                }
                else 
                {
                    //this.Hide();//此页面隐藏
                    frmMain frmmain = new frmMain();//创建新的页面
                    frmmain.Show();//打开新的页面
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
             conn.Close();
            }
        }

ボタンをキャンセルする方法は、キャンセルボタンをダブルクリックします。

private void btnESC_Click(object sender, EventArgs e)
        {
            Application.Exit();//退出程序,结束运行
        }

2.大学のページを追加します:frmAddCollege.cs

 フォームのTextプロパティを変更します:部門情報を追加します、(名前):プロパティ:frmAddCollege

2つのLableコントロールを追加し、2つのテキスト入力ボックスコントロールTextBoxを追加します。名前はtxtDepartmentID、txtDepartmentName、2つのボタンコントロールを追加します。ボタンとコマンド、必要なテキスト値を変更します。

[追加]ボタンをダブルクリックして、[追加]ボタンのOnClickイベントを追加します。コードは次のとおりです。

private void btnAdd_Click(object sender, EventArgs e)
        {
            string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
            SqlConnection a = new SqlConnection(conn);//创建Connection对象

            if (txtDepartmentID.Text == "" || txtDepartmentName.Text == "")
            {
                MessageBox.Show("院系编号或院系名称不能为空!");
            }
            else
            {
                try
                {
                    a.Open();
                    string sqlstr = string.Format("insert into tb_College values('{0}','{1}')", txtDepartmentID.Text, txtDepartmentName.Text);

                    SqlCommand comm = new SqlCommand();//创建Command 对象执行SQL语句
                    comm.Connection = a;
                    comm.CommandText = sqlstr;
                    //int result = comm.ExecuteNonQuery();

                    if (comm.ExecuteNonQuery() > 0)
                    {
                        MessageBox.Show("插入成功");
                    }
                    else
                    {
                        MessageBox.Show("插入失败");
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    a.Close();
                }
            }
        }

このページを閉じるには、キャンセルボタンのクリックイベントを追加します。

private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

2.frmEditCollege.csフォームを作成します

 

 フォーム(名前)を次のように変更します:frmEditCollege、Text:Faculty Information System

データコントロールを追加します:dataGridViewコントロール、上の小さな三角形をクリックし、データソースを追加します-データソースを選択します-データソースを追加します-データベース-データセット-内部のテーブル、データソースを追加します

2つのラベルコントロールを追加する場合、テキストには次の名前が付けられます:部門番号:、部門名、2つのテキストボックスを追加し、4つのボタンを追加します

ストアデータソースのデータを下部のテキストボックスに表示する場合は、データコントロールを二重に追加し、次のコードを追加します。 

​​ private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtCollegeNo.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtCollegeName.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
        }

クエリボタンの場合、クリックイベントを追加します。コードは次のとおりです。

private void btnSelect_Click(object sender, EventArgs e)
        {
            string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
            SqlConnection a = new SqlConnection(conn);

            try
            {
                a.Open();
                string sqlstr = string.Format("select * from tb_College where DepartmentID='{0}'", txtCollegeNo.Text);//Format转化为字符串
                SqlCommand comm = new SqlCommand();
                comm.Connection = a;
                comm.CommandText = sqlstr;

                SqlDataReader read = comm.ExecuteReader();//创建DataReader对象,在数据库中读取数据
                if (read.Read())
                {
                    txtCollegeName.Text = read[1].ToString();
                }
                else
                {
                    MessageBox.Show("查询结果不存在!");
                    txtCollegeName.Text = "";
                }


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                a.Close();
            }
        }

削除ボタンをダブルクリックして、クリックイベントを追加します。

private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DBhelp.conn.Open();
                string sqlstr = string.Format("delete from tb_College where DepartmentID='{0}'and DepartmentName='{1}' ", txtCollegeNo.Text, txtCollegeName.Text);
                DBhelp.comm.CommandText = sqlstr;
                DBhelp.comm.Connection = DBhelp.conn;

                if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("删除成功");

                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBhelp.conn.Close();
            }
        }

[変更]ボタンをダブルクリックして、クリックイベントを追加します。

 private void btnCollege_Click(object sender, EventArgs e)
        {
            /*string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
            SqlConnection a = new SqlConnection(conn);

            if (txtCollegeNo.Text == "" || txtCollegeName.Text == "")
            {
                MessageBox.Show("院系编号或院系名称不能为空!");
            }
            else
            {
                try
                {
                    a.Open();
                    string sqlstr = string.Format("insert into tb_College values('{0}','{1}')", txtCollegeNo.Text, txtCollegeName.Text);

                    SqlCommand comm = new SqlCommand();
                    comm.Connection = a;
                    comm.CommandText = sqlstr;
                    int result = comm.ExecuteNonQuery();

                    if (result != 0)
                    {
                        MessageBox.Show("插入成功");
                    }
                    else
                    {
                        MessageBox.Show("插入失败");
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    a.Close();
                }
            }*/
            //上面代码无用
            try
            {
                DBhelp.conn.Open();
                string sqlstr = string.Format("update tb_College set DepartmentName='{0}' where DepartmentID='{1}'",txtCollegeName.Text,txtCollegeNo.Text);
                DBhelp.comm.CommandText = sqlstr;
                DBhelp.comm.Connection = DBhelp.conn;

                if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("修改成功");

                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBhelp.conn.Close();
            }
        }

[キャンセル]ボタンをクリックして、クリックイベントを追加します。

 private void btnCollegeClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

設定ページが最初に部門IDを選択したときに、frmCollege_Loadコードをロードするようにページを設定する必要があります。

private void frmCollege_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“studentInfDataSet3.tb_College”中。您可以根据需要移动或删除它。
            this.tb_CollegeTableAdapter.Fill(this.studentInfDataSet3.tb_College);
            string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
            SqlConnection a = new SqlConnection(conn);

            try
            {
                a.Open();
                string sqlstr = string.Format("select * from tb_College where DepartmentName='医学院'");
                SqlCommand comm = new SqlCommand();
                comm.Connection = a;
                comm.CommandText = sqlstr;

                SqlDataReader read = comm.ExecuteReader();
                if (read.Read())
                {
                    txtCollegeNo.Text = read[0].ToString();
                }

                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                a.Close();
            }



            try
            {
                DBhelp.conn.Open();
                string sqlstr = "select * from tb_College";
                SqlDataAdapter datadapter = new SqlDataAdapter(sqlstr, DBhelp.conn);
                DataSet set = new System.Data.DataSet();
                datadapter.Fill(set);
                dataGridView1.DataSource = set.Tables[0];

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBhelp.conn.Close();
            }
        }

3.学生の追加ページfrmAddStudent.csを作成します。

 学生番号という名前のラベルを追加し、入力ボックスTextBoxコントロール名前ラベル入力ボックスTextBox、Panelコンテナを追加し、男性と女性という名前の2つのラジオボタンコントロールRadioButtonを配置します 

生年月日に名前を付けるLabelコントロールと、入力ボックスのTextBoxコントロールを追加します。

classという名前のLabelコントロールを追加し、ドロップダウンボックスコントロールComboBoxコントロールを追加し、ComboBoxコントロールの上にある小さな三角形をクリックし、データバインディングアイテムを使用して選択し、データベース内のテーブルをバインドします。

..。 

 2つのボタンを追加する名前の付いたボタン、追加、終了

[追加]ボタンをダブルクリックして、クリックイベントを追加します。

 private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DBhelp.conn.Open();
                string sqlstr = string.Format("insert into tb_Student values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", txtstudentID.Text, txtStudentName.Text, radmale.Checked ? radFamale.Text : radmale.Text, txtBirthday.Text, cmbClassID.Text, txtMobilePhone.Text, txtAddress.Text);
                DBhelp.comm.CommandText = sqlstr;
                DBhelp.comm.Connection = DBhelp.conn;

                if ((int)DBhelp.comm.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("插入成功");

                }
                else
                {
                    MessageBox.Show("插入失败!");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBhelp.conn.Close();
            }
        }

終了ボタンをダブルクリックして、クリックイベントを追加します。

 private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

フォローアップコードを参照してください:学生情報管理システム-セクションコード2

 

おすすめ

転載: blog.csdn.net/dengfengling999/article/details/123717619