ASP和SQL資料庫聯

2022-10-10 17:51:08 字數 1939 閱讀 1616

現在asp和sql資料庫聯絡緊密,包括access資料庫,資料庫的執行效率直接關係著你系統的執行質量,因此優化好你的資料庫,是很有必要的,下面介紹幾種sqlserver/access優化方法,對使用asp的朋友,或許會有不小幫助。

方法一:要使用復合sql查詢,不要使用單一堆徹的大量簡單sql,因為乙個複雜的sql完成的效率高於一堆簡單sql的效率。有多個查詢時,要擅長使用join。

以下查詢是不效率的:

ors=oconn.execute("select * from books")

while not ors.eof

strsql = "select * from authors where authorid="&ors("authorid") ors2=oconn.execute(strsql)

response.write ors("title")&">>"&ors2("name")&"

&q uot;

ors.movenext()

wend

下面這段**就要比上面的效率高多了:

strsql="select books.title,authors.name from books join authors on authors.authorid=books.authorid"

ors=oconn.execute(strsql)

while not ors.eof

response.write ors("title")&">>"&ors("name")&"

&qu ot;

ors.movenext()

wend

方法二:不到萬不得已,不要使用可更新的recordset

ors=oconn.execute("select * from authors where authorid=17",3,3)

ors("name")="darkman"

ors.update()

上面的**要比下面的**慢:

strsql = "update authors set name='darkman' where authorid=17"

oconn.execute strsql

方法三:更新資料庫時,盡量使用批處理更新,也就是把sql組成乙個大的批處理sql,一次執行;要比乙個個更新好得多:

strsql=""

strsql=strsql&"set xact_abort on\n";

strsql=strsql&"begin transaction\n";

strsql=strsql&"insert into orders(ordid,custid,orddat) values('9999','1234',getdate())\n";

strsql=strsql&"insert into orderrows(ordid,ordrow,item,qty) values('9999','01','g4385',5)\n";

strsql=strsql&"insert into orderrows(ordid,ordrow,item,qty) values('9999','02','g4726',1)\n";

strsql=strsql&"commit transaction\n";

strsql=strsql&"set xact_abort off\n";

oconn.execute(strsql);

其中,set xact_abort off 語句告訴sql server,如果事務處理過程中遇到錯誤,就取消已經完成的事務。

方法四:避免text欄位太大。當字串的值大小不固定時,用varchar比用char的效果要好 些。我曾經看到乙個例子程式,欄位被定義為text(255),但是他的取值經常只有20個字元。這個資料表有50k個記錄,從而使這個資料庫很大,大的資料庫必然較慢。

方法五:資料庫索引。需要在where子句出現的字段,最好建立索引;需要排序的字段,也應該這樣做。

asp連線sql資料庫,access資料庫字串

asp連線sqlserver資料庫字串 set connsql server.createobject adodb.connection strsql provider sqloledb.1 password y ht1986 persist security info true user id s...

ASP基礎資料庫

選擇分類 class this.value 所有分類 sql select id,c name,c code from web news class where mn id getvariable mnid order by c code set rsclass conn.execute sql d...

ASP資料庫連線

一 asp的物件訪問資料庫方法 在asp中,用來訪問資料庫的物件統稱ado active data objects 主要含有三種物件 connection recordset command connection 負責開啟或連線資料 recordset 負責訪問資料表 command 負責對資料庫執...