使用 ASP.NET 连接到远程 Oracle XE 数据库时出现错误
我已在我的开发计算机上安装了 Oracle XE,并且运行良好。
然后我在我的测试机上安装了 Oracle XE 客户端,它也运行良好,我可以从浏览器访问开发 PC 数据库。
现在,我想创建一个可以访问该 Oracle XE 数据库的 ASP.Net 应用程序。 我也尝试过,但在我的测试机器上使用 ASP.Net 将数据库连接到开发机器时总是显示错误。
这是我的 ASP.Net 应用程序代码:
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = GetConnectionString();
OracleConnection connection = new OracleConnection(connectionString);
connection.Open();
Label1.Text = "State: " + connection.State;
Label1.Text = "ConnectionString: " + connection.ConnectionString;
OracleCommand command = connection.CreateCommand();
string sql = "SELECT * FROM Users";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string myField = (string)reader["nID"];
Console.WriteLine(myField);
}
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "User Id=System;Password=admin;Data Source=(DESCRIPTION=" +
"(ADDRESS=(PROTOCOL=TCP)(HOST=myServerAddress)(PORT=1521))" +
"(CONNECT_DATA=(SERVICE_NAME=)));";
}
I have installed Oracle XE on my Development machine and it is working fine.
Then I installed Oracle XE client on my Test machine which is also working fine and I can access Development PC database from Browser.
Now, I want to create an ASP.Net application which can access that Oracle XE database.
I tried it too, but it always shows me an error on my TEST machine to connect database to the Development Machine using ASP.Net.
Here is my code for ASP.Net application:
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = GetConnectionString();
OracleConnection connection = new OracleConnection(connectionString);
connection.Open();
Label1.Text = "State: " + connection.State;
Label1.Text = "ConnectionString: " + connection.ConnectionString;
OracleCommand command = connection.CreateCommand();
string sql = "SELECT * FROM Users";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string myField = (string)reader["nID"];
Console.WriteLine(myField);
}
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "User Id=System;Password=admin;Data Source=(DESCRIPTION=" +
"(ADDRESS=(PROTOCOL=TCP)(HOST=myServerAddress)(PORT=1521))" +
"(CONNECT_DATA=(SERVICE_NAME=)));";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保 oracle 侦听器已打开。
要启动这个监听器,
你可以尝试这个
make sure the oracle listener opened.
to start this listener
you can try this one
您不需要在连接字符串中连接服务名称吗?例如,
Don't you need a service name to connect to in your connection string? E.g.,