使用vbScript 鏈接SQLserver數據庫和基礎操作


使用vbs鏈接SQLserver數據庫

  數據庫的創建、設計使用 management studio完成

1.本地鏈接數據庫

set oCon = server.createObject("adodb.connection") 
'創建connection對象

oCon.connectionString = "dirver={sql server}; server=PC1866\WEIBINDB;uid=96weiBin;pwd=96weiBin;dataBase=96weiBin"
'利用connection對象的connectionString屬性 來定義  連接數據庫的參數
'參數 dirver固定為 {sql server}; server是服務器名稱 uid、pwd、dataBase、就可以了
'也可以 定義一個 以key:value; 組成的 連接參數字符串str 再通過 oCon.open str 來連接 oCon.open '連接數據庫
  其中幾個參數
dirver    固定的是 SQL Server
Server     是你的 服務器名稱 可通過 SQLServer可視化工具查看
uid,psw      是你的登錄數據庫用戶的用戶名,密碼
dataBase   是你要打開的數據庫名
 
2.判斷數據庫是否連接成功
set oCon = server.createObject("adodb.connection")
oCon.connectionString = "driver={sql server}; server=PC1866\WEIBINDB;uid=96weiBin;pwd=96weiBin;dataBase=96weiBin"

response.write(oCon.state&"<br>")'未open時 connection 對象的狀態 0


oCon.open

response.write(oCon.state&"<br>")'open后 connection 對象的狀態 1

oCon.close

response.write(oCon.state&"<br>")'close后 connection 對象的狀態 0

3.插入數據 Insert

insert into 
<表名>[(<列名1>[,<列名2>....)]] 
values (<數據1>[,<數據2>...])


'上面偽代碼的  
'<> 是 要寫的屬性,內容為解釋
'[] 是 可選項 根據需求
oCon.exture "insert into userList (usrename, userid) values('yaoming', '1')"
'要注意   values里的值 要用單引號包裹起來

4.更新數據 Update

update <表名> set <列名> = <數據>[,<列明2> = <數據2>]
[where<條件>]


oCon.execute "update getList set sex = 'maile'  where username = 'weibin'"
'把 username是 weibin 的 sex 改為了 maile
5.刪除數據 delete
delete from <表名>
[where <條件>]

'省略 where 則全部刪除
oCon.execute "delete from getList where userid=15"
'刪除 userid 為15的數據

6.查詢數據 Select

select [all | Distinct]<目標表列達式1>[,<目標列表達式2>]
from <表名1>[,<表名>]
[where<條件表達式>]
[grop by <列名1>[having<條件表達式>]]
[order by <列名>[asc|basc]]
    1. 默認是all可設置成distinct,意思就是刪除返回中重復的數據

    2. where 條件
    
    特殊的比較運算符, 除了以下幾個其他都和js相同
        <> 或 !=       不等於
        !>             不大於
        !<             不小於
    between...and 和 not between ... and
    
        select age
        from userList
        where age between 15 and 20 
        '獲取uesrList中age 在15 - 20 的數據
    
    and 和 or連接多個條件 

    3. order by 排序     

        默認ASC 是升序     可以設置 base 為 降序

    4. 使用top 限制返回行數
        
        oCon.execute("select top 2 from userList where sex=maile")
        'top n 還可以設置 n 為百分數  顯示產尋結果的百分之多少

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM