采用asp连接到SQL Server 2008数据库 [英] Connecting to SQL Server 2008 database using asp

查看:171
本文介绍了采用asp连接到SQL Server 2008数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多,但我不能够解决这个问题。我最后2天没有浪费任何显著的结果。希望我会得到一些帮助在这里。

我想连接到使用ASP一个SQL Server 2008数据库。我安装了SQL Server 2008中,创建一个数据库,但我不能够连接到使用ASP code该数据库。我可以看到在Visual Web Developer中的数据库,我也可以通过使用asp.net Visual Web Developer中的附加连接向导进行连接,但我不希望使用添加连接向导。我想写我的ASP code连接到SQL Server数据库在记事本中。我的IIS是工作,我能够运行ASP code来访问其它数据库,如MS Access数据库,但我不能访问SQL Server数据库。

SQL Server Management Studio中的对象资源管理器显示:

 本地主机\\ SQLSERVER3(SQL服务器10.0.1600-RAJ-PC \\拉吉)

数据库

 例子
  表
   系统表
      dbo.cars
      列
          ID(PK,INT NOT NULL)
          名字(VARCHAR(50),NULL)

您可以看到所附jpg图片中的SQL Server及其数据库。我想连接到数据库实例,然后要访问汽车表。

请帮我。

更新时间:

这是我的code:

 < HTML和GT;
< HEAD>
<标题>&留言LT; /标题>
< /头><车身的bgcolor =白文本=黑>
<%
维变量
昏暗adoCon保存数据库连接对象
昏暗rsGuestbook举行在数据库中的记录记录
昏暗的STRSQL举行数据库的SQL查询'创建一个ADO连接odject
设置adoCon =的Server.CreateObject(ADODB.Connection)使用DSN的连接设置为连接对象的有效连接
adoCon.OpenODBC;驱动程序= {SQL本机客户端}; &安培; _
           服务器=本地主机\\ SQLSERVER3; &安培; _
           数据库=范例; &安培; _
           UID =拉吉; &安培; _
           PWD = love1987使用DSN连接设置为连接对象的有效连接
adoCon.OpenDSN =留言板'创建一个ADO Recordset对象
设置rsGuestbook =的Server.CreateObject(ADODB.Recordset)初始化的变量STRSQL用SQL语句来查询数据库
STRSQL =选择的名字从dbo.cars;使用SQL查询打开记录
rsGuestbook.Open STRSQL,adoCon通过记录'循环
这样做虽然没有rsGuestbook.EOF
    写HTML在记录中显示当前记录
    的Response.Write(< BR>中)
    的Response.Write(rsGuestbook(姓名))
    '的Response.Write(< BR>中)
    回复于(rsGuestbook(意见))
    '的Response.Write(< BR>中)    移动到下一个记录在记录
    rsGuestbook.MoveNext
循环重置服务器对象
rsGuestbook.Close设置rsGuestbook =什么
设置adoCon =什么
%GT;< /身体GT;
< / HTML>


解决方案

我可以使用下面的连接字符串我的本地dev的机器连接(与SQL 2008 R2前preSS):

 驱动器= {SQL服务器};服务器主机名= \\实例;数据库= DBNAME; UID =用户; PWD =密码

一件事,我在你的code注意到:你正在试图建立一个DSN-less连接,然后运行就可以了查询,而 USE数据库名或任何东西。这的可以的是问题,或至少的的问题。

I tried a lot but I am not able to deal with this problem. I wasted last 2 days without any significant result. Hope I will get some help here.

I wanted to connect to a SQL Server 2008 database using ASP. I installed SQL Server 2008, created a database, but I am not able to connect to that database using the asp code. I can see the database in Visual Web Developer, I can also connect it through asp.net using the add connection wizard of visual web developer but I don't want to use add connection wizard. I want to write my asp code to connect to SQL Server database in notepad. My IIS is working and I am able to run asp code to access other database like ms access database but I can't access SQL Server db.

The object explorer of SQL Server Management Studio shows:

localhost\SQLSERVER3(SQL Server 10.0.1600-RAJ-PC\raj)

Databases

examples
  tables
   System Tables
      dbo.cars
      columns
          id(PK,int,not null)
          name(varchar(50),null)

You can see the SQL Server and its databases in the attached jpg image. I want to connect to example database and then want to access cars table.

Please help me.

UPDATED

here is my code :

<html>
<head>
<title>Guestbook</title>
</head>

<body bgcolor="white" text="black">
<%
'Dimension variables
Dim adoCon          'Holds the Database Connection Object
Dim rsGuestbook         'Holds the recordset for the records in the database
Dim strSQL          'Holds the SQL query for the database

'Create an ADO connection odject
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "ODBC;Driver={SQL Native Client};" & _
           "Server=localhost\SQLSERVER3;" & _
           "Database=examples;" & _
           "Uid=raj;" & _
           "Pwd=love1987"

'Set an active connection to the Connection object using DSN connection
'adoCon.Open "DSN=guestbook"

'Create an ADO recordset object
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT name FROM dbo.cars;"

'Open the recordset with the SQL query 
rsGuestbook.Open strSQL, adoCon

'Loop through the recordset
Do While not rsGuestbook.EOF
    'Write the HTML to display the current record in the recordset
    Response.Write ("<br>")
    Response.Write (rsGuestbook("Name"))
    'Response.Write ("<br>")
    'Response.Write (rsGuestbook("Comments"))
    'Response.Write ("<br>")

    'Move to the next record in the recordset
    rsGuestbook.MoveNext
Loop

'Reset server objects
rsGuestbook.Close

Set rsGuestbook = Nothing
Set adoCon = Nothing
%>

</body>
</html>

解决方案

I am able to connect using the following connection string on my local dev machine (with SQL 2008 R2 Express):

Driver={SQL Server}; Server=hostname\instancename; Database=dbname; Uid=user; Pwd=password

One thing I noticed in your code: you are trying to establish a DSN-less connection, then you run a query on it without USE dbname or anything. That may be the issue, or a least an issue.

这篇关于采用asp连接到SQL Server 2008数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆