2011-09-29 105 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