㈠ 如何用ASP连接SQLSERVER数据库
思路:
1、ASP连接SQL数据库,连接字符串是关键
2、连接后一般是执行查询语句获得结果集,或者是执行SQL语句,不需要返回结果
3、关闭数据库连接对象
示例代码:
dimConn,strConn,rs,strSQL
strConn="Provider=SQLOLEDB;DataSource=127.0.0.1;UserID=Test;Password=Test;InitialCatalog=Test_DBName"
SetConn=Server.CreateObject("ADODB.Connection")
Conn.openstrConn
iferr.number<>0then
response.Write"数据库链接失败。"&err.Description
response.End
endif
setrs=server.CreateObject("adodb.recordset")
strSQL="selecttitlefromdbo.tb_e_table"
rs.openstrSQL,conn,1,1
IfNotrs.eofThen
'结果集存在,则可输出rs
response.writers("title")
EndIf
rs.close
Setrs=Nothing
Conn.close
SetConn=Nothing
其中
【Provider=SQLOLEDB;】:是声明使用的是MSSQL协议,
【Data Source=127.0.0.1;】:是数据库的所在服务器IP地址或计算机名称,
【User ID=Test;】:要连接的数据库用户名,
【Password=Test;】:要连接的数据库用户密码,
【Initial Catalog=Test_DBName】:要连接的数据库名称