Create project Windows Windows Forms Application :

Create a DBhelp.cs class , Connect database , Just call this class every time : Call like this :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. Click item to add item :Windows forms , Write login form :Login.cs:

first : Modify the of the form Test Attribute is login ,(Name) The attribute is named frmLogin

Add a picture control PictureBox control ,(Name) Named :pictureBox1,
Image attribute : import picture ,SizeMode attribute :StretchImage To display the whole picture

Add a Label control (Name) name :lblUserName,Text: user name , Input box control TextBox,(Name):txtUserName

Add a Label control (Name) name :lblUserPwd ,Text: password
, Input box control TextBox,(Name):txtUserPwd, If you want the password not to be displayed, set the properties PasswordChar:*

add to 2 individual Button control , previous (Name):btnRegister Text: Sign in , Last one (Name):btnESC Text: cancel

The style is as follows :

Method in login button : Double click the login button : An event of login button will be automatically generated in the background qu
private void btnRegister_Click(object sender, EventArgs e) { string strconn =
"Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
SqlConnection conn = new SqlConnection(strconn);// establish SqlConnection object , Connect database
string name = txtUserName.Text;// Gets the contents of the name text box string pwd =
txtUserPwd.Text;// Gets the contents of the password text box try { conn.Open(); string
sqlstr=string.Format("select * from tb_User where UserName='{0}' and
UserPasswd='{1}'",name,pwd); SqlCommand comm = new
SqlCommand();// establish sqlCommand object , implement SQL sentence comm.Connection =
conn;// implement SqlCommand Object Connection attribute , set up Command Object Connection object comm.CommandText =
sqlstr;// implement SqlCommand Object CommandText attribute , set up Command Object sql sentence /* if (name ==
string.Empty || pwd == string.Empty) { MessageBox.Show(" User name or password cannot be empty ", " System prompt ");
}*/ if (comm.ExecuteScalar() == null)//ExecuteScalar() Returns the first row and the first column in the query result set , No return null
{ MessageBox.Show(" User name and password do not match , Login failed ");// Prompt information this.txtUserName.Clear();
this.txtUserPwd.Clear(); this.txtUserName.Focus(); } else {
//this.Hide();// This page is hidden frmMain frmmain = new frmMain();// Create a new page
frmmain.Show();// Open a new page } } catch (Exception ex) { MessageBox.Show(ex.Message);
} finally { conn.Close(); } }
Method of canceling button , Double click the Cancel button :
private void btnESC_Click(object sender, EventArgs e) {
Application.Exit();// Exit program , End operation }
2. Add college page :frmAddCollege.cs

  Change the of this form Text attribute : Add department information ,(Name): attribute :frmAddCollege

Add two Lable control , Add two text entry box controls TextBox, Named :txtDepartmentID,txtDepartmentName, Add two button controls Button 
And give them commands , This is required for changes Text value

Double click the Add button , Add for add button OnClick Click event , The code is as follows :
private void btnAdd_Click(object sender, EventArgs e) { string conn = "Data
Source=.;Initial Catalog=studentInf;Integrated Security=True"; SqlConnection a
= new SqlConnection(conn);// establish Connection object if (txtDepartmentID.Text == "" ||
txtDepartmentName.Text == "") { MessageBox.Show(" Department number or department name cannot be empty !"); } else { try
{ a.Open(); string sqlstr = string.Format("insert into tb_College
values('{0}','{1}')", txtDepartmentID.Text, txtDepartmentName.Text); SqlCommand
comm = new SqlCommand();// establish Command Object execution SQL sentence comm.Connection = a;
comm.CommandText = sqlstr; //int result = comm.ExecuteNonQuery(); if
(comm.ExecuteNonQuery() > 0) { MessageBox.Show(" Insert successful "); } else {
MessageBox.Show(" Insert failed "); } } catch (Exception ex) {
MessageBox.Show(ex.Message); } finally { a.Close(); } } }
Add click event for cancel button , Close this page :
private void btnClose_Click(object sender, EventArgs e) { this.Hide(); }
2. establish frmEditCollege.cs forms

 

  Modify form (Name) by :frmEditCollege,Text: Department Information System

Add a data control :dataGridView control , Click the small triangle at the top , add data source - Select data source - add data source - database - data set - Watch inside , The data source is added

Add two in Lable control ,Text Named : Department number :, Department name , Add two text boxes , add to 4 Buttons

When you want the data of the store data source to be displayed in the lower text box , Double plus data control , Add the following code : 
​​ 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(); }
For query button , Add click event , The code is as follows :
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 Convert to string SqlCommand comm = new SqlCommand();
comm.Connection = a; comm.CommandText = sqlstr; SqlDataReader read =
comm.ExecuteReader();// establish DataReader object , Read data in database if (read.Read()) {
txtCollegeName.Text = read[1].ToString(); } else { MessageBox.Show(" Query result does not exist !");
txtCollegeName.Text = ""; } } catch (Exception ex) {
MessageBox.Show(ex.Message); } finally { a.Close(); } }
Double click the delete button , Add click event :
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(" Deleted successfully "); } else { MessageBox.Show(" Delete failed "); } } catch (Exception
ex) { MessageBox.Show(ex.Message); } finally { DBhelp.conn.Close(); } }
Double click the Modify button , Add click event :
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(" Department number or department name cannot be empty !"); } 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(" Insert successful "); } else {
MessageBox.Show(" Insert failed "); } } catch (Exception ex) {
MessageBox.Show(ex.Message); } finally { a.Close(); } }*/ // The above code is useless 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(" Modified successfully "); } else {
MessageBox.Show(" Modification failed "); } } catch (Exception ex) {
MessageBox.Show(ex.Message); } finally { DBhelp.conn.Close(); } }
Click the Cancel button , Add click event :
private void btnCollegeClose_Click(object sender, EventArgs e) { this.Hide();
}
Select the Department at the beginning of the setting page ID, Page loading needs to be set frmCollege_Load code :
private void frmCollege_Load(object sender, EventArgs e) { // TODO:
This line of code loads the data into the table “studentInfDataSet3.tb_College” in . You can move or delete it as needed .
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=' medical college '");
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. Create add student page ,frmAddStudent.cs:

  add to Lable Named student number , Input box TextBox control   full name Label
Input box TextBox, Add a Panel Two radio button controls are placed in the container RadioButton Named male   , female  

add to Label Control named birth date , Input box TextBox control

add to Label Control naming : class , Add a drop-down box control ComboBox control , click ComboBox Small triangle above control , Choose to use data bound items , Bind tables in the database

... 

  Add two buttons Button name , add to , sign out

Double click the Add button , Add click event :
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(" Insert successful "); } else {
MessageBox.Show(" Insert failed !"); } } catch (Exception ex) {
MessageBox.Show(ex.Message); } finally { DBhelp.conn.Close(); } }
Double click the exit button , Add click event :
private void btnClose_Click(object sender, EventArgs e) { this.Hide(); }

For subsequent codes, see : Student information management system - Partial code 2

 

Technology
Daily Recommendation
views 267
views 232
views 231