我終于把我的界面變得沉魚落雁,傾國傾城…了!!!
上圖!!!
界面設(shè)計
登錄界面:
導(dǎo)航界面:
插入界面:
刪除界面:
查詢界面:
修改界面:
可能它還不是那么完美,之后邊做再邊完善吧~
功能以及相應(yīng)代碼實(shí)現(xiàn)
以下
1.Clear ALL按鈕,用于清除所有填寫的東西(因?yàn)槲野l(fā)現(xiàn)測試的時候一直寫了刪,刪了寫,怪麻煩的,主要還是我懶~)
實(shí)現(xiàn)代碼:
private void buttonclear_Click(object sender, EventArgs e)
{
textBox_sno.Text = "";
textBox_sname.Text = "";
textBox_ssex.Text = "";
textBox_sdept.Text = "";
textBox_sage.Text = "";
checkBox_sage.Checked = false;
checkBox_sdept.Checked = false;
checkBox_ssex.Checked = false;
checkBox_sname.Checked = false;
//清除輸入內(nèi)容和復(fù)選框的狀態(tài)
}
2.BACK按鈕,用于回到導(dǎo)航頁面,不是用于回到之前的數(shù)據(jù)狀態(tài)的。
實(shí)現(xiàn)代碼:(其實(shí)就是切換以下界面)
private void button_BACK_Click(object sender, EventArgs e)
{
FormStu formstu = new FormStu();
formstu.Show();
this.Hide();
}
1.插入數(shù)據(jù)
實(shí)現(xiàn)代碼:
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=school;Persist Security Info=True;User ID=sa;Password=");
private void button_insert_Click(object sender, EventArgs e)
{
String StuID = textBox_sno.Text.Trim();
String StuName = textBox_sname.Text.Trim();
String StuSex = textBox_ssex.Text.Trim();
String StuSdept = textBox_sdept.Text.Trim();
String StuAge = textBox_sage.Text.Trim();//取出字符串
try
{
con.Open();
String insertstr = "INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) " +"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuSdept + "'," + StuAge + ")";
SqlCommand cmd = new SqlCommand(insertstr, con); //實(shí)例化數(shù)據(jù)庫命令對象
cmd.ExecuteNonQuery(); //執(zhí)行命令
}
catch
{
MessageBox.Show("輸入數(shù)據(jù)有誤,請輸入有效數(shù)據(jù)!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Dispose(); //釋放
}
this.studentTableAdapter.Fill(this.schoolDataSet.Student);//使其顯示更新后的表
}
輸入插入數(shù)據(jù)
插入:
再次插入會出錯:
2.刪除數(shù)據(jù)
實(shí)現(xiàn)代碼:
private void button_delete_Click(object sender, EventArgs e)
{
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當(dāng)前行第一列的值,也就是ID
string delete_by_id = "delete from Student where Sno=https:www.xiaoyuani.com" + "'"+select_id+"'";//sql刪除語句,根據(jù)學(xué)號刪除
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery(); //執(zhí)行命令
}
catch
{
MessageBox.Show("請正確選擇行!","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.schoolDataSet.Student);//使其顯示更新后的表
}
未選中會彈錯誤對話框:
選中行,并刪除:
刪除后:
3.修改數(shù)據(jù)
用法:輸入要修改的學(xué)生信息的學(xué)號,選擇要修改的項(xiàng),并輸入修改后的數(shù)據(jù),可以多選。
(這個“李小勇”是之前測試的時候改的,后來就忘記改回來了,感覺還不錯~)
實(shí)現(xiàn)代碼:
private void button_update_Click(object sender, EventArgs e)
{
int flag1 = 0, flag2 = 0;
try
{
con.Open();//打開數(shù)據(jù)庫
string updatestr = "UPDATE Student SET ";
String StuID = textBox_sno.Text.Trim();
String StuSdept = textBox_sdept.Text.Trim();
String StuAge = textBox_sage.Text.Trim();//取出字符串
String StuName = textBox_sname.Text.Trim(https:www.xiaoyuani.com);
String StuSex = textBox_ssex.Text.Trim();
if (checkBox_sdept.Checked == true)
{
if (flag1 == 0)
{
updatestr += "Sdept = '" + StuSdept + "'";
flag1 = 1;
}
else
updatestr += ", Sdept = '" + StuSdept + "'";
}
if (checkBox_sage.Checked == true)
{
if (flag1 == 0)
{
updatestr += "Sage = " + StuAge ;
flag1 = 1;
}
else
updatestr += ", Sage = " + StuAge ;
}
if (checkBox_sname.Checked == true)
{
if (flag1 == 0)
{
updatestr += "Sname = '" + StuName + "'";
flag1 = 1;
}
else
updatestr += ", Sname = '" + StuName + "'";
}
if (checkBox_ssex.Checked == true)
{
if (flag1 == 0)
{
updatestr += "Ssex = '" + StuSex + "'";
flag1 = 1;
}
else
updatestr += ", Ssex = '" + StuSex + "'";
}//被選中的項(xiàng)取出相應(yīng)字符串
updatestr += " WHERE Sno = '" + StuID + "https:www.xiaoyuani.com'";
SqlCommand cmd = new SqlCommand(updatestr, con);
cmd.ExecuteNonQuery();
}
catch
{
flag2 = 1;
MessageBox.Show("輸入數(shù)據(jù)違反要求!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();//斷開數(shù)據(jù)庫連接
}
this.studentTableAdapter.Fill(this.schoolDataSet.Student);
if (flag2 == 0)
{
MessageBox.Show("修改成功!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
原始:
修改:
輸入錯誤的數(shù)據(jù):(性別只能輸入 男/女)
4.查詢數(shù)據(jù)
用法:和修改的類似,選擇查詢的項(xiàng),并輸入,會根據(jù)所輸入的查詢。
實(shí)現(xiàn)代碼:
private void button_select_Click(object sender, EventArgs e)
{
String StuID = textBox_sno.Text.Trim(https:www.xiaoyuani.com);
String StuName = textBox_sname.Text.Trim();
String StuSex = textBox_ssex.Text.Trim();
String StuSdept = textBox_sdept.Text.Trim();
String StuAge = textBox_sage.Text.Trim();//取出字符串
try
{
con.Open();
String select_by_id = "select * from Student where ";
int flag1 = 0; //表示前面是否已經(jīng)加了篩選條件,為1,則后面的條件需要加AND
if (checkBox_sno.Checked == true)
{
if(flag1 == 0)
{
select_by_id += "Sno='" + StuID + "'";
flag1 = 1;
}
else
{
select_by_id += "AND Sno='" + StuID + "'";
}
}
if(checkBox_sname.Checked == true)
{
if (flag1 == 0)
{
select_by_id += "Sname='" + StuName + "'";
flag1 = 1;
}
else
{
select_by_id += "AND Sname='" + StuName + "'";
}
}
if(checkBox_ssex.Checked == true)
{
if (flag1 == 0)
{
select_by_id += "Ssex='" + StuSex + "'";
flag1 = 1;
}
else
{
select_by_id += "AND Ssex='" + StuSex + "'";
}
}
if(checkBox_sdept.Checked == true)
{
if (flag1 == 0)
{
select_by_id += "Sdept='" + StuSdept + "'";
flag1 = 1;
}
else
{
select_by_id += "AND Sdept='" + StuSdept + "'";
}
}
if(checkBox_sage.Checked == true)
{
if (flag1 == 0)
{
select_by_id += "Sage=" + StuAge ;
flag1 = 1;
}
else
{
select_by_id += "AND Sage=" + StuAge;
}
}
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("查詢語句有誤,請認(rèn)真檢查SQL語句!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();Show ALL 按鈕:顯示Student表中的所有數(shù)據(jù)(我發(fā)現(xiàn)查詢了之后,回不到之前的表,怪怪的~,所以加了它)
實(shí)現(xiàn)代碼:
private void buttonall_Click(object sender, EventArgs e)
{
try
{
con.Open();
String select_by_id = "select * from Student";
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(https:www.xiaoyuani.com);
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查詢語句有誤,請認(rèn)真檢查SQL語句!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();單個查詢:
(下面這兩張的是在比較早時間截的,所以“李勇”還沒有變成“李小勇”。。。)
多個條件查詢:
關(guān)于數(shù)據(jù)庫
1.關(guān)于插入,修改的數(shù)據(jù)是否合理,我是在數(shù)據(jù)庫的表上加了約束條件實(shí)現(xiàn)的,比在Visual Studio中檢查數(shù)據(jù)要方便。
2.數(shù)據(jù)庫會和這里有一致的改變:
遇到的問題及解決方法
這次實(shí)驗(yàn)遇到了很多問題,所以時間用的比較長,交進(jìn)度的時間也拖后了。
尤其是在UPDATE和SELECT的地方,這里我想得太美好了,以致做起來還比較費(fèi)力,真是自己挖的坑自己跳,但好在我最后解決了!奧利給!!!
這里把問題列出來,防止以后自己再次踩坑,也給各位小伙伴做個小小參考。
Q1.修改的時候需要把不用修改的值也輸入很是麻煩。
Solution:加入復(fù)選框,修改哪項(xiàng)就選擇哪項(xiàng)
(查找那里也類似,選擇查詢的依據(jù)條件)
Q2.修改處代碼實(shí)現(xiàn),要判斷復(fù)選框的狀態(tài)然后更改相應(yīng)的SQL語句,四個的排列組合太多種,用if語句實(shí)現(xiàn)太困難。
Solution:引入標(biāo)志變量flag1,flag1 = 0表示其前面沒有條件,不需要加AND;flag1 = 1表示其前面有其他條件,要在SQL語句后加AND連接。(emmm…說著有些難理解,還是放代碼)
例:
String select_by_id = "select * from Student where ";
int flag1 = 0; //表示前面是否已經(jīng)加了篩選條件,為1,則后面的條件需要加AND
if (checkBox_sno.Checked == true)
{
if(flag1 == 0)
{
select_by_id += "Sno='" + StuID + "'";
flag1 = 1;//置為1,表示有條件
}
else
{
select_by_id += "AND Sno='" + StuID + "'";
}
}
//后面其他復(fù)選框的處理一致,省略。
(查詢和這處也類似,設(shè)置flag2,判斷是否選擇,連接后面的字符串)
Q3.修改的界面,發(fā)現(xiàn)只能修改一次,而后修改就會出現(xiàn)異常
Solution:調(diào)試后發(fā)現(xiàn)是打開數(shù)據(jù)庫處的錯誤
發(fā)現(xiàn)這里用的是con.Dispose(),百度后了解:
con.Dispose()是釋放內(nèi)存里的con,并沒有斷開連接
con.Close()才是斷開連接
用con.Dispose()之后第二次就不能再打開連接了,會出錯誤,我覺得可能是這樣。所以我改成con.close()之后就對了。
上面的解決方法是我能想到的唯一的方法了,可能不是最簡單的,好像也沒什么技術(shù)含量,但起碼它能解決一下。以后想到更好的,會繼續(xù)優(yōu)化的。
(調(diào)界面的色彩搭配,也用了不少時間,這里英文字體很好看,所以很多我都用的英文的)
【小筆記】使顯示查詢結(jié)果的語句:
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
就是設(shè)置dataGridView的數(shù)據(jù)源,但不能直接將sqlDataReader賦給dataGridView1.DataSource,中間用bindingSource傳遞一下。
【心得】這次的實(shí)驗(yàn)很是讓人欣慰啊~看上去就賞心悅目。有做得更好的想法,然后去想辦法實(shí)現(xiàn),才能真的做得更好。這次用時沒有很集中,從上完實(shí)驗(yàn)課之后幾乎每天都有在摳它。
它還是有些缺陷:刪除那里,發(fā)現(xiàn)如果不選擇行就刪除彈出錯誤之后,再去選擇行去刪除還是會彈出錯誤,需要完善。其他地方可能還是有缺陷,以后做的時候發(fā)現(xiàn)問題再完善。
之前別的學(xué)科的大實(shí)驗(yàn),都是當(dāng)做任務(wù),作業(yè)來做,這次做大實(shí)驗(yàn)真的體會到了一種設(shè)計的感覺~繼續(xù)加油。