2012-09-26 75 views
0

我嘗試連接asp.net中的oracle數據庫。我想要處理與SqlDataSource的連接。當我使用下面的代碼,我得到以下錯誤:Gridview的Oracle連接問題

說明:在當前Web請求的執行過程中發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

Exception Details: System.ComponentModel.Win32Exception: The system can not find the file specified.

Source Error:

[Win32Exception (0x80004005): The system can not find the file specified.]

[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]

這裏是aspx文件:


<%@ Page Language="C#" AutoEventWireup="true" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
    <asp:SqlDataSource 
     id="SqlDataSource1" 
     runat="server" 
     DataSourceMode="DataReader"    
     SelectCommand="SELECT * FROM PERSON " 
     ConnectionString="<%$ ConnectionStrings:OracleConnectionString %>"> 
    </asp:SqlDataSource> 


    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" BackColor="WindowFrame" AllowSorting="true" AllowPaging="true"> 

     <Columns> 
      <asp:BoundField HeaderText="Numarası" DataField="ID" /> 
      <asp:BoundField HeaderText="Adı" DataField="NAME" /> 
      <asp:BoundField HeaderText="Soyadı" DataField="SURNAME" /> 

     </Columns> 

    </asp:GridView> 

    <br /> 

</div>  
</form> 


但我可以直接連接數據庫,除了我可以用另一個代碼文件將其連接,用程序如下:

private void ReadOracleTypesExample(string connectionString) 
    { 
     OracleConnection connection = new OracleConnection(connectionString); 
     connection.Open(); 
     OracleCommand command = connection.CreateCommand(); 
     try 
     { 
      command.CommandText = "SELECT * FROM PERSON"; 
      OracleDataReader reader = command.ExecuteReader(); 


     if (reader.HasRows) 
     { 
      while (reader.Read()) 
      { 

       OracleString oraclesring2 = reader.GetOracleString(1); 
       Label2.Text += ("<br />" + oraclesring2.ToString()); 

       OracleString oraclestring3 = reader.GetOracleString(2); 
       Label3.Text += ("<br /> " + oraclestring3.ToString()); 
      } 
     } 
     else 
     { 
      Console.WriteLine("No rows found."); 
     } 
     reader.Close(); 

    } 
    catch (Exception e) 
    { 
     Label1.Text = e.Message; 
    } 
    finally 
    { 
     connection.Close(); 
    } 
} 


Oracle數據庫是由C#以外的工具進行訪問。我無法想象爲什麼我無法連接到數據庫,我會跑掉鐵軌。提前致謝。

+0

嘗試設置你的SqlDataSource的ProviderName屬性。默認是SQL Server。請參閱http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.providername.aspx – GTG

回答

0

爲了使用SqlDataSource控件連接到Oracle數據庫,您必須配置的SqlDataSource。

1從工具箱的數據選項卡,將一個SqlDataSource控件到你的頁面。

2如果SqlDataSource的任務智能標記沒有出現,請右鍵單擊該控件,然後單擊顯示智能標記。

3在SqlDataSource的任務列表中,單擊配置數據源。

4在「配置數據源」對話框中,單擊「新建連接」。

5顯示選擇數據源對話框。

6在數據源列表中,單擊Oracle數據庫,然後單擊繼續。

7將顯示「添加連接」對話框。

8在「服務器名稱」框中,鍵入Oracle服務器的名稱。

9輸入連接數據庫的用戶名和密碼。

10確保選中是,保存此連接複選框,如果需要,更改連接字符串的名稱,然後單擊下一步以將連接字符串信息保存在應用程序的Web.config文件中。

11出現配置選擇語句窗格。

12單擊下一步。

13點擊測試查詢按鈕來測試你對數據庫的查詢,然後單擊Finish(完成)。

+0

感謝您的快速響應。儘管我在源代碼樣式中編輯了必要的修改,但似乎我的編輯並不足夠。visual studio 2012所做的更改爲:在web.config中: tahasozgen

+0

我很樂意幫助你Varoluscu在這裏你有不同的字符串連接格式:http://www.connectionstrings.com/ –

+0

我的第一個連接字符串是從那裏得到的,但它沒有像上面看到的那樣工作。 – tahasozgen

0

[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]

看起來你的連接字符串有問題。您的頁面無法連接到數據庫。

我用下面的連接字符串:

add name="ConnectionString" connectionString="Data Source=localhost;Password=123456;User ID=TAHA";Unicode=True" providerName="System.Data.OracleClient" />