資源描述:
《《用戶注冊(cè)》PPT課件》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、第十一講用戶注冊(cè)窗體實(shí)例1教學(xué)目標(biāo)掌握用戶注冊(cè)窗體的設(shè)計(jì)過(guò)程掌握用戶注冊(cè)的事務(wù)處理過(guò)程掌握將數(shù)據(jù)添加到數(shù)據(jù)庫(kù)的方法完成用戶注冊(cè)窗體實(shí)例的程序編寫(xiě)2四、主要程序代碼-Access數(shù)據(jù)庫(kù)C#中對(duì)數(shù)據(jù)庫(kù)操作的主要步驟如下:引入命名空間:usingSystem.Data.OleDb;連接數(shù)據(jù)庫(kù)執(zhí)行SQL語(yǔ)句將數(shù)據(jù)添加到數(shù)據(jù)庫(kù)關(guān)閉數(shù)據(jù)庫(kù)3第一步:連接數(shù)據(jù)庫(kù)構(gòu)造連接數(shù)據(jù)庫(kù)的字符串:stringstrcon="JetOLEDB:EngineType=5;Provider=Microsoft.Jet.OLEDB.4.0;
2、DataSource=student.mdb";或者:stringstrcon="JetOLEDB:EngineType=5;Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+@"....student.mdb";內(nèi)容回顧:4使用OleDbConnection創(chuàng)建數(shù)據(jù)庫(kù)的連接cn:OleDbConnectioncn=newOleDbConnection(strcon);打開(kāi)連接:cn.open();第一大步結(jié)束。5第二步:執(zhí)行SQL語(yǔ)句使用OleDbComma
3、nd創(chuàng)建對(duì)象cmd:OleDbCommandcmd=newOleDbCommand();設(shè)置要執(zhí)行的SQL語(yǔ)句字符串:stringsqlstr=“select/insert/update/delete…….”;設(shè)置cmd對(duì)象的三個(gè)屬性:cmd.Connection=cn;cmd.CommandText=sqlstr;cmd.CommandType=CommandType.Text;第一步中創(chuàng)建的連接6調(diào)用cmd對(duì)象的ExecuteReader()方法執(zhí)行SQL語(yǔ)句,并賦值給類(lèi)OleDbDataReader
4、的對(duì)象SDR:OleDbDataReaderSDR=cmd.ExecuteReader();第二大步結(jié)束。7第三步:讀取SQL語(yǔ)句選擇的數(shù)據(jù)讀取下一條記錄:SDR.Read();判斷是否讀取到數(shù)據(jù),如果能讀取到數(shù)據(jù)說(shuō)明數(shù)據(jù)庫(kù)里面存在相應(yīng)的用戶名和密碼,否則說(shuō)明數(shù)據(jù)庫(kù)里面沒(méi)有:if(SDR.HasRows==true){this.hide();//隱藏登錄窗口Form1frm1=newForm1();//創(chuàng)建并打開(kāi)主界面frm1.Show();}elseMessageBox.Show("用戶名或密碼錯(cuò)誤",
5、"登錄失敗");第四步:關(guān)閉數(shù)據(jù)庫(kù)連接:cn.Close();8完整代碼:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Runtime.InteropServices;usingSystem.Windows.Forms;usingSystem.Data.OleDb;namespac
6、eWindowsApplication1{publicpartialclassForm1:Form{9publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){if(userid.Text.Trim()==""passwd.Text.Trim()=="")MessageBox.Show("請(qǐng)輸入用戶名及密碼","登錄失敗");else{//連接字符串stringstrcon="JetOLED
7、B:EngineType=5;Provider=Microsoft.Jet.OLEDB.4.0;DataSource=student.mdb";//創(chuàng)建數(shù)據(jù)庫(kù)的連接publicOleDbConnectioncn=newOleDbConnection(strcon);10cn.Open();//打開(kāi)連接//創(chuàng)建執(zhí)行命令對(duì)象cmdOleDbCommandcmd=newOleDbCommand();//設(shè)置要執(zhí)行的sql語(yǔ)句sqlstrstringsqlstr="selectfromuserinfowhereU
8、serName='"+userid.Text.Trim()+"'andPaswd='"+passwd.Text.Trim()+"'";//設(shè)置cmd對(duì)象的三個(gè)屬性cmd.Connection=cn;cmd.CommandText=sqlstr;cmd.CommandType=CommandType.Text;//讀取數(shù)據(jù)并賦值給OleDbDataReader對(duì)象SDROleDbDataReaderSDR=cmd.Execute