You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2022与Oracle 21c连接超时及插入测试问题求助

连接Oracle 21c时出现Connection request timeout的问题解决求助

背景

我正在提升英语水平,表述可能存在不当之处。我想通过插入操作测试Visual Studio 2022中ASP.NET Core 6项目与Oracle 21c的连接及配置是否正确。目前已安装Oracle Database Express Edition和Oracle SQL Developer,创建项目后通过服务器资源管理器验证了SYSTEM用户连接Prueba数据库的有效性,但运行代码时在connection.Open();行出现超时错误。

配置文件与代码

appsettings.json配置

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        }
    },
    "AllowedHosts": "*",
    "ConnectionStrings": {
        "SqlServerConnection": "Server=.; Database=Prueba; User ID=SYSTEM; Password=qwe123; Trust Server Certificate=true;",
        "OracleConnection": "User ID=SYSTEM; Password=qwe123; Pooling=true; Connection Timeout=120; Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));"
    }
}

HomeController.cs代码

using Microsoft.AspNetCore.Mvc;
using Oracle.ManagedDataAccess.Client;
using PruebaConexion.Models;
using System.Data.Db;
using System.Diagnostics;
using System.Xml.Serialization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Data.SqlClient;

namespace PruebaConexion.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private IConfiguration _config;

        public HomeController(ILogger<HomeController> logger, IConfiguration config)
        {
            _logger = logger;
            _config = config;
        }

        public IActionResult Index()
        {
            Prueba();
            return View();
        }

        public void Prueba()
        {
            DbConnection connection = new OracleConnection(_config["ConnectionStrings:OracleConnection"]);
            connection.Open();
            DbCommand cmd = connection.CreateCommand();
            cmd.CommandText = "insert into VEHICLE_PERSON ( EMP_NUMBER, LASTNAME, FIRSTNAME, " +
                "COSTCENTER, DEPARTMENT, CREDENT_NUMBER, NUMBER1, FROM_DATE, TO_DATE, RIGHTS1 ) " +
                "values ( 'Prueba', 'Carrasco', 'Leonardo', 'Prueba', 'Desarrollo', 'Prueba', " +
                "'Prueba', '10/10/2022', '11/11/2023', 'Prueba');";
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

额外测试代码Test2

public void Test2()
{
    OracleConnectionStringBuilder builder = new OracleConnectionStringBuilder();
    builder.DataSource = "localhost:1521/Prueba";
    builder.UserID = "SYSTEM";
    builder.PersistSecurityInfo = true;
    builder.Password = "password";
    builder.ConnectionTimeout = 50;
    Console.WriteLine(builder.ConnectionString);

    using (OracleConnection connection = new OracleConnection(builder.ConnectionString))
    {
        try
        {
            connection.Open();
            Console.WriteLine("Conexión válida");
            OracleCommand command = new OracleCommand("SELECT BANNER FROM v$version;", connection);
            OracleDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Console.WriteLine(reader.GetString(0));
                }
            }
            else
            {
                Console.WriteLine("Sin resultados.");
            }
            reader.Close();
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }
}

错误信息

运行程序时,在connection.Open();行出现错误:

Oracle.ManagedDataAccess.Client.OracleException: 'Connection request timeout.'

排查建议

  • 确认服务名一致性:
    第一个连接字符串使用SERVICE_NAME=ORCL,但Test2中用的是localhost:1521/Prueba,两者可能不匹配。在SQL Developer中执行SELECT value FROM v$parameter WHERE name = 'service_name';查询正确的服务名,Oracle XE默认服务名通常为XE而非ORCL。
  • 验证端口可达性:
    执行PowerShell命令Test-NetConnection localhost -Port 1521,检查1521端口是否处于监听状态,确保Oracle服务在该端口提供连接。
  • 检查Oracle服务状态:
    打开Windows服务管理器,确认OracleService[你的服务名]和OracleOraDB21Home1TNSListener两个服务处于运行状态。
  • 修正密码不一致问题:
    Test2代码中密码写的是password,但配置文件里是qwe123,统一密码后再测试。
  • 确保驱动版本兼容:
    检查NuGet包Oracle.ManagedDataAccess.Core的版本,安装与Oracle 21c兼容的最新稳定版。
  • 简化连接字符串测试:
    使用最简连接字符串排除冗余配置干扰,例如:User ID=SYSTEM;Password=qwe123;Data Source=localhost:1521/XE;(替换为实际服务名)。
  • 排查防火墙干扰:
    临时关闭本地防火墙,测试是否能正常连接,排除防火墙拦截1521端口的可能。

内容的提问来源于stack exchange,提问作者Leonardo Carrasco Velazquez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 16:10:26