版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第19章ADO.NET(C#)數(shù)據(jù)庫技方案,與過去的ADO或OLEDB相比更適合開發(fā)企業(yè)級應(yīng)用程序。下面將通過幾個ADO.NTE的幾種連接技術(shù)。實例 235-235-SQLServer2000是一個大型的關(guān)系數(shù)據(jù)庫系統(tǒng),在.NET中對SQLServerADO對數(shù)據(jù)19.1.1所示。19.1.1235-235-(DataSet什么數(shù)據(jù)格式傳輸,在.NET框架中,使用數(shù)據(jù)集可以方便地在各層之間傳輸數(shù)據(jù)。表 說設(shè)置用于打開SQLServer數(shù)據(jù)庫的字符 "Server=(local);User (local機上。如果要連接計算機只需把(local)換成計算機的IP地址或計算機名稱即可。例如Userid:SQLServerSQLServer管理員賬sa進行登陸。235-DataBase:是指選定本地計算機或計算機時想要連接SQLServer數(shù)據(jù)庫的名稱235-完成ADO.NETsnear表 字類長是否主4是否否否否否8否√新建一個,創(chuàng)建一個新窗體,默認名為“Default.aspxHTML選項中向窗體拖放一個table控件,在數(shù)據(jù)選項中拖放一個GridView控件添加到table控件中。設(shè)計代碼前,需要先命名空間usingSystem.Data.Sql;using;在Page_Load加載程序中編寫代碼,如下所示protectedvoidPage_Load(objectsender,EventArgs{stringConStr="DataSource=;IntegratedSecurity=sspi;"+"InitialSqlConnectionmyconnection=newSqlConnection(ConStr);stringSqlStr="Select*Fromgoodstable";SqlDataAdaptermyadapter=newSqlDataAdapter(myconnection,myconnection);DataSetmyset=newDataSet();GridView1.DataSource=myset;}235-實例 ODBC連接數(shù)據(jù)236-236-本實例實現(xiàn)ASP.NET通過ODBCSQL數(shù)據(jù)庫,并在頁面中顯示出數(shù)據(jù)庫連接狀態(tài),單SQL.2ODBC連接數(shù)據(jù)236-236-ODBC提供了一個公共數(shù)據(jù)層,可用來幾乎所有的RDBMS,ODBC使用RDBMS的驅(qū)動程序數(shù)據(jù)源,而該驅(qū)動程序由ODBCDriverManager加載和管理。它還提供諸如APIDriverManage進行通信,因此如果需RDBMS,則只需要改變連接細節(jié)。ODBC最重要的特性是,它236-完成ODBC236-“efault.aspx向窗體拖放一個tableButton控件和一個Label添加到table控件中。設(shè)計代碼前,需要先命名空間usingSystem.Data.Odbc;,代碼如下 using 236-protectedvoidButton1_Click(objectsender,EventArgs{236-protectedvoidButton1_Click(objectsender,EventArgs{stringodbcstr="DSN=sqlODBC;uid=sa;pwd=";OdbcConnectionoc=newOdbcConnection(odbcstr);{Label1.Text連接成功}catch(Exception{Label1.Text=}{}}實例 Access數(shù)據(jù)庫連DAO連接加密的Access97237-237-Access數(shù)據(jù)庫是微軟公司的小型數(shù)據(jù)庫產(chǎn)品,是Office產(chǎn)品中的一個組件。AccessPAP、JSP等語言直接,相比起其他數(shù)據(jù)庫靈活性更大。本實例介紹使用ASP.NET實現(xiàn)與19.1.3所示。19.1.3Access237-237-本實例中,起著重要作用的是ASP.NET連接Access數(shù)據(jù)庫的連接字符串,stringstringConStr=.Jet.OLEDB.4.0;Datasource="分析連接Access數(shù)據(jù)庫字符串,連接字符串中,Provider屬性指定使用的數(shù)據(jù)庫引擎.Jet.OLEDB.4.0。Datasource237-237-完成連接Access“efault.aspx中添加table在標準選項中拖放一個Button和一個Label控件控件置于table設(shè)計代碼前,需要先命名空間usingSystem.Data.OleDb;,如下所示(4)在Button(4)在Button按鈕的protectedvoidButton1_Click(objectsender,EventArgs{stringConStr= .Jet.OLEDB.4.0;Datasource="237-237-OleDbConnectionOC=newOleDbConnection(ConStr);{Label1.Text成功連接Access數(shù)據(jù)庫}{Label1.Text連接Access數(shù)據(jù)庫失敗}{}}實例 Oracle數(shù)據(jù)庫連238-238-ASP.NETOracle數(shù)據(jù)庫連接狀態(tài),單擊“確認連接按鈕”,其右冊出現(xiàn)19.1.419.1.4Oracle238-238-連接和操作Oracle數(shù)據(jù)庫ASP.NET專門提供的Oracle.NETFramework數(shù)據(jù)庫提供程序類。Oracle數(shù)據(jù)庫提供程序位于System.Data.Oracle 命名空間中,并包含在System.Data.Oracle.dll程序集中。在項目中添加Oracle.dll的步驟是:進入“解決方案管理器”,右鍵單擊本項目,在右鍵菜單里選擇添加項,然后在“.NET”表中選擇“System.Data.Oracle”項并點using;238-“efault.aspx中添加table在標準選項中拖放一個Button和一個Label控件控件置于table設(shè)計代碼前,需要先命名空間usingSystem.Data.OleDb;,如下所示 usingSystem.Data. 238-protectedvoidButton1Click(objectsender,EventArgs{238-protectedvoidButton1Click(objectsender,EventArgs{stringoracleconnstr="DataSource=Oracle9i;IntegratedSecurite=yes";OracleConnectionoConnection=newOracleConnection(oracleconnstr);{Label1.Text連接Oracle數(shù)據(jù)庫成功}catch(Exception{Label1.Text連接Oracle數(shù)據(jù)庫失敗}{}}ASP.NET控件中,GridView控件是功能最強大的。除了可以采取實例 利用GridView瀏239-239-通過GridViewDataSetDataSourceGridViewDataBind方法就可以了。19.2.1所示。19.2.1GridView瀏覽數(shù)據(jù)庫.表格單239-239-ADO.NET有兩個組件構(gòu)成,分別是DataSet和.NETFramework數(shù)據(jù)提供程序,而.NETFramework數(shù)據(jù)提供程序包括、mand、DataReader和DataAdapter對象,因此DataAdapterDataSet對象、ConnectionDataAdapter對象下面對3個對象進行介紹。Connection對象:數(shù)據(jù)庫應(yīng)用程序與數(shù)據(jù)庫進行交互首先必須建立與數(shù)據(jù)庫的連接,在ADO.NET中可以使用Connection對象進行數(shù)據(jù)庫的連接。ConnectionString屬性是Connection對象的關(guān)鍵屬性,Connection對象是通過ConnectionString屬性的設(shè)置來連接數(shù)據(jù)庫DataAdapter對象:DataAdapter可以比作DataSet對象和數(shù)據(jù)之間的橋梁,在ADO.NET中具有及其重要的作用,DataAdapterFillDataSet中在對數(shù)據(jù)完成增加、刪除、或修改操作后在調(diào)用Update方法來更新數(shù)據(jù)源。DataSet對象:DataSet對象是由一組DataTable對象組成,它具備多個表數(shù)據(jù)以及表間關(guān)系的能力這些表就在DataTable對象中而表之間的關(guān)系則用DataRlation對象表示。DataTable對象中包含了DataRow和Dolumn對象,分別存放表中行和列的數(shù)據(jù)信息。DataSet對象中的數(shù)據(jù)完全采取XML格式,它和XML文檔可以互換,這樣就使得跨平臺成為可能,也似的DataSet對象可以作為Web服務(wù)或其它類型調(diào)用的返回值DataSet類的構(gòu)造函數(shù)創(chuàng)建DataSet類的新對象通常使用的語法有兩種:PublicDataSet():初始化DataSet239-239-完成GridView“ealt.aspxHTML選項中向窗體拖放一個table19.2.2table控在數(shù)據(jù)選項中拖放一個GridView控件添加到table設(shè)計代碼前,需要先命名空間using ;引引用命名空間 protectedvoidPage_Load(objectsender,protectedvoidPage_Load(objectsender,EventArgs{//連接字符stringConStr="server=(local);user//創(chuàng)建SqlConnection對象并與數(shù)據(jù)庫SqlConnectionmyconnection=new//定義SQL語stringSqlStr="select*from//創(chuàng)建SqlDataAdapter對象并接收SQL語SqlDataAdaptermyadapter=newSqlDataAdapter(SqlStr,//創(chuàng)建數(shù)據(jù)DataSetmyset=new//指定GridView控件的數(shù)據(jù)源并綁定至GridView1.DataSource=myset;//關(guān)閉數(shù)據(jù)庫連239-239-}實例 利用GridView瀏240-240-GridView控件顯示出的數(shù)據(jù)分頁顯示。通常當您的數(shù)據(jù)庫中的數(shù)19.2所示。19.2.3GridView瀏覽數(shù)據(jù)庫.表格多240-240-Alloging,用來設(shè)置GridView控件是都允許分頁,另一個就是PageIndexChanging,用來設(shè)置 (2(2) 填寫PageIndexChanging的分頁代碼。代碼如下所示240-protectedvoidGridView1PageIndexChanging(objectsender, geEventArgs{GridView1.PageIndex=e.Ne }完成GridViewDefal.aspx中添加table在數(shù)據(jù)選項中拖放一個GridView控件添加到table把GridView控件的 ging屬性設(shè)置為“true“ ging屬性設(shè)設(shè)計代碼前,需要先命名空間using ;編寫PageIndexChanging的分頁代碼protectedvoidPageLoad(objectsender,EventArgs{//連接字符stringConStr="server=(local);user//創(chuàng)建SqlConnection對象并與數(shù)據(jù)庫連SqlConnectionmyconnection=new//定義SQL語stringSqlStr="select*from//創(chuàng)建SqlDataAdapter對象并接收SQL語SqlDataAdaptermyadapter=newSqlDataAdapter(SqlStr,//創(chuàng)建數(shù)據(jù)DataSetmyset=new//指定GridView控件的數(shù)據(jù)源并綁定至控GridView1.DataSource=myset;//關(guān)閉數(shù)據(jù)庫}protectedvoidGridView1_PageIndexChanging(objectsender,GridVie{GridView1.PageIndex=e.NegeIndex;}240-數(shù)據(jù)查詢是常用的數(shù)據(jù)庫操作之一,從數(shù)據(jù)庫中查詢數(shù)據(jù)一般有兩中方式:一是通過DataReader對象來直接;另一種則是通過DataSet和DataAdapter來。在SQLServer中提供了Select語句進行數(shù)據(jù)查詢,但Select語句查詢的語法有很多種,下面幾個示例將要介紹ASP.NET如何通過Select語句對數(shù)據(jù)庫進行各種查詢。實例 通用查242-242-DropDownList控件、TextBoxButtonGridView19.3.1所示:19.3.1242-242-stringstringSqlstr="Select*FromgoodstableWhere"+DropDownList1.SelectedValue+"LikeLIKEM%'將搜索以字母MLIKEh'將搜索以字母hLIKEabc%'將搜索在任何位置包含字母abcLIKE_ty'tyLIKE[CK]ars[eo]n'將搜索下列字符串:Carsen、Karsen、CarsonKarsonLIKEM-Z]ty'tyMZ242-LIKET[^a]%'Ta242-“efault.aspx中添加table在數(shù)據(jù)選項中拖放一個GridView控件添加到tableDropDownList控件、TextBoxButtontableButton控件的Text19.3.2DropDownList控件的“ListItemsItems5個成員,它們的Text屬性和綁定數(shù)據(jù)的字段值分別為:成成員成員成員成員成員商品名商品規(guī)商品型商品價商品數(shù)eeceer 命名空間using ;protectedvoidPage_Load(objectsender,EventArgs{stringConStr="Server=(local);Userid=sa;pwd=;DataBase=snear";SqlConnectionmyconnection=newSqlConnection(ConStr);stringSqlStr="select*fromSqlDataAdaptermyadapter=newprotectedvoidPage_Load(objectsender,EventArgs{stringConStr="Server=(local);Userid=sa;pwd=;DataBase=snear";SqlConnectionmyconnection=newSqlConnection(ConStr);stringSqlStr="select*fromSqlDataAdaptermyadapter=newSqlDataAdapter(SqlStr,myconnection);DataSetmyset=newDataSet();GridView1.DataSource=myset;}242-242-protectedvoidGridView1_PageIndexChanging(objectsender, {GridView1.PageIndex=e.Ne }protectedvoidButton1_Click(objectsender,EventArgs{stringConStr="Server=(local);Userid=sa;pwd=;DataBase=snear";SqlConnectionmyconnection=newSqlConnection(ConStr);stringSqlstr="Select*FromgoodstableWhere"+DropDownList1.SelectedValue+"Like'%"+TextBox1.Text+"%'";SqlDataAdaptermyadapter=newSqlDataAdapter(Sqlstr,myconnection);DataSetmyset=newDataSet();GridView1.DataSource=myset;}實例 模糊查243-243-19.3.3所示:19.3.3243-243-stringstringSqlStr="select*fromgoodstablewheregoodsnamelike'%"++"%'orgoodsspeclike'%"+TextBox1.Text+"%'orgoodstypelike'%"+TextBox1.Text+"%' goodspricelike'%"+TextBox1.Text+"%'orgoodsnumberlike'%"+TextBox1.Text+通過SQLServer提供的“Like”關(guān)鍵字和“%243-Text243-新建一個標準,創(chuàng)建一個新窗體,默認名為“Default.aspx在工具箱的HTML選項中向窗體拖放一個table控件。在數(shù)據(jù)選項中拖放一個GridView控件添加到table在標準選項中拖放一個TextBox控件和一個Button設(shè)計代碼前,需要先命名空間usingSystem.Data.Sql;編寫Page_Load中連接SQLServer數(shù)據(jù)庫的連接代碼,在Button按紐的243-protectedvoidPage_Load(objectsender,EventArgs{SqlConnectionmyconnection243-protectedvoidPage_Load(objectsender,EventArgs{SqlConnectionmyconnection=newSqlConnection("server=(local);userSqlDataAdaptermyadapter=newSqlDataAdapter("select*fromgoodstable",DataSetmyset=newDataSet();GridView1.DataSource=myset;}protectedvoidButton1_Click(objectsender,EventArgs{SqlConnectionmyconnection=newSqlConnection("server=(local);userstringSqlStr="select*fromgoodstablewheregoodsnamelike'%"+TextBox1.Text+"%'orgoodsspeclike'%"+TextBox1.Text+"%'orgoodstypelike'%"+TextBox1.Text+"%' goodspricelike'%"+TextBox1.Text+"%'orgoodsnumberlike'%"+TextBox1.Text+"%'";SqlDataAdaptermyadapter1=newSqlDataAdapter(SqlStr,myconnection);DataSetmyset=newDataSet();GridView1.DataSource=myset;}實例 數(shù)244-244- 果是以商品價格的升序,商品數(shù)量的降序進行排序。如圖19.3.4所示。19.3.4數(shù)據(jù)244-244-SQLServerOrderBy語句對查詢結(jié)果進行排序的。OrderBy子句,指stringSqlStr="select*fromgoodstableorderbystringSqlStr="select*fromgoodstableorderbygoodsnumberdesc,goodsprice244-244-完成數(shù) “efault.aspx中添加table在數(shù)據(jù)選項中拖放一個GridView控件添加到table設(shè)計代碼前,需要先命名空間using ;編寫Page_Load中連接SQLServer數(shù)據(jù)庫的連接代碼和執(zhí)行排序查詢代碼,代protectedprotectedvoidPage_Load(objectsender,EventArgs{//stringConStr="server=(local);userid=sa;pwd=;DataBase=snear";SqlConnectionmyconnection=newSqlConnection(ConStr);stringSqlStr="select*fromgoodstableorderbygoodsnumberdesc,goodspriceasc";//創(chuàng)建SqlDataAdapterSqlDataAdaptermyadapter=newSqlDataAdapter(SqlStr,myconnection);//創(chuàng)建DataSetDataSetmyset=newDataSet();//GridView1.DataSource=myset;244-244-}protectedvoidGridView1_PageIndexChanging(objectsender, {//GridView1.PageIndex=e.Ne }使用ODBC非DSN連接SQLServer數(shù)據(jù)庫實例 分組統(tǒng)使用ODBCDSN連接MySQLServer245-245-分組統(tǒng)計類似于報表底部的小計,對報表中的某些數(shù)據(jù)進行匯總,壓縮都一條記錄上。SQLServerGroupBy子句進行數(shù)據(jù)分組。本例運行后將會統(tǒng)計出原有數(shù)據(jù)表內(nèi)同等商19.3.5所示19.3.519.3.6245-245-SQLServerGroupBy子句對數(shù)據(jù)的查詢結(jié)果進行分組統(tǒng)計。GROUPBY子句用來為結(jié)果集中的每一行產(chǎn)生聚合值。如果聚合函數(shù)沒有使用GROUPBY子句,則只為SELECT語句報告一個聚合值。245-GROUPBY關(guān)鍵字后面跟著列的列表,稱為分組列。GROUPBY子句限制結(jié)果集中的行;對于分組列中的每個非重復(fù)值只有一行。本實例在ASP.NET的GROUPBY子句分組統(tǒng)計代245-“efault.aspx中添加table在數(shù)據(jù)選項中拖放一個GridView控件添加到table設(shè)計代碼前,需要先命名空間using ;245-protectedvoidPage_Load(objectsender,EventArgs{stringConStr245-protectedvoidPage_Load(objectsender,EventArgs{stringConStr="server=(local);userid=sa;pwd=;DataBase=snear";SqlConnectionmyconnection=newSqlConnection(ConStr);stringSqlStr="selectsum(goodsnumber)sumnumber,goodsidfromgoodssellgroupbygoodsid";SqlDataAdaptermyadapter=newSqlDataAdapter(SqlStr,myconnection);DataSetmyset=newDataSet();GridView1.DataSource=myset;}實例 數(shù)據(jù)匯246-246-19.3.7所示。19.3.7246-246-246-stringSqlStr="select246-stringSqlStr="selectsum(goodsnumber)sumnumberfromgoodssell“efault.aspx中添加table在數(shù)據(jù)選項中拖放一個GridView控件添加到table在標準選項中拖放一個Button控件置于table設(shè)計代碼前,需要先命名空間usingSystem.Data.Sql;246-protectedvoidPage_Load(objectsender,EventArgs{SqlConnectionmyconnection=newSqlConnection("server=(local);userSqlDataAdaptermyadapter=newSqlDataAdapter("select246-protectedvoidPage_Load(objectsender,EventArgs{SqlConnectionmyconnection=newSqlConnection("server=(local);userSqlDataAdaptermyadapter=newSqlDataAdapter("select*fromgoodssell",DataSetmyset=newDataSet();GridView1.DataSource=myset;}protectedvoidButton1_Click(objectsender,EventArgs{SqlConnectionmyconnection=newSqlConnection("server=(local);userstringSqlStr="selectsum(goodsnumber)sumnumberfromgoodssell";SqlDataAdaptermyadapter=newSqlDataAdapter(SqlStr,myconnection);DataSetmyset=newDataSet();GridView2.DataSource=myset;}使用ODBC非DSN連接SQLServer數(shù)據(jù)庫實例 多表查使用ODBCDSN連接MySQLServer247-247-SELECT語句除了可以顯示單個表中的信息之外,還可以顯示來自多個表中的信息。本實例from19.3.8所示:19.3.8本實例主要通過SQLServer提供的“Like”關(guān)鍵字和“%”通配符進行查詢,將兩張數(shù)據(jù)表其字段名依次綁定至文本框控件Text屬性,代碼執(zhí)行后,在文本框中輸入有關(guān)數(shù)據(jù)表內(nèi)容的任意247-stringsql1="select*from(SELECTgoodssell.goodsid,goodssell.goodsnumber,goodstable.goodsname,goodstable.goodsspec,goodstable.goodstype,goodstable.goodsnumberAS247-stringsql1="select*from(SELECTgoodssell.goodsid,goodssell.goodsnumber,goodstable.goodsname,goodstable.goodsspec,goodstable.goodstype,goodstable.goodsnumberASExpr1,goodstable.goodspriceFROMgoodssellLEFTOUTERJOINgoodstableONgoodssell.goodsid=goodstable.id)DERIVEDTBLwheregoodsidlike'%"+TextBox1.Text+"%'orgoodsspeclike'%"+TextBox1.Text+"%'"新建一個,創(chuàng)建一個窗體,默認名為“Default.aspx在主頁面工具箱HTML中添加table控件。3GridViewtable23個GridView2個GridView(3)1個TextBox1buttontable247-protectedvoidButton1_Click(objectsender,EventArgs247-protectedvoidButton1_Click(objectsender,EventArgs{SqlConnectionmyconnection=newSqlConnection("server=(local);userstringsql1="select*from(SELECTgoodssell.goodsid,goodssell.goodsnumber,goodstable.goodsname,goodstable.goodsspec,goodstable.goodstype,goodstable.goodsnumberASExpr1,goodstable.goodspriceFROMgoodssellLEFTOUTERJOINgoodstableONgoodssell.goodsid=goodstable.id)DERIVEDTBLwheregoodsidlike'%"+TextBox1.Text+"%'orgoodsspeclike'%"+TextBox1.Text+"%'";SqlDataAdaptermyadapter3=newSqlDataAdapter(sql1,myconnection);DataSetmyset=newDataSet();GridView3.DataSource=myset;}實例 批量錄入數(shù)248-248-19.4.1-19.4.2所示.2248-248-SqlConnection對象和mand對象,然后打開數(shù)據(jù)庫連接,最后調(diào)用mandExecuteNonQueryInsert語句中指定查詢語句來實現(xiàn)數(shù)據(jù)的批量插入。使用insert248-stringSqlSre="insertintogoodstable(goodsname,goodsspec,goodstype,goodsprice,goodsnumber)values('"+gdsname3+"','"+gdsspec3+"','"+gdstype3+"','"+gdsprice3+"','"+nb3+"')";(2)Insert…Select248-stringSqlSre="insertintogoodstable(goodsname,goodsspec,goodstype,goodsprice,goodsnumber)values('"+gdsname3+"','"+gdsspec3+"','"+gdstype3+"','"+gdsprice3+"','"+nb3+"')";新建一個,創(chuàng)建一個窗體,默認名為“Default.aspx在主頁面工具箱HTML中添加table控件。stringConStr="server=(local);userid=sa;pwd=;DataBase=snear";SqlConnectionmyconnection=newstringConStr="server=(local);userid=sa;pwd=;DataBase=snear";SqlConnectionmyconnection=newSqlConnection(ConStr);stringSqlStr="select*fromgoodstable";SqlDataAdaptermyadapter=newSqlDataAdapter(SqlStr,myconnection);DataSetmyset=newGridView1.DataSource=protectedvoidButton1_Click(objectsender,EventArgs{}在標準選項中拖放一個Button控件置于table控件,在Button按鈕的Click GridView1.DataSource=protectedvoidButton1_Click(objectsender,EventArgs{}Web窗體,默認名為“Default2.aspxHTML選項中向窗體拖放一個table5個TextBox3個Button控件。protectedvoidButton1_Click(objectsender,EventArgs{//若1-5文本框為空時,就會彈出消息框提if(this.tb1.Text==""||tb2.Text==""||tb3.Text==""||tb4.Text==""||tb5.Text==""){}{stringgdsname=this.TextBox1.Text;stringgdsspec=this.TextBox2.Text;stringgdstype=this.TextBox3.Text;stringgdsprice=this.TextBox4.Text;stringnb=this.TextBox5.Text;stringSqlStr="insertintogoodstable(goodsname,goodsspec,goodstype,goodsprice,goodsnumber)values('"+gdsname+"','"+gdsspec+"','"+gdstype+"','"+gdsprice+"','"+nb+"')";SqlConnectionmycon=newSqlConnection("server=(local);usermandmycmd=new mand(SqlStr,mycon);if(tb6.Text==""&&tb11.Text==""&&tb16.Text=={}}}248-實例 249-249-能,還要具有修改功能。本實例將要介紹在ASP.NET中是怎樣進行修改數(shù)據(jù)的。19.4.3所示。19.4.3GridView編輯數(shù)249-249-當數(shù)據(jù)添加到表中后,如果某些數(shù)據(jù)發(fā)生了變化,就需要對表中的數(shù)據(jù)進行修改。在SQLServer中,對數(shù)據(jù)的修改可以通過UPDATE語句實現(xiàn)。語法格式如下:UPDATE目標表SET{列名=UPDATE目標表SET{列名=表達式}[,...n][FROM另一表名][WHERE條件表達式249-stringSqlStr="updategoodstablesetgoodsname='"+this.TextBox1.Text+"',goodsspec='"+this.TextBox2.Text+"',goodstype='"+this.TextBox3.Text+"',goodsprice='"+this.TextBox4.Text249-stringSqlStr="updategoodstablesetgoodsname='"+this.TextBox1.Text+"',goodsspec='"+this.TextBox2.Text+"',goodstype='"+this.TextBox3.Text+"',goodsprice='"+this.TextBox4.Text+"',goodsnumber='"+this.TextBox5.Text+"'whereid="+Request["id"];“Defalt.aspx在工具箱的HTML選項中向窗體拖放一個tableGridView控件添加到table控件中并與數(shù)據(jù)庫連接,將數(shù)據(jù)表19.4.4添加編輯edit.aspx中向窗體拖放一個table5個TextBox1個Button控件。如圖protectedvoidButton1Click(objectsender,EventArgs{SqlConnectionmycon=newSqlConnection("server=(local);userstringSqlStr="updategoodstablesetgoodsname='"+this.TextBox1.Text+"',goodsspec='"+this.TextBox2.Text+"',goodstype='"+this.TextBox3.Text+"',goodsprice='"+this.TextBox4.TextprotectedvoidButton1Click(objectsender,EventArgs{SqlConnectionmycon=newSqlConnection("server=(local);userstringSqlStr="updategoodstablesetgoodsname='"+this.TextBox1.Text+"',goodsspec='"+this.TextBox2.Text+"',goodstype='"+this.TextBox3.Text+"',goodsprice='"+this.TextBox4.Text+"',goodsnumber='"+this.TextBox5.Text+"'whereid="+Request["id"]; = mand(SqlStr,249-249-}實例 250-250-19.4.6所示。250-250-SQLServer中的DELETE語句可以刪除數(shù)據(jù),其語法及相關(guān)用法如下: DELETE[table.*]FROMtableWHERE DELETE語句的語法包含如下部分:可以使用DELETEExecute方法與一個DROP語句從數(shù)據(jù)庫中放棄之中的值,應(yīng)創(chuàng)建一個更新查詢將值改變?yōu)镹ull。 stringSqlStr="deletefromgoodstablewhereid='" 250-250-GridView1.DataKeys[e.RowIndex].Value+Defal.aspx在工具箱的HTML選項中向窗體拖放一個tableGridView控件添加到table控件中并與數(shù)據(jù)庫連接,將數(shù)據(jù)表在GridViewusingusing;protectedvoidPage_Load(objectsender,EventArgs{SqlConnectionmyconnection=newSqlConnection("server=(local);userSqlDataAdaptermyadapter=newSqlDataAdapter("select*fromgoodstable",DataSetmyset=newDataSet();GridView1.DataSource=myset;GridView1.DataKeyNames=newstring[]{"id"};}protectedvoidGridView1RowDeleting(objectsender,GridViewDeleteEventArgs{SqlConnectionmyconnection=newSqlConnection("server=(local);user mand=new mand("deletefromgoodstablewhereid='"+GridView1.DataKeys[e.RowIndex].Value+"'",myconnection);SqlDataAdaptermyadapter=newSqlDataAdapter("select*fromgoodstable",DataSetmyset=newDataSet();GridView1.DataSource=myset;GridView1.DataKeyNames=newstring[]{"id"};}GridViewButton控件Click單 250-251-251-實例 過程的應(yīng)過程是的應(yīng)用程序,它是在服務(wù)器上的例行程序幾過程??梢越o過程傳19.5.1-19.5.2所示。圖19.5.1過程的應(yīng)圖19.5.2過程的應(yīng)251-251-過程可以由應(yīng)用程序通過一個調(diào)用來執(zhí)行,而且允許用戶變量和有條件執(zhí)行,允許在SQLServer中建立一個以后使用EXECUTE語句來執(zhí)行過程其中mandmandcmd1=mand("exec'"+tb1.Text+"','"+tb2.Text+"','"+tb3.Text+"','"+tb4.Text+"','"+tb5.Text+"'",SqlConnection("server=(local);user 251-“Default.aspx添加table控件。GridView控件添加到table控件中并與數(shù)據(jù)庫連接,將數(shù)據(jù)表Web窗體,默認名為“Default2.aspxHTML選table5TextBox1Button控件。分別將Button控件的Text設(shè)計代碼前,需要先命名空間usingSystem.Data.Sql;251-251-protectedvoidPage_Load(objectsender,EventArgs{}protectedvoidButton1_Click(objectsender,EventArgs{mandcmd1=new mand("execaddGoodstable'"+tb1.Text+"','"+tb2.Text+"','"+tb3.Text+"','"+tb4.Text+"','"+tb5.Text+"'",newSqlConnection("server=(local);userid=sa;pwd=;Database=snear"));}protectedvoidButton2_Click(objectsender,EventArgs{}實例 試圖的應(yīng)252-252-SQL查詢。19.5.3252-252-GREATEGREATEVIEWview_nameSELECT[WithCheck252-SqlDataAdaptermyadapter1252-SqlDataAdaptermyadapter1=newSqlDataAdapter("select*fromVIEWgoodssell",新建一個標準,創(chuàng)建一個新窗體,默認名為“Default.aspx在工具箱HTML選項中想主頁面添加table控件。在數(shù)據(jù)選項中拖放3個GridView控件添加到tableGridView1控件和protectedvoidPage_Load(objectprotectedvoidPage_Load(objectsender,EventArgs{SqlConnectionmyconnection=newSqlConnection("server=(local);userSqlDataAdaptermyadapter1=newSqlDataAdapter("select*fromVIEWgoodssell",myconnection);SqlDataAdaptermyadapter2=newSqlDataAdapter("select*from252-252-SqlDataAdaptermyadapter3=newSqlDataAdapter("select*fromgoodssell",DataSetmyset1=newDataSet();DataSetmyset2=newDataSet();DataSetmyset3=newDataSet();GridView1.DataSource=myset1;GridView2.DataSource=myset2;GridView3.DataSource=myset3;}實例 觸發(fā)器的應(yīng)252-252-19.5.419.5.5252-252-在SQLSrever中,觸發(fā)器分為兩種,分別為AfterInsteadOf觸發(fā)器。After觸發(fā)器stringsqlstr="insertinto觸發(fā)器應(yīng)用表2(學 ,語文成績,英語成績,數(shù)學成績,歷史績)values('"gnamegspecgtypegpricenb252-“Default.aspx項中想主頁面添加table控件。stringconstr="server=(local);userid=sa;pwd=;DataBase=snear";protectedvoidPage_Load(objectsender,EventArgse){if(Page.IsPostBack==stringconstr="server=(local);userid=sa;pwd=;DataBase=snear";protectedvoidPage_Load(objectsender,EventArgse){if(Page.IsPostBack=={SqlConnectionmyconnection=newSqlDataAdaptermyadapter1newSqlDataAdapter("select*from觸發(fā)器應(yīng)2",SqlDataAdaptermyadapter2newSqlDataAdapter("select*from觸發(fā)器應(yīng)1",DataSetmyset1=newDataSet();DataSetmyset2=newDataSet();GridView1.DataSource=myset1;GridView2.DataSource=myset2;}}((3)5個TextBox控件和Button控件置于able控件中,并在Button的 protectedvoidButton1_Click(objectsender,EventArgs{stringgname=this.TextBox1.Text;stringgspec=this.TextBox2.Text;stringgtype=this.TextBox3.Text;stringgprice=this.TextBox4.Text;stringnb=this.TextBox5.Text;SqlConnectionmyconnection=newstringsqlstr="insertinto觸發(fā)器應(yīng)用表2(學生 ,語文成績,英語成績,數(shù)學成績,歷成績)values('"+gname+"','"+gspec+"','"+gtype+"','"+gprice+"','"+nb+"')";mandcmd=new mand(sqlstr,myconnection);252-252-}的損失,下面幾個實例介紹在ASP.NET中用代碼備份及恢復(fù)數(shù)據(jù)庫。實例 附加數(shù)據(jù)253-253-就會把選擇的數(shù)據(jù)庫文件和日志文件附加到本地SQLServer19.6.1所示。19.6.1附加數(shù)據(jù)253-253-253-253-stringaddSql="EXECsp_attach_db@dbname=N'pubs',@filename1=N'"+strPath+"',@filename2=N'"+sttPath+"'";新建一個標準,創(chuàng)建一個新窗體,默認名為“Default.aspx在工具箱HTML選項中想主頁面添加table控件。2TextBox控件、2FileUpload1Buttontable設(shè)計代碼前,需要先名命空間using ;using;在Button控件的Click中編寫執(zhí)行附加數(shù)據(jù)庫的操作代碼,代碼如下所示253-253-protectedvoidButton1_Click(objectsender,EventArgs{stringstrPath=FileUpload1.FileName;stringsttPath=stringConstr="server=(local);userid=sa;pwd=";SqlConnectioncon=newSqlConnection(Constr);stringaddSql="EXECsp_attach_db@dbname=N'pubs',@filename1=N'"+strPath+"',@filename2=N'"+sttPath+"'";mandcmd= mand(addSql,{}{}{}實例 備份數(shù)據(jù)254-254-19.6.2所示。19.6.2備份數(shù)據(jù)254-254-在SQLServer中,提供了BackupDatabase BACKUPDATABASE數(shù)據(jù)庫名TO備份設(shè)備 [WITH[NAME=’[WITH[NAME=’備份的名稱backupSql="backupdatabase"+DropDownList1.Text+"todisk='"++254-新建一個標準,創(chuàng)建一個新窗體,默認名為“Default.aspx在工具箱HTML選項中想主頁面添加table控件。1個TextBox控件、1個DropDownList1個Button控件添加到table控件中。設(shè)計代碼前,需要先命名空間usingSystem.Data.Sql;protectedvoidPageLoad(objectsender,EventArgs{ifprotectedvoidPageLoad(objectsender,EventArgs{if{SqlConnectionmyconn=newSqlConnection("server=(local);usermand=mand("sphelpdb",SqlDataReaderdr DropDownList1.DataSource=dr;DropDownList1.DataTextField="name";}}(5)在Button控件 protectedvoidButton1_Click(objectsender,EventArgs{stringpathTextBox1.Text;獲得備份路徑及數(shù)據(jù)庫名稱stringdbname=DropDownList1.SelectedValue;stringbackupSql"usesnear";//生成備份SQL語backupSql="backupdatabase"+DropDownList1.Text+"todisk='"+TextBox1.Text+"'";SqlConnectionmyconnnn=newSqlConnection("server=(local);usermandcom= mand(backupSql,{254-254-Label1.Text備份成功";}catch(Exception{Label1.Text備份失敗}{}}protectedvoidDropDownList1_SelectedIndexChanged(objectsender,EventArgs{TextBox1.Text=@"C:\BACKUP\"+DropDownList1.SelectedValue+}實例 恢復(fù)數(shù)據(jù)256-256-SQLServer中提供的RESTORE語句還本實例中應(yīng)用到RESTORE restoreSql="usemasterrestoredatabasesnearfromdisk='"+path+ 256-256-新建一個標準,創(chuàng)建一個新窗體,默認名為“Default.aspx在工具箱HTML選項中想主頁面添加table控件。1TextBox控件、1FileUpload1Button控件添加到table控件中。設(shè)計代碼前,需要先命名空間usingSystem.Data.Sql;256-protectedvoidButton1_Click(objectsender,EventArgs{256-protectedvoidButton1_Click(objectsender,EventArgs{stringpathFileUpload1.FileName;獲得備份路徑及數(shù)據(jù)庫名稱stringdbname=DropDownList1.SelectedValue;stringrestoreSql;//"usemaster";//生成備份SQL語restoreSql="usemasterrestoredatabasesnearfromdisk='"+path+SqlConnectionmycon=newSqlConnection("server=(local);userid=sa;pwd=;");mandcom mand(restoreSql,mycon);//生{Label1.Text="恢復(fù)成功";}catch(Exception{Label1.Text恢復(fù)失敗ex.ToString();//異常處}{}}實例 新建數(shù)據(jù)庫和257-257-鈕后,程序就會在本地SQLServer上新建一個數(shù)據(jù)庫,建完數(shù)據(jù)庫的同時,下拉列表控件中會顯19.7.1所示。19.7.1新建數(shù)據(jù)庫個257-257-SQLServersp_helpdb語句、CreateDataBaseCreateTable語句。3個語句的意思是: [EXECUTE]sp_helpdb[數(shù)據(jù)庫名 ”可縮寫成“EXECsp_helpdb語句的代碼如下: mand= mand("sp_helpdb", CreateDataBase語句:創(chuàng)建一個新數(shù)據(jù)庫及該數(shù)據(jù)庫的文件,或從先前創(chuàng)建的數(shù) = mand("createdatabase"+TextBox1.Text+"",=mand("use=mand("use"+DropDownList1.Text+"createtable"TextBox2.Text+"(idvarchar(4))",257-新建一個標準,創(chuàng)建一個新窗體,默認名為“Default.aspx在工具箱HTML選項中想主頁面添加table控件。table控件。分別將Button控件的Text首先在Page_Load中編寫代碼,將有關(guān)指定數(shù)據(jù)庫或所有數(shù)據(jù)庫的信息綁定protectedvoidPage_Load(objectsender,EventArgs{if{SqlConnectionmyconn=newSqlConnection("server=(local);userprotectedvoidPage_Load(objectsender,EventArgs{if{SqlConnectionmyconn=newSqlConnection("server=(local);usermand=mand("sp_helpdb",SqlDataReaderdr DropDownList1.DataSource=dr;DropDownList1.DataTextField="name";}}protectedvoidButton1Click(objectsender,EventArgs{if(TextBox1.Text=={}{SqlConnectionmycon=new = mand("createdatabase"+TextBox1.Text"",SqlDataReaderdd= }{Response.Write("<script>alert('庫名已存在}SqlConnectionmycon2=new2=mand("sphelpd
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度演員廣告代言合同
- 2025年度醫(yī)療機構(gòu)藥品采購委托代購合同
- 農(nóng)業(yè)綠色發(fā)展行動計劃
- 養(yǎng)老院合同協(xié)議書
- 用戶體驗設(shè)計原則及實踐
- 簡易買賣合同
- 云計算在企業(yè)資源規(guī)劃中的應(yīng)用
- 三農(nóng)產(chǎn)品追溯系統(tǒng)建設(shè)方案
- 模具設(shè)計與制造技術(shù)作業(yè)指導(dǎo)書
- 建房勞務(wù)人工的合同
- 數(shù)學-河南省三門峽市2024-2025學年高二上學期1月期末調(diào)研考試試題和答案
- 二零二五版電力設(shè)施維修保養(yǎng)合同協(xié)議3篇
- 最經(jīng)典凈水廠施工組織設(shè)計
- VDA6.3過程審核報告
- 2025年春新人教版數(shù)學七年級下冊教學課件
- 《心臟血管的解剖》課件
- 心肺復(fù)蘇課件2024
- 2024-2030年中國并購基金行業(yè)發(fā)展前景預(yù)測及投資策略研究報告
- 河道清淤安全培訓(xùn)課件
- 2024年湖南商務(wù)職業(yè)技術(shù)學院單招職業(yè)適應(yīng)性測試題庫帶答案
- 7.3.1印度(第1課時)七年級地理下冊(人教版)
評論
0/150
提交評論