




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第五講 Command對(duì)象四、案例(在小型超市管理系統(tǒng)中的實(shí)現(xiàn))l SqlCommand .CommandType= CommandType.Text(SQL命令)的例子功能描述:實(shí)現(xiàn)對(duì)表tbYHXXB(用戶信息表【由用戶ID、用戶名、密碼、用戶類型4個(gè)字段構(gòu)成】,如下圖)的添加、修改和刪除,沒有返回結(jié)果,操作結(jié)果看表的內(nèi)容變化代碼參看附錄1l SqlCommand .CommandType= CommandType.StoredProcedure(存儲(chǔ)過程)的例子用到SqlParameter對(duì)象功能描述:實(shí)現(xiàn)對(duì)表tbYHXXB(用戶信息表)的添加、修改和刪除,沒有返回結(jié)果,操作結(jié)果看表的內(nèi)容
2、變化代碼參看附錄2l SqlCommand .CommandType= CommandType. TableDirect(表)的例子功能描述:打開表tbYHXXB(用戶信息表),用ExcuteReader方法實(shí)現(xiàn)。代碼參看附錄3五、上機(jī)實(shí)踐仿照【案例】上機(jī)練習(xí),驗(yàn)證。運(yùn)行界面如下圖【附錄1】提示:MySQL為前面創(chuàng)建的動(dòng)態(tài)鏈接庫(kù)ClassLibOfSuperMarket.dllprivate void button1_Click(object sender, EventArgs e) /用SQL命令添加一個(gè)用戶:“10001,張三,收銀員” MySQL getConn=new MySQL();
3、 SqlConnection theConn = null; try theConn = getConn.GetConnetion(); theConn.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = theConn; sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "Insert Into tbYHXXB Values('10001','張三','','收銀員')&
4、quot; sqlCmd.ExecuteNonQuery(); catch (SqlException sqlEx) MessageBox.Show(sqlEx.Message, "SQL錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); catch(Exception Ex) MessageBox.Show(Ex.Message, "SQL錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System
5、.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show("記錄添加成功!請(qǐng)查看數(shù)據(jù)庫(kù)中的表tbYHXXB", "添加記錄", MessageBoxButtons.OK, MessageBoxIcon.Information); private void button2_Click(object sender, EventArgs e) /用SQL命令修改用戶參數(shù):將“10001,張三,收銀員”改為 /“10001,我的名字,經(jīng)理” MySQL getConn = new MySQL();
6、SqlConnection theConn = null; try theConn = getConn.GetConnetion(); theConn.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = theConn; sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "Update tbYHXXB Set 用戶名='我的名字',密碼='',用戶類型='經(jīng)理'" sqlCmd.
7、ExecuteNonQuery(); catch (SqlException sqlEx) MessageBox.Show(sqlEx.Message, "SQL錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); catch (Exception Ex) MessageBox.Show(Ex.Message, "SQL錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.Connec
8、tionState.Open) theConn.Close(); MessageBox.Show("記錄修改成功!請(qǐng)查看數(shù)據(jù)庫(kù)中的表tbYHXXB", "修改記錄", MessageBoxButtons.OK, MessageBoxIcon.Information); private void button3_Click(object sender, EventArgs e) /用SQL命令刪除一個(gè)用戶:“10001,張三,收銀員” MySQL getConn = new MySQL(); SqlConnection theConn = null; tr
9、y theConn = getConn.GetConnetion(); theConn.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = theConn; sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "Delete from tbYHXXB where 用戶ID='10001'" sqlCmd.ExecuteNonQuery(); catch (SqlException sqlEx) MessageBo
10、x.Show(sqlEx.Message, "SQL錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); catch (Exception Ex) MessageBox.Show(Ex.Message, "SQL錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show("
11、記錄刪除成功!請(qǐng)查看數(shù)據(jù)庫(kù)中的表tbYHXXB", "刪除記錄", MessageBoxButtons.OK, MessageBoxIcon.Information); 【附錄2】:private void button4_Click(object sender, EventArgs e) /調(diào)用存儲(chǔ)過程AddUser添加一個(gè)用戶:“10001,張三,收銀員” MySQL getConn = new MySQL(); SqlConnection theConn = null; try theConn = getConn.GetConnetion(); theCon
12、n.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = theConn; sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.CommandText = "AddUser" SqlParameter UserID = new SqlParameter(); UserID.ParameterName = "UserID" UserID.Value = "10001" UserID.DbType =
13、 DbType.String; UserID.Direction = ParameterDirection.Input; SqlParameter UserName = new SqlParameter(); UserName.ParameterName = "UserName" UserName.Value = "張三" UserName.DbType = DbType.String; UserName.Direction = ParameterDirection.Input; SqlParameter Pwd= new SqlParameter();
14、 Pwd.ParameterName = "Pwd" Pwd.Value = "" Pwd.DbType = DbType.String; Pwd.Direction = ParameterDirection.Input; SqlParameter UserType = new SqlParameter(); UserType.ParameterName = "Type" UserType.Value = "收銀員" UserType.DbType = DbType.String; UserType.Directi
15、on = ParameterDirection.Input; sqlCmd.Parameters.Add(UserID); sqlCmd.Parameters.Add(UserName); sqlCmd.Parameters.Add(Pwd); sqlCmd.Parameters.Add(UserType); sqlCmd.ExecuteNonQuery(); catch (SqlException sqlEx) MessageBox.Show(sqlEx.Message, "SQL錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Erro
16、r); catch (Exception Ex) MessageBox.Show(Ex.Message, "SQL錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show("記錄添加成功!請(qǐng)查看數(shù)據(jù)庫(kù)中的表tbYHXXB", "添加記錄", MessageBoxButtons.OK, MessageBoxI
17、con.Information); private void button5_Click(object sender, EventArgs e) /調(diào)用存儲(chǔ)過程ModiUser修改用戶:“12345,玉溪,經(jīng)理” MySQL getConn = new MySQL(); SqlConnection theConn = null; try theConn = getConn.GetConnetion(); theConn.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = theConn; sqlCmd.Comman
18、dType = CommandType.StoredProcedure; sqlCmd.CommandText = "ModiUser" SqlParameter UserID = new SqlParameter(); UserID.ParameterName = "UserID" UserID.Value = "12345" UserID.DbType = DbType.String; UserID.Direction = ParameterDirection.Input; SqlParameter UserName = new
19、SqlParameter(); UserName.ParameterName = "UserName" UserName.Value = "玉溪" UserName.DbType = DbType.String; UserName.Direction = ParameterDirection.Input; SqlParameter Pwd = new SqlParameter(); Pwd.ParameterName = "Pwd" Pwd.Value = "" Pwd.DbType = DbType.String
20、; Pwd.Direction = ParameterDirection.Input; SqlParameter UserType = new SqlParameter(); UserType.ParameterName = "Type" UserType.Value = "經(jīng)理" UserType.DbType = DbType.String; UserType.Direction = ParameterDirection.Input; sqlCmd.Parameters.Add(UserID); sqlCmd.Parameters.Add(UserN
21、ame); sqlCmd.Parameters.Add(Pwd); sqlCmd.Parameters.Add(UserType); sqlCmd.ExecuteNonQuery(); catch (SqlException sqlEx) MessageBox.Show(sqlEx.Message, "SQL錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); catch (Exception Ex) MessageBox.Show(Ex.Message, "SQL錯(cuò)誤", MessageBoxButtons
22、.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show("記錄修改成功!請(qǐng)查看數(shù)據(jù)庫(kù)中的表tbYHXXB", "添加記錄", MessageBoxButtons.OK, MessageBoxIcon.Information); private void button6_Click(object sender, EventArgs e) /調(diào)用存儲(chǔ)過程DeliUser刪
23、除用戶:“12345,玉溪,經(jīng)理” MySQL getConn = new MySQL(); SqlConnection theConn = null; try theConn = getConn.GetConnetion(); theConn.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = theConn; sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.CommandText = "DelUser" SqlParamete
24、r UserID = new SqlParameter(); UserID.ParameterName = "UserID" UserID.Value = "" UserID.DbType = DbType.String; UserID.Direction = ParameterDirection.Input; sqlCmd.Parameters.Add(UserID); sqlCmd.ExecuteNonQuery(); catch (SqlException sqlEx) MessageBox.Show(sqlEx.Message, "SQ
25、L錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); catch (Exception Ex) MessageBox.Show(Ex.Message, "SQL錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show("記錄刪除成功!請(qǐng)查看數(shù)據(jù)庫(kù)中的表tbYHXXB", "添加記錄", MessageBoxButtons.OK, MessageBoxIcon.Information); 【
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025屆中國(guó)燃?xì)庑@招聘正式啟動(dòng)筆試參考題庫(kù)附帶答案詳解
- 保證函和保證合同范本
- 加工產(chǎn)品企業(yè)合同范本
- 養(yǎng)殖買賣合同范例
- 農(nóng)村購(gòu)墓地合同范本
- 介紹股合同范本
- 勞動(dòng)聘用合同范本
- 親兄弟合伙合同范本
- pe管材購(gòu)銷合同范本
- 虧損分擔(dān)合同范例
- 2025人教版一年級(jí)下冊(cè)數(shù)學(xué)教學(xué)進(jìn)度表
- DeepSeek教案寫作指令
- 休學(xué)復(fù)學(xué)申請(qǐng)書
- 瓷磚鋪貼勞務(wù)承包協(xié)議書
- 2025年四川司法警官職業(yè)學(xué)院高職單招職業(yè)適應(yīng)性測(cè)試近5年??及鎱⒖碱}庫(kù)含答案解析
- 新建污水處理廠工程EPC總承包投標(biāo)方案(技術(shù)標(biāo))
- 山東省德州市2024-2025學(xué)年高三上學(xué)期1月期末生物試題(有答案)
- 《宏觀經(jīng)濟(jì)管理研究》課件
- 本人報(bào)廢車輛委托書
- 雙減政策與五項(xiàng)管理解讀
- 2025年道德與法治小學(xué)六年級(jí)下冊(cè)教學(xué)計(jì)劃(含進(jìn)度表)
評(píng)論
0/150
提交評(píng)論