2011-09-29 108 views
0

我尝试了很多,但我无法处理这个问题。我最后2天浪费了没有任何重要结果。希望我会在这里得到一些帮助。使用asp连接到SQL Server 2008数据库

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

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> 
+0

C#或VB?你的代码到目前为止看起来如何? –

+2

2件事。 1。您是否正在寻求ASP(经典)或ASP.NET的帮助。 2.你有没有一个你正在使用的代码样本来尝试连接到数据库以显示你已经尝试了什么? –

+0

啊,你改变了默认实例吗? – ApolloSoftware

回答

2

我能够使用我的本地dev的机器下面的连接字符串(与SQL 2008 R2快递)连接:

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

我在你的代码中注意到的一件事情是:你正试图建立一个没有DSN的连接,然后在它上面运行一个查询而没有USE dbname或任何东西。那可能是是问题,或者至少是问题。

+0

感谢你的回复。所以我应该使用Driver = {SQL Server};服务器=本地主机\ SQLSERVER3;数据库=例子; UID =拉吉; Pwd = xxxxxx ..........我在连接到我的sql服务器数据库时使用了Windows身份验证选项。所以我应该在uid和pwd字段中使用Windows用户名和密码,或者使用普通用户名'sa'和相应的密码来连接..? –

+0

我只用'sa'用户试过... – bfavaretto

+0

非常感谢好友....你解决了我的问题..我现在可以连接了...非常感谢你...只是一个更多的帮助。相同的连接字符串是否也可以与asp.net一起使用?如果不是plz,请指定asp.net中使用的连接字符串。 –

1

尝试创建一个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

+0

嗨,谢谢你的回答,但我想连接到MSSQL服务器数据库。我们可以使用微软访问驱动程序吗?是非常丰富的信息,但我需要创建一个dsn连接。 –

+0

是的,抱歉,我只是复制并粘贴了该文本,但可以为SQL Server而不是Access创建DSN。试想一下,您可以尝试DSN方法来进行故障排除,或者仅仅是为了让您现在就了解这个障碍,以便您可以专注于项目的其他部分。如果问题对于你的开发环境来说是独一无二的,那么当你转向产品时,这并不重要。 –

+0

,但DSN也只存在于其创建的机器上,所以如果您移动了asp网站,则需要在移动到的机器上创建一个新的DSN。如果您需要重新使用代码,但必须重新配置不同的数据库,这很好。 – BerggreenDK