本週完成了對管理員操作的Student表、Course表、SC表的增刪改查操作,這些操作比上週更加完善了。
現在對Student表的操作沒有問題了。Course表和SC表還有一些問題需要解決。
1、增加或刪除時可以增刪多個成員信息,之前只能操作一個,必須重新啓動纔可以。
解決方法:將this.Dispose()改爲this.Close()。
Close後數據庫連接可以再次打開,被關閉的對象還可以再次使用;而Dispose後連接字符串被清空,連接不能再打開。
2、修改時對於Course表可以通過Cno一次修改一條記錄,增加了通過課程名一次修改多門相同類型的課程的信息。
3、查找時,不僅可以根據主碼查找,還增加了模糊查詢,可以通過任意一種方式查詢。
對Course和SC表還存在一些問題需要改進,在增加時輸入的信息不能爲空,否則會出現輸入的數據有誤;SC表的查詢不能只通過Sno和Cno查詢。
Couse表的代碼:
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 dazuoye2
{
public partial class Admin_Course : Form
{
public Admin_Course()
{
InitializeComponent();
}
private void Admin_Course_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數據加載到表“schoolDataSet1.Course”中。您可以根據需要移動或刪除它。
this.courseTableAdapter.Fill(this.schoolDataSet1.Course);
}
public void clear()
{
textBoxcno.Text = "";
textBoxcname.Text = "";
textBoxcpno.Text = "";
textBoxcredit.Text = "";
}
SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = School; Persist Security Info = True;User ID = sa; Password = 123"); //連接數據庫
//public string CouCcredit { get; private set; }
public string EMPTY_Cno(string obj)//輸入的數據Cpno爲空時的情況
{
if (obj == null)
return DBNull.Value.ToString();
return obj;
}
private void buttonadd_Click(object sender, EventArgs e)//增加
{
string CouCno = textBoxcno.Text.Trim();
string CouCname = textBoxcname.Text.Trim();
string CouCpno =EMPTY_Cno(textBoxcpno.Text);
string CouCcredit = textBoxcredit.Text.Trim();
try
{
con.Open(); //打開數據庫
string insertCou = "INSERT INTO Course(Cno,Cname,Cpno,Ccredit)" + "VALUES('" + CouCno + "','" + CouCname + "','" + CouCpno + "'," + CouCcredit+ ")";
SqlCommand cmd = new SqlCommand(insertCou, con);
cmd.ExecuteNonQuery(); //將增加後的信息直接出來
}
catch
{
MessageBox.Show("輸入數據違反要求!");
}
finally
{
con.Close();
}
this.courseTableAdapter.Fill(this.schoolDataSet1.Course);
}
private void buttonclose_Click(object sender, EventArgs e)
{
this.Close();
}
private void buttondelete_Click(object sender, EventArgs e)//刪除
{
try
{
con.Open(); //打開數據庫
string select_Cno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是Cno
string delete_by_Cno = "DELETE FROM Course WHERE Cno='" + select_Cno + "'";//sql刪除語句
SqlCommand cmd = new SqlCommand(delete_by_Cno, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("請選擇正確行!");
}
finally
{
// con.Dispose(); //關閉數據庫
con.Close();
}
this.courseTableAdapter.Fill(this.schoolDataSet1.Course);
}
private void buttonchange_Click(object sender, EventArgs e)//修改
{
string CouCno = textBoxcno.Text.Trim();
string CouCname = textBoxcname.Text.Trim();
string CouCpno = textBoxcpno.Text.Trim();
string CouCcredit = textBoxcredit.Text.Trim();
try
{
con.Open(); //打開數據庫
if (CouCname != "") //修改課程名稱,只能根據課程號修改
{
string update_cname = "UPDATE Course SET Cname='" + CouCname + "'WHERE Cno='" + CouCno + "'";
SqlCommand cmd = new SqlCommand(update_cname, con);
cmd.ExecuteNonQuery();
}
if ( CouCpno != "" &&CouCno != "" || CouCname != ""&& CouCpno != "") //修改Cpno,通過課程號修改一門課的或者通過課程名修改多門課的
{
string update_cpno = "UPDATE Course SET Cpno='" + CouCpno + "' WHERE Cno='" + CouCno + "' OR Cname='"+CouCname+"'";
SqlCommand cmd2 = new SqlCommand(update_cpno, con);
cmd2.ExecuteNonQuery();
}
if (CouCcredit != "" && CouCno != "" || CouCname != "" && CouCcredit != "")//修改Credit,通過課程號修改一門課的或者通過課程名修改多門課的
{
string update_credit = "UPDATE Course SET Ccredit='" + CouCcredit + " 'WHERE Cno='" + CouCno + "' OR Cname='"+CouCname+"'";
SqlCommand cmd3 = new SqlCommand(update_credit, con);
cmd3.ExecuteNonQuery();
}
/* if (CouCpno != ""&&CouCno!="" )//根據課程號修改Cpno
{
string update_cpno = "UPDATE Course SET Cpno='" + CouCpno + "' WHERE Cno='" + CouCno +"'";
SqlCommand cmd = new SqlCommand(update_cpno, con);
cmd.ExecuteNonQuery();
}
if (CouCpno != "" && CouCno == "" && CouCname != "")//根據課程名修改Cpno
{
string update_cpno = "UPDATE Course SET Cpno='" + CouCpno + "' WHERE Cname='" + CouCname + "'";
SqlCommand cmd = new SqlCommand(update_cpno, con);
cmd.ExecuteNonQuery();
}
if (CouCcredit != "" && CouCno != "" )//根據課程號修改Ccredit
{
string update_credit = "UPDATE Course SET Ccredit='" + CouCcredit+ "' WHERE Cno='" + CouCno + "'";
SqlCommand cmd = new SqlCommand(update_credit, con);
cmd.ExecuteNonQuery();
}
if (CouCcredit != "" && CouCname != "" && CouCno == "")//根據課程名修改Ccredit
{
string update_credit = "UPDATE Course SET Ccredit='" + CouCcredit + "' WHERE Cname='" + CouCname + "'";
SqlCommand cmd = new SqlCommand(update_credit, con);
cmd.ExecuteNonQuery();
}*/
}
catch
{
MessageBox.Show("輸入數據違反要求!");
}
finally
{
//con.Dispose(); //關閉數據庫
con.Close();
}
this.courseTableAdapter.Fill(this.schoolDataSet1.Course);
}
private void buttonselect_Click(object sender, EventArgs e)//查找
{
string CouCno = textBoxcno.Text.Trim();
string CouCname = textBoxcname.Text.Trim();
string CouCpno = textBoxcpno.Text.Trim();
string CouCcredit = textBoxcredit.Text.Trim();
String conn = "Data Source =.; Initial Catalog = School; Persist Security Info = True;User ID = sa; Password = 123";
SqlConnection sqlconnection = new SqlConnection(conn);//實例化連接對象
try
{
sqlconnection.Open();
if (CouCno != "")//按照課序號查找,只有一個
{
String select_by_cno = "select * from Course where Cno='" + CouCno + "'";
SqlCommand sqlcommand = new SqlCommand(select_by_cno, sqlconnection);
SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
BindingSource bindingsource = new BindingSource();
bindingsource.DataSource = sqldatareader;
dataGridView1.DataSource = bindingsource;
//將讀出來的值賦給數據源,再將數據源給dataGridView
}
if (CouCname != "" && CouCpno == "" && CouCcredit == "")//按照課程名查找
{
String select_by_cname = "select * from Course where Cname='" + CouCname + "'";
SqlCommand sqlcommand = new SqlCommand(select_by_cname, sqlconnection);
SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
BindingSource bindingsource = new BindingSource();
bindingsource.DataSource = sqldatareader;
dataGridView1.DataSource = bindingsource;
}
if (CouCpno != "" && CouCname == "" && CouCcredit == "")//按照Cpno查找
{
String select_by_cpno = "select * from Course where Cpno='" + CouCpno + "'";
SqlCommand sqlcommand = new SqlCommand(select_by_cpno, sqlconnection);
SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
BindingSource bindingsource = new BindingSource();
bindingsource.DataSource = sqldatareader;
dataGridView1.DataSource = bindingsource;
}
if (CouCcredit != "" && CouCname == "" && CouCpno == "")//按照Credit查找
{
String select_by_credit = "select * from Course where Ccredit='" + CouCcredit + "'";
SqlCommand sqlcommand = new SqlCommand(select_by_credit, sqlconnection);
SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
BindingSource bindingsource = new BindingSource();
bindingsource.DataSource = sqldatareader;
dataGridView1.DataSource = bindingsource;
}
if (CouCname != "" && CouCpno != "" && CouCcredit == "")//按照Cname和Cpno查找
{
String select_by_namepno = "select * from Course where Cname='" + CouCname + "' AND Cpno='"+CouCpno+"'";
SqlCommand sqlcommand = new SqlCommand(select_by_namepno, sqlconnection);
SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
BindingSource bindingsource = new BindingSource();
bindingsource.DataSource = sqldatareader;
dataGridView1.DataSource = bindingsource;
}
if (CouCname != "" && CouCcredit != "" && CouCpno == "")//按照Cname和Ccredit查找
{
String select_by_namecredit = "select * from Course where Cname='" + CouCname + "' AND Ccredit='" + CouCcredit + "'";
SqlCommand sqlcommand = new SqlCommand(select_by_namecredit, sqlconnection);
SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
BindingSource bindingsource = new BindingSource();
bindingsource.DataSource = sqldatareader;
dataGridView1.DataSource = bindingsource;
}
if (CouCpno != "" && CouCcredit != "" && CouCname == "")//按照Cpno和Ccredit查找
{
String select_by_pnocredit = "select * from Course where Cpno='" + CouCpno + "' AND Ccredit='" + CouCcredit + "'";
SqlCommand sqlcommand = new SqlCommand(select_by_pnocredit, sqlconnection);
SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
BindingSource bindingsource = new BindingSource();
bindingsource.DataSource = sqldatareader;
dataGridView1.DataSource = bindingsource;
}
if (CouCname != "" && CouCpno != "" && CouCcredit != "")//按照Cname和Cpno和Ccredit查找
{
String select_by_pnocreditname = "select * from Course where Cpno='" + CouCpno + "' AND Ccredit='" + CouCcredit + "' AND Cname='"+CouCname+"'";
SqlCommand sqlcommand = new SqlCommand(select_by_pnocreditname, sqlconnection);
SqlDataReader sqldatareader = sqlcommand.ExecuteReader();
BindingSource bindingsource = new BindingSource();
bindingsource.DataSource = sqldatareader;
dataGridView1.DataSource = bindingsource;
}
}
catch
{
MessageBox.Show("查詢語句有誤,請認真檢查SQL語句");
}
finally
{
sqlconnection.Close();
}
}
private void buttonempty_Click(object sender, EventArgs e)//清空
{
clear();
}
}
}
在寫代碼的過程中,一定要把表名和變量名寫正確,系統不會因爲表名錯誤給報錯,但是運行是錯誤的,我有的時候會因爲把名字拼錯了,把Student拼成了Studnet,找了半天才看見是名字寫錯了。