C# 数据库dataGridView刷新数据和主外键判断
本文主要講訴在使用VS2012+SQL Server數(shù)據(jù)庫(kù)做系統(tǒng)中,通常會(huì)遇到幾個(gè)問(wèn)題.使用dataGridView控件在修改、刪除、插入數(shù)據(jù)后,怎樣刷新數(shù)據(jù)顯示操作后的結(jié)果.同時(shí)在對(duì)數(shù)據(jù)操作時(shí)通常會(huì)判斷數(shù)據(jù)的主鍵是否存在或重復(fù),判斷外鍵是否重復(fù),這幾個(gè)問(wèn)題我推薦使用函數(shù)的形式完成,同時(shí)推薦一個(gè)操作格式,下面將詳細(xì)介紹.
一.dataGridView刷新數(shù)據(jù)
如下圖所示,在數(shù)據(jù)庫(kù)中刪除一個(gè)數(shù)據(jù)或插入一個(gè)數(shù)據(jù),我們都希望能夠在左邊的dataGridView控件中顯示操作后的內(nèi)容,而使用dataGridView1.Refresh()刷新只是刷新頁(yè)面重繪控件,沒(méi)有從數(shù)據(jù)庫(kù)里讀取內(nèi)容.所以我們需要重新綁定數(shù)據(jù),點(diǎn)擊按鈕故障信息刪除后,實(shí)現(xiàn)重新讀取數(shù)據(jù)庫(kù)的值,并顯示在控件dataGridView中即可.而這里我采用自定義函數(shù)實(shí)現(xiàn)重新綁定數(shù)據(jù).
?
代碼如下:
public void ShowInfoQues() {//定義數(shù)據(jù)庫(kù)連接語(yǔ)句:服務(wù)器=.(本地) 數(shù)據(jù)庫(kù)名=astring consqlserver = "Data Source=.;Initial Catalog=a;Integrated Security=True;";string sql = "select * from Question"; //定義SQL查詢語(yǔ)句 DataSet ds = new DataSet(); //定義數(shù)據(jù)內(nèi)存中緩存SqlConnection con; //定義SQL Server連接對(duì)象SqlDataAdapter da; //數(shù)據(jù)庫(kù)命令和數(shù)據(jù)庫(kù)連接con = new SqlConnection(consqlserver); //定義SQL Server連接對(duì)象da = new SqlDataAdapter(sql, con); //數(shù)據(jù)庫(kù)命令和數(shù)據(jù)庫(kù)連接con.Open();try{da.Fill(ds); //填充數(shù)據(jù)dataGridView1.DataSource = ds.Tables[0]; //獲取數(shù)據(jù)源賦值數(shù)據(jù)庫(kù)控件}catch (Exception msg){MessageBox.Show(msg.Message); //異常處理}finally{con.Close(); //關(guān)閉連接con.Dispose(); //釋放連接da.Dispose(); //釋放資源} }點(diǎn)擊按鈕"故障信息刪除"后調(diào)用該函數(shù)ShowInfoQues()即可實(shí)現(xiàn),通常填充數(shù)據(jù)中需要判斷語(yǔ)句如下,如果插入到的結(jié)果存在時(shí)數(shù)據(jù)顯示在dataGridView控件中,但這樣會(huì)存在一個(gè)問(wèn)題,當(dāng)數(shù)據(jù)庫(kù)中只存在一個(gè)數(shù)據(jù)時(shí),刪除該數(shù)據(jù)后表中為空就不會(huì)更新dataGridView,因?yàn)榇藭r(shí)已經(jīng)沒(méi)有數(shù)據(jù)就不會(huì)執(zhí)行dataGridView1.DataSource = ds.Tables[0].它會(huì)始終顯示一行數(shù)據(jù),除非在此載入該窗體,所以此時(shí)不需要下面判斷:
if (ds.Tables[0].Rows.Count > 0) //判斷是否符合條件的數(shù)據(jù)記錄 {dataGridView1.DataSource = ds.Tables[0]; }在上面代碼函數(shù)ShowInfoQues()中我設(shè)置為public,因?yàn)樵邳c(diǎn)擊“故障信息導(dǎo)入”按鈕,它會(huì)進(jìn)入另外一個(gè)界面,在那個(gè)界面填寫完要插入的數(shù)據(jù)后同時(shí)更新父窗口dataGridView控件中的數(shù)據(jù),實(shí)現(xiàn)插入數(shù)據(jù)更新,其中子窗口中的構(gòu)造函數(shù)將帶參數(shù),如下圖所示是子界面的代碼:
//父窗體custServ 父窗體中含函數(shù)ShowInfoQues() custServ cstemp = new custServ(); public custServInput(custServ cs) {InitializeComponent();cstemp = cs; } //點(diǎn)擊"確認(rèn)添加"按鈕實(shí)現(xiàn)滿意度調(diào)查 private void button1_Click(object sender, EventArgs e) { ... //插入數(shù)據(jù)操作cstemp.ShowInfoQues(); //父窗口刷新this.Hide(); //隱藏窗體 }父窗體中的代碼如下:
//點(diǎn)擊"故障信息錄入"按鈕 private void repInf_Click(object sender, EventArgs e) {custServInput cSI = new custServInput(this);cSI.Show(); }二.數(shù)據(jù)庫(kù)主外鍵的判斷
在對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作時(shí),通常會(huì)遇到判斷主鍵是否存在或是否重復(fù)的問(wèn)題,或判斷外鍵是否存在的問(wèn)題,因?yàn)槿绻怄I不存在是不能插入數(shù)據(jù)的.在判斷主鍵與外鍵時(shí),我們可以定義一個(gè)函數(shù),通過(guò)調(diào)用這個(gè)函數(shù)進(jìn)行判斷.
//該函數(shù)用于判斷表中Phoneid(手機(jī)序列號(hào))是否存在 private bool IsPhoneIdentify() {string consqlserver = "Data Source=.;Initial Catalog=TeleMS;Integrated Security=True;";string sql = "select * from Question where phoneid='" + textBox2.Text.Trim() + "'";SqlConnection Conn = new SqlConnection(consqlserver); //定義SQL Server連接對(duì)象 Conn.Open();SqlCommand Cmd = new SqlCommand(sql, Conn);SqlDataReader reader = Cmd.ExecuteReader(); //數(shù)據(jù)庫(kù)命令和數(shù)據(jù)庫(kù)連接if (reader.Read()) //存在{return true;}else{return false;} }上面代碼是判斷手機(jī)序列號(hào)是否存在,它通過(guò)判斷數(shù)據(jù)庫(kù)中phoneid=textBox2內(nèi)容,如果存在返回true,如果不存在返回false并提示錯(cuò)誤信息"該外鍵不存在,請(qǐng)驗(yàn)證插入!"提示:使用try{..}catch(Exception msg){MessageBox.Show(msg.Message);}finally{..}時(shí)C#會(huì)自動(dòng)生成異常信息,但通過(guò)自定義函數(shù)總體感覺(jué)更好,至少讓我們能更好的了解數(shù)據(jù)庫(kù)原理及常見(jiàn)的操作錯(cuò)誤.
三.推薦格式源代碼
?通過(guò)上面的敘述,可能你還不知道怎樣使用這些函數(shù)在對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作時(shí)進(jìn)行一些簡(jiǎn)單的判斷,下面通過(guò)需要插入如圖數(shù)據(jù),其中主外鍵表明:
我推薦的代碼如下:
//點(diǎn)擊"確認(rèn)記錄"按鈕實(shí)現(xiàn)滿意度調(diào)查 private void button1_Click(object sender, EventArgs e) {if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == ""){MessageBox.Show("請(qǐng)輸入顧客身份證號(hào)、售后人員編號(hào)和手機(jī)序列號(hào)!", "信息提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);}else if (IsCusIdentify() == false) //顧客身份證號(hào)重復(fù)(主鍵){MessageBox.Show("該顧客身份證號(hào)已經(jīng)存在,請(qǐng)驗(yàn)證輸入!", "錯(cuò)誤提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);}else if (IsPhoneIdentify() == false) //手機(jī)序列號(hào)不存在(外鍵){MessageBox.Show("該手機(jī)序列號(hào)不存在,請(qǐng)驗(yàn)證輸入!", "錯(cuò)誤提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);}else if (IsUserIdentify() == false) //售后人員編號(hào)不存在(外鍵){MessageBox.Show("該售后人員編號(hào)錯(cuò)誤,請(qǐng)驗(yàn)證!", "錯(cuò)誤提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);}else if (MessageBox.Show("確認(rèn)要添加該進(jìn)貨訂單信息?", "驗(yàn)證提示",System.Windows.Forms.MessageBoxButtons.YesNo,System.Windows.Forms.MessageBoxIcon.Question)==System.Windows.Forms.DialogResult.Yes){//實(shí)現(xiàn)插入數(shù)據(jù)操作string sqlInsert = string.Format(@"insert into ServiceInfo...");string consqlserver = "Data Source=.;Initial Catalog=a;Integrated Security=True;";SqlConnection cons = new SqlConnection(consqlserver); //定義SQL Server連接對(duì)象 SqlCommand cmd = new SqlCommand(sqlInsert, cons); //執(zhí)行一個(gè)SQL語(yǔ)句try{cons.Open(); //打開連接cmd.ExecuteNonQuery(); //返回執(zhí)行SQL語(yǔ)句的行數(shù)MessageBox.Show("顧客手機(jī)使用滿意情況調(diào)查數(shù)據(jù)記錄成功!", "信息提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information);csrmtemp.ShowInfoQues(); //父窗口刷新this.Hide(); //隱藏窗體}catch (Exception msg){MessageBox.Show(msg.Message); //異常處理}finally{cons.Close(); //關(guān)閉連接cons.Dispose(); //釋放連接cmd.Dispose(); //釋放資源}} }四.總結(jié)
上面的排版可能有點(diǎn)亂,可能寫得不是很好!但作者盡力了.原本想分成兩篇寫的,但因?yàn)樯厦鏀⑹龅膸c(diǎn)是最近做項(xiàng)目同時(shí)遇到的一些知識(shí),所以就同時(shí)寫了.主要從dataGridView和主外鍵兩個(gè)方面敘述,通過(guò)自己認(rèn)為比較好的代碼,傳遞給大家一些東西.希望大家能學(xué)到有用的知識(shí),作者已盡力,如果有不足和不喜歡的地方,見(jiàn)諒!
(By:Eastmount 2013-9-8 夜11點(diǎn)http://blog.csdn.net/eastmount)
總結(jié)
以上是生活随笔為你收集整理的C# 数据库dataGridView刷新数据和主外键判断的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C# VS2012操作word文档 (二
- 下一篇: C# 数据库存储过程的讲解应用