asp對資料庫的基本操作

來源:互聯網
上載者:User

一、添加記錄

方法1:Connection對象的Execute方法

set con=Server.CreateObject("ADODB.Connection")
insertstr="insert into 表名(欄位1,欄位2,...) values(value1,value2,...)  '插入記錄SQL語句
con.Open constr
con.Execute(insertstr)
con.close
set con=nothing
-------------------------------------------------------------------------------------------
方法二:RecordSet對象的AddNew方法

set con=Server.CreateObject("ADODB.Connection")
set rs =Server.CreateObject("ADODB.RecordSet")
querystr="select * from 表名"                '查詢記錄SQL語句
con.open constr
rs.open querystr,con,1,3

rs.AddNew
rs("欄位1")=value1
rs("欄位2")=value2
......
rs.Update

rs.close
set rs=nothing
con.close
set con=nothing

 

二、查詢記錄

set con=Server.CreateObject("ADODB.Connection")
set rs =Server.CreateObject("ADODB.RecordSet")
querystr="select 欄位 from 表名 where ...."             '查詢記錄SQL語句
con.open constr
rs.open querystr,con,1,1

do while not rs.EOF do
'讀取資料
  變數1=rs("欄位1")
  變數2=rs("欄位2")
  ...
loop

rs.close
set rs=nothing
con.close
set con=nothing

 三、修改記錄

方法一:Connection對象的Execute方法

 set con=Server.CreateObject("ADODB.Connection")
 con.open constr
 updatestr="update 表名 set 欄位1=value1,欄位2=value2 where id=..."   '更新記錄SQL語句
 con.Execute(updatestr)
 con.close
 set con=nothing
----------------------------------------------------------------------
方法二:RecordSet對象的Update方法
 
 set con=Server.CreateObject("ADODB.Connection")
 set rs= Server.CreateObject("ADODB.RecordSet")
 querystr="select 欄位 from 表名 where ...."             '查詢記錄SQL語句
 con.open constr    
 rs.open  querystr,con,1,3
 rs("欄位1")=value1
 rs("欄位2")=value2
 ...
 rs.Update

 rs.close
 set rs=nothing
 con.close
 set con=nothing

 

四、刪除記錄

set con=Server.CreateObject("ADODB.Connection")
deletestr="delete from 表名 where id="&id          '刪除記錄SQL語句
con.open constr
con.Execute(deletestr)
con.close
set con=nothing

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.