且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

采用asp连接到SQL Server 2008数据库

更新时间:2023-02-22 13:57:21

我可以使用下面的连接字符串我的本地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.