




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、第五講 Command對象四、案例(在小型超市管理系統中的實現)l SqlCommand .CommandType= CommandType.Text(SQL命令)的例子功能描述:實現對表tbYHXXB(用戶信息表【由用戶ID、用戶名、密碼、用戶類型4個字段構成】,如下圖)的添加、修改和刪除,沒有返回結果,操作結果看表的內容變化代碼參看附錄1l SqlCommand .CommandType= CommandType.StoredProcedure(存儲過程)的例子用到SqlParameter對象功能描述:實現對表tbYHXXB(用戶信息表)的添加、修改和刪除,沒有返回結果,操作結果看表的內容
2、變化代碼參看附錄2l SqlCommand .CommandType= CommandType. TableDirect(表)的例子功能描述:打開表tbYHXXB(用戶信息表),用ExcuteReader方法實現。代碼參看附錄3五、上機實踐仿照【案例】上機練習,驗證。運行界面如下圖【附錄1】提示:MySQL為前面創建的動態鏈接庫ClassLibOfSuperMarket.dllprivate void button1_Click(object sender, EventArgs e) /用SQL命令添加一個用戶:“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錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); catch(Exception Ex) MessageBox.Show(Ex.Message, "SQL錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System
5、.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show("記錄添加成功!請查看數據庫中的表tbYHXXB", "添加記錄", MessageBoxButtons.OK, MessageBoxIcon.Information); private void button2_Click(object sender, EventArgs e) /用SQL命令修改用戶參數:將“10001,張三,收銀員”改為 /“10001,我的名字,經理” 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 用戶名='我的名字',密碼='',用戶類型='經理'" sqlCmd.
7、ExecuteNonQuery(); catch (SqlException sqlEx) MessageBox.Show(sqlEx.Message, "SQL錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); catch (Exception Ex) MessageBox.Show(Ex.Message, "SQL錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.Connec
8、tionState.Open) theConn.Close(); MessageBox.Show("記錄修改成功!請查看數據庫中的表tbYHXXB", "修改記錄", MessageBoxButtons.OK, MessageBoxIcon.Information); private void button3_Click(object sender, EventArgs e) /用SQL命令刪除一個用戶:“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錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); catch (Exception Ex) MessageBox.Show(Ex.Message, "SQL錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show("
11、記錄刪除成功!請查看數據庫中的表tbYHXXB", "刪除記錄", MessageBoxButtons.OK, MessageBoxIcon.Information); 【附錄2】:private void button4_Click(object sender, EventArgs e) /調用存儲過程AddUser添加一個用戶:“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錯誤", MessageBoxButtons.OK, MessageBoxIcon.Erro
16、r); catch (Exception Ex) MessageBox.Show(Ex.Message, "SQL錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show("記錄添加成功!請查看數據庫中的表tbYHXXB", "添加記錄", MessageBoxButtons.OK, MessageBoxI
17、con.Information); private void button5_Click(object sender, EventArgs e) /調用存儲過程ModiUser修改用戶:“12345,玉溪,經理” 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 = "經理" 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錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); catch (Exception Ex) MessageBox.Show(Ex.Message, "SQL錯誤", MessageBoxButtons
22、.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show("記錄修改成功!請查看數據庫中的表tbYHXXB", "添加記錄", MessageBoxButtons.OK, MessageBoxIcon.Information); private void button6_Click(object sender, EventArgs e) /調用存儲過程DeliUser刪
23、除用戶:“12345,玉溪,經理” 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錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); catch (Exception Ex) MessageBox.Show(Ex.Message, "SQL錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show("記錄刪除成功!請查看數據庫中的表tbYHXXB", "添加記錄", MessageBoxButtons.OK, MessageBoxIcon.Information); 【
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中國仁用杏項目創業計劃書
- 中國可穿戴醫療設備項目創業計劃書
- 中國金盞花項目創業計劃書
- 中國內容分析軟件項目創業計劃書
- 中國電子睡眠儀項目創業計劃書
- 樂理級考試試題及答案
- 設施管理人才職業發展路徑-洞察闡釋
- 2025合同范本商業店鋪外墻廣告位租賃合同樣本
- 生態移民安置房置換與交易服務合同
- 商業街區店面全面轉讓及裝修工程合同
- 醫療美容行業美容管理流程標準化解決方案
- 新《安全生產法》安全培訓
- 《工貿企業重大事故隱患判定標準》培訓
- 《南海爭端問題》課件
- 【MOOC】工業設計面面觀-鄭州大學 中國大學慕課MOOC答案
- 中央空調更換壓縮機維修合同書
- 《中小學生時間規劃管理主題班會:做時間的主人》課件(五套)
- (完整版)英語四級詞匯表
- 【生物】魚課件+2024-2025學年人教版生物七年級上冊
- 2024年江蘇省南通市中考化學試卷真題(含答案解析)
- 工業污水處理的PLC控制
評論
0/150
提交評論