asp中如何读取sqlserver数据库中的数据

2024-12-19 11:39:36
推荐回答(3个)
回答1:

<%
      SET  Conn=Server.CreateObject("ADODB.Connection")
      Dim i,j,Sql
       Set rt=Server.CreateObject("ADODB.Recordset")
      'Conn.Open  "Server=IP地址;Provider=sqloledb;Database=库名称;UID=用户名;PWD=密码;"
      Conn.Open  "Server=192.168.1.1;Provider=sqloledb;Database=mysql;UID=sa;PWD=mysql;"
 %>

   <%' 读SqlServer库中的表名:
      Set  rs=Conn.OpenSchema(20)
      While not rs.EOF
      if rs(3)="TABLE" then
         'response.write(rs(2)&"
")'表的类型
         'Response.Write("所在数据库名:" &  rs(0) )
         'Response.Write("所有者:" &  rs(1)  )
         'Response.Write("表 名:" &  rs(2) &"
" )
       %>
    
    <%=rs(2)%>    
      <%    
      '这样,知道表名了,现在再来看看怎么对表的字段进行操作。假设:其中数据库中有表:[admin] 获取该表的所有字段名:
       x="[admin]"
       rt.open"select * from "&rs(2)&" where 1<>1",conn,1,3
       j=rt.Fields.count
       For i=0 to (j-1)
      
       'Response.Write("第" & i+1 & "个字段名:" & rt.Fields(i).Name & "

")
       response.write(""&rt(i).name)
       Next
       rt.close
      
      end if
      rs.MoveNext
      Wend 
     %>
    
     

回答2:

dim conn,connstr,rs,sql
set conn = Server.CreateObject("Adodb.Connection")
connstr = "Provider=SQLOLEDB;data source=127.0.0.1;initial catalog=DBname;uid=username;pwd=password"
conn.open connstr
set rs = Server.CreateObject("Adodb.RecordSet")
sql = "select id from table"
rs.open sql,conn,1,1
if not rs.eof
response.write "ID为:" & rs("id")
end if
rs.close
conn.close
set conn=nothing

回答3:

想学就从头学,想用就花钱买,不想学就别学
100元