国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET 中數據庫操作初步

2024-07-10 13:11:14
字體:
來源:轉載
供稿:網友
  一、定義oledbcommand類型變量:mycommand
  
  要對數據庫進行增加、刪除、修改的操作我們還需要根據myconnectio的類型定義一個oledbcommand或者sqlcommand對象(請注意如果myconnection是oledbconnection類型,那么只能用oledbcommand;如果myconnection是sqlconnection類型,那么那么只能用sqlcommand。這里假設myconnection是oledbconnection類)。
  
  方法一
  你可以象拖放myconnection一樣拖放一個oledbcommand,并命名為 mycommand。
  方法二
  在(關聯文件).cs文件中protected system.data.oledb.oledbconnection myconnection;下面手動添加:
  protected system.data.oledb.oledbcommand mycommand;在
  private void initializecomponent()中
  this.myconnection =newsystem.data.oledb.oledbconnection();的下一行下面手動添加:
  this.mycommand = new system.data.oledb.oledbcommand();即可完成對mycommand的定義
  說明:mycommand的作用是用來執行sql命令
  
  二、利用定義的myconnectio和mycommand對數據庫進行增加、刪除、修改
  
  首先我們需要連接并打開一個數據庫(關于數據庫的連接和打開的操作請察看我們以前的文章)。
  打開數據庫:
  myconnectio.open();然后我們需要給mycommand指定要執行的sql命令 :
  mycommand.commandtext = "delete from admin";接著我們需要給mycommand指定數據源(對那個數據庫執行sql命令):
  mycommand.connection = myconnection;然后我們執行mycommand命令即可:
  mycommand. executenonquery();如果我們在執行還有
  "delete from admin";后需要接著執行
  “insert into admin (admin_code,admin_pwd) values(‘aa’,’bb’)”,則我們只要再次指定mycommand指定要執行的sql命令 :
  mycommand.commandtext =“insert into admin (admin_code,admin_pwd) values(‘aa’,’bb’)”,然后執行mycommand. executenonquery();即可。(由于數據庫未關閉,所以我們不需要也不可以再次myconnectio.open();,同理由于沒有改變mycommand的數據源所以我們也沒有必要再次指定mycommand.connection = myconnection;)
  
  下面我們將詳細講解如何在page_load()中對數據庫的增加、刪除、修改,最后我們再來總結一下executenonquery(),executescalar(),executereader的用法
  
  1、 增加新的記錄
  private void page_load(object sender, system.eventargs e)
  {
  myconnection.open();’打開數據庫
  mycommand1.commandtext = "insert into admin values(‘aaddq‘,‘as‘,‘ss‘)";
  mycommand1.connection = myconnection;
  mycommand1.executenonquery();’由于增加了一條記錄,所以返回1
  //或者mycommand1.executereader();先增加一條記錄,然后返回一個system.data.oledb.oledbdatareader類型的對象,該對象為:eof
  //或者mycommand1. executescalar();先增加一條記錄,返回未實列化的對象
  myconnection.close();
  }2、 刪除現有數據
  private void page_load(object sender, system.eventargs e)
  {
  myconnection.open();’打開數據庫
  mycommand1.commandtext = "delete * from admin";
  mycommand1.connection = myconnection;
  mycommand1.executenonquery();’由于刪除了n條記錄,所以返回n
  //或者mycommand1.executereader();先刪除n條記錄,然后返回一個system.data.oledb.oledbdatareader類型的對象,該對象為:eof
  //或者mycommand1. executescalar();先刪除n條記錄,返回未實列化的對象
  myconnection.close();
  }
  3、 修改現有數據
  private void page_load(object sender, system.eventargs e)
  {
  myconnection.open();’打開數據庫
  mycommand1.commandtext = "update admin set admin_code=’212’,admin_pwd=’43’ where admin_code=’23’";
  mycommand1.connection = myconnection;
  mycommand1.executenonquery();’由于修改了1條記錄,所以返回n
  //或者mycommand1.executereader();先修改了1條記錄,然后返回一個system.data.oledb.oledbdatareader類型的對象,該對象為:eof
  //或者mycommand1. executescalar();先修改了1條記錄,返回未實列化的對象
  myconnection.close();
  }
  
  三、關于mycommand的executenonquery(),executescalar(),executereader方法的區別:
  
  1、executenonquery():執行sql,返回一個整型變量,如果sql是對數據庫的記錄進行操作,那么返回操作影響的記錄條數,如果是
  sql="create table lookupcodes (code_id smallint identity(1,1) primary key clustered, code_desc varchar(50) not null)"那么在表創建成功后該方法返回 –1。
  例如:
  private void page_load(object sender, system.eventargs e)
  {
  myconnection.open();’打開數據庫
  mycommand1.commandtext = "create table lookupcodes (code_id smallint identity(1,1) primary key clustered, code_desc varchar(50) not null)"; mycommand1.connection = myconnection;
  mycommand1.executenonquery();’首先建立一個lookupcodes表,然后返回-1
  //或者mycommand1.executereader();首先建立一個lookupcodes表,然后返回一個system.data.oledb.oledbdatareader類型的對象,該對象為:eof
  //或者mycommand1. executescalar();首先建立一個lookupcodes表,返回未實列化的對象
  myconnection.close();
  }
  2、 executescalar():執行sql,(如果sql是查詢select)返回查詢結果的第一行第一列,如果(如果sql不是查詢select)那么返回未實列化的對象,因為對象未實列化,所以返回結果不能tostring(),不能equals(null),也就是說返回結果沒有任何作用
  
  3、 executereader方法執行sql,(如果sql是查詢select)返回查詢結果的集合,類型是system.data.oledb.oledbdatareader,你可以通過此結果,獲取查詢的數據。如果(如果sql不是查詢select)那么返回一個沒有任何數據的system.data.oledb.oledbdatareader類型的集合(eof)
  
  四、總結:
  
  asp.net中對于數據庫的操作方法很多,要實現統一個目標不同的人可能會采取不同的方法,就好像在asp中有的人喜歡用rs.addnew,有的人喜歡用”insert into”,主要是看個人的習慣,當然在性能上不同的方法可能會存在較大的差別,這個只能靠我們在平常的學習中一點一滴的積累經驗的。另外順便說一下asp.net頁提供類似如下方式的操作方法:
  
  oledbcommand2.parameters("au_id").value = textbox1.text
  oledbcommand2.parameters("au_lname").value = textbox2.text
  oledbcommand2.parameters("au_fname").value = textbox3.text
  oledbcommand2.parameters("phone").value = textbox4.text
  oledbcommand2.parameters("address").value = textbox5.text
  oledbcommand2.parameters("city").value = textbox6.text
  oledbcommand2.parameters("st").value = textbox7.text
  oledbcommand2.parameters("zip").value = textbox8.text
  oledbcommand2.parameters("contract").value = checkbox1.checked
  cmdresults = oledbcommand2.executenonquery()
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐东| 天峨县| 开封市| 和政县| 淮阳县| 宜宾县| 山东省| 乐业县| 华坪县| 九江县| 房山区| 嵩明县| 鄂托克前旗| 高阳县| 九江市| 民县| 石家庄市| 安陆市| 泾阳县| 保定市| 林西县| 屏山县| 美姑县| 和林格尔县| 普格县| 鄄城县| 北辰区| 吴忠市| 新邵县| 杨浦区| 桐乡市| 临夏县| 章丘市| 南郑县| 轮台县| 蒲城县| 鄂州市| 泗阳县| 永嘉县| 宣武区| 裕民县|