96weibin

使用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 为百分数  显示产寻结果的百分之多少

 

 

 

 

 

 

 

 

 

分类:

技术点:

相关文章:

  • 2021-08-08
  • 2021-11-28
  • 2022-12-23
  • 2021-11-23
  • 2021-11-27
  • 2021-08-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
  • 2021-12-29
  • 2021-11-15
  • 2021-07-17
  • 2021-12-13
相关资源
相似解决方案