form1 登录界面
form2 功能选择界面
form3 增加信息界面
form4 删除信息界面
form5 修改信息界面
form6 查询信息界面
从之前的登录界面登录成功后进入功能选择界面
代码
private void button2_Click(object sender, EventArgs e)
{
Form4 form4 = new Form4();
form4.Show();
this.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
this.Hide();
}
private void guanbi_Click(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
Form5 form5 = new Form5();
form5.Show();
this.Hide();
}
private void button4_Click(object sender, EventArgs e)
{
Form6 form6 = new Form6();
form6.Show();
this.Hide();
}
增加信息界面
代码:
private void button1_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuAge = textBox4.Text.Trim();
String StuSdept = textBox5.Text.Trim(); //读取需要插入的值
SqlConnection con = new SqlConnection("Data Source=LAPTOP-93V8OVPD\\MSSQLSERVER01;Initial Catalog=Fwork;User ID=sa;Password=155872");
try
{
con.Open();
string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sage,Sdept) " + //拼接字符串
"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "'," + StuAge + ",'" + StuSdept + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Close();
}
this.studentTableAdapter.Fill(this.fworkDataSet5.Student);
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
private void button2_Click(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void Form3_Load_1(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“fworkDataSet5.Student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.fworkDataSet5.Student);
}
删除信息界面
private void Form4_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“fworkDataSet7.Student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.fworkDataSet7.Student);
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=LAPTOP-93V8OVPD\\MSSQLSERVER01;Initial Catalog=Fwork;User ID=sa;Password=155872");
try
{
con.Open(); //打开
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
string delete_by_id = "delete from Student where Sno=" + select_id;//sql删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con); //使用
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请正确选择行!");
}
finally
{
con.Dispose(); //释放
}
this.studentTableAdapter.Fill(this.fworkDataSet7.Student);
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
}
修改信息界面
private void Form5_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“fworkDataSet7.Student”中。您可以根据需要移动或删除它。
this.studentTableAdapter1.Fill(this.fworkDataSet7.Student);
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
private void guanbi_Click(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuAge = textBox4.Text.Trim();
String StuSdept = textBox5.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=LAPTOP-93V8OVPD\\MSSQLSERVER01;Initial Catalog=Fwork;User ID=sa;Password=155872");
try
{
con.Open();
if (StuName != "")
{
string insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
if(StuSex !="")
{
string insertStr = "UPDATE Student SET Ssex = '" + StuSex + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
if(StuAge !="")
{
string insertStr = "UPDATE Student SET Sage = '" + StuAge + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
if(StuSdept !="")
{
string insertStr = "UPDATE Student SET Sdept = '" + StuSdept + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Close();
}
this.studentTableAdapter1.Fill(this.fworkDataSet7.Student);
}
查找信息
private void Form6_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“fworkDataSet7.Student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.fworkDataSet7.Student);
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
private void guanbi_Click(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuAge = textBox4.Text.Trim();
String StuSdept = textBox5.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=LAPTOP-93V8OVPD\\MSSQLSERVER01;Initial Catalog=Fwork;User ID=sa;Password=155872");
try
{
String select_by_id = "select * from Student where ";
int flag = 0; //0是单条件,1是多条件
con.Open();
if (StuID != "")
select_by_id += "Sno='" + StuID + "'"; //单条件 按学号
if (StuName != "")
{
if (flag == 0)
{
select_by_id += "Sname='" + StuName + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Sname='" + StuName + "'";
}
if (StuAge != "")
{
if (flag == 0)
{
select_by_id += "Sage='" + StuAge + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Sage='" + StuAge + "'";
}
if (StuSdept != "")
{
if (flag == 0)
{
select_by_id += "Sdept='" + StuSdept + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Sdept='" + StuSdept + "'";
}
if (StuSex != "")
{
if (flag == 0)
{
select_by_id += "Ssex='" + StuSex + "'";
flag = 1;
}
if (flag == 1)
select_by_id += "And Ssex='" + StuSex + "'";
}
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("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
}
private void button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=LAPTOP-93V8OVPD\\MSSQLSERVER01;Initial Catalog=Fwork;User ID=sa;Password=155872");
con.Open();
String select_by_id = "select * from Student ";
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
con.Close();
}
因为有老师的示范,功能实现没那么困难,就是偶尔有些小问题。功能基本没问题,以上为管理员的界面、