我嘗試連接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#以外的工具進行訪問。我無法想象爲什麼我無法連接到數據庫,我會跑掉鐵軌。提前致謝。
嘗試設置你的SqlDataSource的ProviderName屬性。默認是SQL Server。請參閱http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.providername.aspx – GTG