【问题标题】:Connecting to SQL Server 2008 database using asp使用asp连接到SQL Server 2008数据库
【发布时间】: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


【解决方案1】:

我可以在本地开发机器上使用以下连接字符串进行连接(使用 SQL 2008 R2 Express):

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

我在您的代码中注意到一件事:您正在尝试建立一个无 DSN 连接,然后您在没有 USE dbname 或任何东西的情况下对其运行查询。这可能是问题,或者至少是一个问题。

【讨论】:

  • 感谢您的回复。所以我应该使用 Driver={SQL Server};服务器=本地主机\SQLSERVER3;数据库=示例; uid=raj; Pwd=xxxxxx........我在连接到我的 sql server 数据库时使用了 windows 身份验证选项。那么我应该在uid和pwd字段中使用windows用户名和密码还是使用普通用户名'sa'和相应的密码来连接..?
  • 非常感谢好友....您解决了我的问题..我现在可以连接...非常感谢...再提供一个帮助。相同的连接字符串是否也适用于 asp.net?如果不是,请指定 asp.net 中使用的连接字符串。
  • 刚刚检查为 ASP.net 项目,我使用了一个非常相似的连接字符串,只是没有 Driver={SQL Server}; 部分。但是,我不是“手动”连接到服务器,我的 CMS (DotNetNuke) 正在为我做这件事,所以我不确定。
  • 嗨,有没有办法在运行时在您指定的连接字符串中添加服务器属性的值。这意味着我为用户提供了一个文本框,他在其中输入服务器名称,并且我想在我的连接字符串中使用该服务器名称。请帮忙。
  • 当然,毕竟是字符串!在经典 ASP (vbscript) 中,您可以这样做: server=request("server_name") [line break] connstr = "Driver={SQL Server}; Server=" & server & "; Database=dbname; Uid=user; Pwd =password" [换行符] adoCon.Open connstr.但是:就服务器安全而言,您尝试做的事情听起来很危险,请注意您允许网络用户做的事情!
【解决方案2】:

尝试创建一个 DSN,然后在连接字符串中按名称引用它。

  1. 在控制面板中打开 ODBC 图标。
  2. 选择系统 DSN 选项卡。
  3. 单击“系统 DSN”选项卡中的“添加”。
  4. 选择 Microsoft Access 驱动程序。点击完成。
  5. 在下一个屏幕中,单击“选择”以定位数据库。
  6. 为数据库指定一个数据源名称 (DSN)。
  7. 点击确定。

    set conn=Server.CreateObject("ADODB.Connection")

    conn.Open "northwind"

http://www.w3schools.com/ado/ado_connect.asp

【讨论】:

  • 嗨,感谢您的回答,但我正在尝试连接到 mssql 服务器数据库。我们可以使用微软访问驱动程序来做到这一点吗?您的帖子内容非常丰富,但我需要创建一个无 dsn 的连接。
  • 是的,对不起,我只是复制并粘贴了该文本,但是可以为 SQL Server 而不是 Access 创建 DSN。只是想您可以尝试 DSN 方法进行故障排除,或者只是暂时让您克服这个障碍,以便您可以专注于项目的其他部分。如果问题是您的开发环境所特有的,那么当您迁移到 prod 时就无关紧要了。
  • 但是 DSN 也只会存在于它创建的机器上,所以如果你移动 asp 站点,你需要在你移动它的机器上创建一个新的 DSN。如果您需要重用代码但必须针对不同的数据库重新配置,这很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
相关资源
最近更新 更多