版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、第十四講 SqlDataReader對象一、SqlDataReader對象SqlDataReader對象是對連接執(zhí)行 Transact-SQL 語句返回的一個只讀的數(shù)據(jù)流。常用屬性:1)Item 索引器屬性,以原始格式獲得一列的值SqlDataReader對象名.索引值SqlDataReader對象名.“表中的字段名”2)FieldCount 獲取當(dāng)前行的列數(shù)3)HasRows 獲取一個值,以表示SqlDataReader對象中是否包含一行或多行記錄。二、常用方法:1)Read 使DataReader對象前進(jìn)到下一條記錄(如果有)2)Close 關(guān)閉DataReader對象。注意,關(guān)閉閱讀器對
2、象并不會自動關(guān)閉底層連接例:在學(xué)生成績數(shù)據(jù)庫中新建一個成績表,輸入期若干個學(xué)生的學(xué)號及語文、數(shù)學(xué)、英語三門課的成績,創(chuàng)建一個視圖,可輸出所有學(xué)生的成績及總分和平均分,并按總分的降序排列。設(shè)計一個應(yīng)用程序,可查看全班所有學(xué)生的總分及平均分。創(chuàng)建視圖的SQL代碼參考如下:CREATE VIEW dbo.成績排名AS SELECT TOP 100 PERCENT 學(xué)號, 語文, 數(shù)學(xué), 英語, 語文 + 數(shù)學(xué) + 英語 AS 總分, (語文 + 數(shù)學(xué) + 英語) / 3 AS 平均分 FROM dbo.成績表 ORDER BY 總分 DESC程序代碼參考如下: string SqlStr = &qu
3、ot;Server=192.168.200.61;User Id=sa;Pwd=;DataBase=stu" SqlConnection con = new SqlConnection(SqlStr); con.Open(); SqlStr = "select * from view1 " SqlCommand cmd = new SqlCommand(SqlStr, con); SqlDataReader dr = cmd.ExecuteReader(); string s = "學(xué)號 語文 數(shù)學(xué) 外語 總分 平均分n" while (dr.
4、Read() int i = 0; while (i < dr.fieldcount) s = s + " " + dri.ToString(); i+; s = s + "n" MessageBox.Show(s); con.Close(); con.Dispose();實作練習(xí):在航班信息管理數(shù)據(jù)庫中建立一個用戶表 (用戶名,密碼,用戶類型),輸入若干條記錄,編程查看并輸出用戶表中的所有記錄。 string constr = "Data Source=0F6A988FB791401;Initial Catalog=航班信息管理;Int
5、egrated Security=True" SqlConnection con = new SqlConnection(constr); con.Open(); string comstr = "select * from 用戶表" SqlCommand com = new SqlCommand(comstr, con); SqlDataReader dr = com.ExecuteReader(); string str = "" ; while (dr.Read() for (int i = 0; i < dr.FieldCount
6、 - 1; i+) str = str + dri.ToString() + " " str = str + "n" MessageBox.Show(str); con.Close();三、案例:用戶登錄界面設(shè)計及實現(xiàn):需要引用的命名空間為:using System.Data.SqlClient;登錄界面代碼: string str = "Server=(local);User Id=sa;Pwd=;DataBase=航班信息管理" SqlConnection con = new SqlConnection(str); con.Ope
7、n(); str = "select * from 用戶表 where 用戶名='" + textBox1.Text + "'" SqlCommand com = new SqlCommand(str, con); com.Connection = con; SqlDataReader dr = com.ExecuteReader(); if(dr.HasRows) dr.Read(); string s = dr1.ToString(); if (s.Trim()=textBox2.Text) Form1 f1 = new Form1(
8、); f1.Show(); else MessageBox.Show("密碼錯誤!"); else MessageBox.Show("用戶名不存在!"); 四、實作練習(xí):編程實現(xiàn)航班信息管理的用戶注冊功能。參考代碼如下: string constr = "Data Source=0F6A988FB791401;Initial Catalog=航班信息管理;Integrated Security=True" SqlConnection con = new SqlConnection(constr); con.Open(); string
9、 comstr = "select * from 用戶表 where 用戶名='" + textBox1.Text + "'" SqlCommand com = new SqlCommand(comstr, con); SqlDataReader dr = com.ExecuteReader(); if (dr.HasRows) MessageBox.Show("用戶名已存在,重新注冊!"); else con.Close(); con.Open(); comstr = "insert into 用戶表 (用
10、戶名,密碼,用戶類型) values('" + textBox1.Text + "','" + textBox2.Text + "','學(xué)生')" com = new SqlCommand(comstr, con); if (com.ExecuteNonQuery() = 1) MessageBox.Show("注冊成功!"); else MessageBox.Show("數(shù)據(jù)庫寫入失敗!"); con.Close();五、向組合框中添加數(shù)據(jù)庫中的信息 st
11、ring str = "Server=(local);User Id=sa;Pwd=;DataBase=航班信息管理" SqlConnection con = new SqlConnection(str); con.Open(); SqlCommand cmd = new SqlCommand("select 航班號 from 航班信息表"); cmd.Connection = con; SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read() comboBox3.Items.Add(dr0); c
12、omboBox3.Text = comboBox3.Items0.ToString();con.Close();向下拉列表框圖中添加數(shù)據(jù)集 string constr = "Data Source=417-80;Initial Catalog=student;Integrated Security=True" SqlConnection con = new SqlConnection(constr); con.Open(); string sqlstr = "select userType from userType" SqlCommand com =
13、new SqlCommand(sqlstr, con); SqlDataReader dr = com.ExecuteReader(); while (dr.Read() cmbUserTpe.Items.Add (dr0.ToString (); con.Close(); cmbUserTpe.SelectedIndex = 0;定義用戶類型類 public class UserTypeClass int userTypeID; string userType; public int UserTypeID get return userTypeID; public string UserTy
14、pe get return userType; public UserTypeClass(int id,string type) userTypeID = id; userType = type; 將用戶類型對象添加到下拉列表框中 string constr = "Data Source=417-80;Initial Catalog=student;Integrated Security=True" SqlConnection con = new SqlConnection(constr); con.Open(); string sqlstr = "select
15、* from userType" SqlCommand com = new SqlCommand(sqlstr, con); SqlDataReader dr = com.ExecuteReader(); while (dr.Read() UserTypeClass x = new UserTypeClass(Convert .ToInt16 ( dr0),dr.GetString(1); cmbUserTpe.Items.Add (x); con.Close(); cmbUserTpe.SelectedIndex = 0; cmbUserTpe.DisplayMember = &q
16、uot;UserType"完成用戶注冊 string constr = "Data Source=417-80;Initial Catalog=student;Integrated Security=True" SqlConnection con = new SqlConnection(constr); con.Open(); string sqlstr = "insert into usertabel(username,userpwd,usertypeid) values('0','1',2)" UserTyp
17、eClass x=cmbUserTpe.SelectedItem as UserTypeClass ; sqlstr = string.Format(sqlstr, txtUsername.Text, txtUserPWD.Text, x.UserTypeID); SqlCommand com = new SqlCommand(sqlstr, con); if (com.ExecuteNonQuery() > 0) MessageBox.Show("用戶注冊成功!"); con.Close();用戶注冊功能的完善(二次輸入密碼是否一致,用戶名是否已注冊) if (tx
18、tUserPWD.Text != txtUserPWD2.Text) MessageBox.Show("二次輸入的密碼不一致!"); else string constr = "Data Source=417-80;Initial Catalog=student;Integrated Security=True" SqlConnection con = new SqlConnection(constr); con.Open(); string sqlstr = "select * from usertabel where username=
19、39;" + txtUsername.Text + "'" SqlCommand com = new SqlCommand(sqlstr, con); if (com.ExecuteScalar() !=null) MessageBox.Show("用戶名已注冊!"); else sqlstr = "insert into usertabel(username,userpwd,usertypeid) values('0','1',2)" UserTypeClass x = cmbUse
20、rTpe.SelectedItem as UserTypeClass; sqlstr = string.Format(sqlstr, txtUsername.Text, txtUserPWD.Text, x.UserTypeID); com = new SqlCommand(sqlstr, con); if (com.ExecuteNonQuery() > 0) MessageBox.Show("用戶注冊成功!"); con.Close(); 用戶登錄功能的實現(xiàn)與SQL注入式攻擊的防范: string constr = "Data Source=417-80
21、;Initial Catalog=student;Integrated Security=True" SqlConnection con = new SqlConnection(constr); con.Open(); string sqlstr = "select * from usertabel where username='0' and userpwd='1'" sqlstr =string .Format (sqlstr,txtUsername.Text ,txtUserPWD.Text ); SqlCommand com
22、 = new SqlCommand(sqlstr, con); if (com.ExecuteScalar() != null) MessageBox.Show("登錄成功!"); this.Close(); else MessageBox.Show("登錄失敗!"); con.Close();SQL注入式攻擊若用戶輸入的用戶名為abc,密碼為' or '1'='1 則sqlstr =string .Format (sqlstr,txtUsername.Text ,txtUserPWD.Text );命令生成的sqlstr為:s
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 智研咨詢發(fā)布:2024年中國變壓器行業(yè)市場現(xiàn)狀及投資前景分析報告
- 二零二五個人退伙合作協(xié)議(旅游文化產(chǎn)業(yè)特定)2篇
- 重慶市集成電路產(chǎn)業(yè)發(fā)展政策優(yōu)化研究
- RIS輔助的低軌衛(wèi)星通信系統(tǒng)覆蓋性能優(yōu)化策略研究
- 二零二五年度專業(yè)運輸個人承包合同范本2篇
- 二零二五版養(yǎng)老保險待遇領(lǐng)取資格終止?fàn)幾h處理合同3篇
- 二零二五年度個人金融衍生品交易合同范本2篇
- 二零二五版?zhèn)€人合伙健身俱樂部退伙會員權(quán)益協(xié)議2篇
- 二零二五年度個人商鋪租賃合同涉及租賃保證金退還細(xì)則2篇
- 近年來我國藥事管理工作的重大事件
- 【MOOC】數(shù)字?jǐn)z影技術(shù)與藝術(shù)-西南石油大學(xué) 中國大學(xué)慕課MOOC答案
- 礦石包銷合同范本
- B區(qū)地下室碳纖維加固施工方案
- 旅行社脫團(tuán)安全協(xié)議書范文模板
- 期中測試卷-2024-2025學(xué)年統(tǒng)編版語文五年級上冊
- 醫(yī)院三基考核試題(康復(fù)理療科)
- 新教材人教版高中物理選擇性必修第三冊全冊各章節(jié)知識點考點
- CJT 354-2010 城市軌道交通車輛空調(diào)、采暖及通風(fēng)裝置技術(shù)條件
- 2024-2030年中國招標(biāo)代理行業(yè)深度分析及發(fā)展前景與發(fā)展戰(zhàn)略研究報告
- 醫(yī)師定期考核 (公共衛(wèi)生)試題庫500題(含答案)
- 暑假作業(yè) 11 高二英語語法填空20篇(原卷版)-【暑假分層作業(yè)】2024年高二英語暑假培優(yōu)練(人教版2019)
評論
0/150
提交評論