任務:增加增、刪、改、查的功能
目錄
增加組件、優化界面
通用代碼
SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******"); //新建連接
try
{
con.Open(); //打開
string ComStr = ———— //SQL語句
SqlCommand cmd = new SqlCommand(ComStr, con);
cmd.ExecuteNonQuery(); //返回執行命令後影響的參數
this.Close();
}
catch
{
MessageBox.Show("錯誤!請重新操作!");
}
finally
{
con.Close();
}
注:這裏出現的錯誤,點一下就解決了。
增
界面
新建一個form。
因爲性別、系別爲固定選項,則選擇ComboBox組件。
插眼:ComboBox使用
屬性中Items裏,寫入下拉框的內容,一行爲一項。
插眼:設置成只讀
只讀即爲只顯示,無法編輯。
將ComboBox設置成只讀。
屬性中,找到DropDownStyle,將其改爲DropDownList。
代碼
用try-catch防止出現問題。
新建一個SqlConnection類 con,連接SQL數據庫。內容與之前的相同。
SqlConnection con = new SqlConnection("Data Source =.; Initial Catalog = STU; Persist Security Info = True; User ID = sa; Password = ******");
添加語句:
string ComStr = "INSERT INTO Student(Sno, Sname, Ssex, Sage, Sdept)" + "VALUES ('" + StuNum + "','" + StuName + "','" + StuSex + "'," + StuAge + ",'" + StuDept + "')"; //SQL添加語句
調用參數:
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) //增
{
Form5 form5 = new Form5();
form5.Show();
}
結果
添加成功
Form中的結果。
SQL中的結果。
刪
插眼:(未解決)兩個FORM連接。————已經解決
原本打算這四個功能都與“增”一樣新建一個Form再操作,然後主界面的代碼只有打開Form,顯得主界面代碼整潔些。
但是這步出現了個問題,查了資料也沒能解決。
我做的刪除功能是通過點擊選擇想要刪除的行,暫時沒有輸入數據進行刪除的功能。但是連接數據庫的DataGirlView只在主界面,點擊選取表的功能也只能出現在主界面進行,在子界面沒辦法操作主界面。
所以刪除的代碼只好寫在了主界面的代碼裏。
刪除語句:
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); //讀取改行第一列的值,即Sno主碼
string delete_by_id = "delete from Student where Sno=" + select_id; //SQL刪除語句
注:此處select_id即爲讀取的Sno主碼,將其作爲參數,帶參函數傳至子窗口,即可在子窗口提取select_id使用。
主界面,帶參函數:
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); //讀取改行第一列的值,即Sno主碼
Form6 form6 = new Form6(select_id);
子界面:
public Form6()
{
InitializeComponent();
}
public Form6(string select_id) //參數
{
this.select_id = select_id;
InitializeComponent();
}
刪除代碼:
private void button3_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("刪除成功!請點擊“刷新”按鈕");
}
catch
{
MessageBox.Show("錯誤!請重新操作!");
}
finally
{
con.Close();
}
}
結果
刪除成功。
注:必須點擊錶行前面、選取全部改行,點擊其他位置會錯誤。
Form中的結果。
SQL中的結果。
改
思路:修改即爲刪除+添加,選中一行,將原數據刪除再在此行添加新數據。
代碼
讀取選取的該行的第一列,即學號。主界面代碼:
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
{
}
}
子窗口代碼:
實例化:
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();
}
結果
Form中的結果。
SQL中的結果。
查
查找的方法比較麻煩,我也沒有找到更好的方法。
出現問題和刪除的問題相似,都是現實結果必須在有DataGirdView的主界面寫代碼,才能現實SQL操作的結果。而填寫查找內容在子窗口,爲了將傳遞兩個窗口的內容,引入全局變量。
在子窗口存入查找的內容,在主界面進行SQL操作現實查找的結果。
public static string StuText;
public static string StuSex;
public static string Studept;
public static int flag = -1;
界面
新建一個查找方式界面,
代碼
利用Hide(); 和 Show(); 函數進行隱藏和顯示。
開始按鈕“確定”、textBox、和兩個comboBox都進行隱藏,點擊“”“選擇”按鈕後,顯示“確定”按鈕。
以下爲子窗口部分代碼:
public Form8()
{
InitializeComponent();
textBox1.Hide();
comboBox2.Hide();
comboBox3.Hide();
button3.Hide();
}
按鈕“選擇”,更改flag,以進行if選擇操作。
選擇“學號”、“姓名”、“年齡”的時候,顯示textBox。
選擇“性別”、“系別”的時候,顯示comboBox。
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();
}
以下爲主界面部分代碼:
實例化。
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();
}
}
結果
點擊“查找方式”,只顯示選擇方式,隱藏“確定”按鈕。
選擇後顯示“確定”按鈕,以及textBox和comboBox。
點擊“確定”後退出,點擊“查詢”按鈕。
顯示成功。
取消
所有的取消按鈕。
private void buttons2_Click(object sender, EventArgs e)
{
this.Close();
}
}
刷新
因爲DataGirlView顯示的表進行操作後無法立即顯示,所以增加了一個“刷新”按鈕。
主界面的這段代碼,即爲刷新的代碼。
this.studentTableAdapter.Fill(this.sTUDataSet3.Student);
MessageBox.Show("刷新");
再加上查找功能後,表中只顯示查找後的結果,爲了刷新表,增加以下代碼,將其填到“刷新“”按鈕中:
即SELECT查找全部Student表。
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();
}