使用vbScript 连接SQLserver数据库和基础操做

使用vbs连接SQLserver数据库sql

  数据库的建立、设计使用 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.更新数据 Updateui

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.查询数据 Selectspa

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 为百分数  显示产寻结果的百分之多少
相关文章
相关标签/搜索