【发布时间】:2011-09-29 20:54:14
【问题描述】:
我尝试了很多,但我无法处理这个问题。我浪费了最后两天,没有任何明显的结果。希望我能在这里得到一些帮助。
我想使用 ASP 连接到 SQL Server 2008 数据库。我安装了 SQL Server 2008,创建了一个数据库,但我无法使用 asp 代码连接到该数据库。我可以在 Visual Web Developer 中看到数据库,我也可以使用 Visual Web Developer 的添加连接向导通过 asp.net 连接它,但我不想使用添加连接向导。我想编写我的 asp 代码以在记事本中连接到 SQL Server 数据库。我的 IIS 正在工作,我可以运行 asp 代码来访问其他数据库,如 ms 访问数据库,但我无法访问 SQL Server db。
SQL Server Management Studio 的对象浏览器显示:
localhost\SQLSERVER3(SQL Server 10.0.1600-RAJ-PC\raj)
数据库
examples
tables
System Tables
dbo.cars
columns
id(PK,int,not null)
name(varchar(50),null)
您可以在附加的 jpg 图像中看到 SQL Server 及其数据库。我想连接到示例数据库,然后想访问汽车表。
请帮帮我。
更新
这是我的代码:
<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>
【问题讨论】:
-
C# 还是 VB?到目前为止,您的代码是什么样的?
-
2 件事。 1. 您是在寻求 ASP(经典)或 ASP.NET 方面的帮助。 2. 您是否有用于尝试连接数据库的代码示例以显示您的尝试?
-
啊,你改变了默认实例?
-
@Alan Moore:我正在使用 VB。我觉得我无法正确指定连接字符串。这是我的代码的一部分。 adoCon.Open "ODBC;Driver={SQL Native Client};" & _ "服务器=本地主机\SQLSERVER3;" &_“数据库=示例;” & _ "uid=raj;" & _ "密码=love1987"
-
@blake 我正在寻求有关 asp 的帮助,但如果您解释与 asp.net 的连接,那将非常有帮助,因为它比经典的 asp 更好。
标签: sql-server asp-classic ado