无法将连接字符串传递给 OleDbConnection

发布于 2024-12-19 03:13:29 字数 684 浏览 3 评论 0原文

更新:错误是由名为 OleDbConnection 的类隐藏 OleDbConnection 构造函数引起的,因此不提供重载方法。

我正处于学习如何使用 ole 数据库连接的早期阶段,但是我立即遇到了问题。尽管有一个重载方法来创建 OleDbConnection 连接,但它根本不允许我传入连接字符串。

这就是我正在尝试的。

private OleDbConnection _myConnection = null;

public bool CreateConnection()
{
    try
    {
        _myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["OracleDefault"].ConnectionString);
    }
    catch (Exception e)
    {
        throw new Exception(e.Message);
    }

    return true;
}

现在似乎没有任何问题,但是我收到一条错误消息,指出该方法有 0 个参数,但使用 1 个参数调用。我见过无数的例子表明它是以这种方式完成的,但由于某种原因它不允许我这样做。有什么想法吗?

更新:删除了分号,因为它不应该出现在我发布的示例中,但这不是问题。它根本不接受任何形式的字符串。

UPDATE: Error was caused by a class named the OleDbConnection hiding the OleDbConnection constructors and therefore not presenting the overload method.

I am in the early stages of learning how to use ole database connections however I have run into a probem straight away. Despite there being an overload method to create an OleDbConnection connection it simply wont allow me to pass a connection string in.

Heres what I am trying.

private OleDbConnection _myConnection = null;

public bool CreateConnection()
{
    try
    {
        _myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["OracleDefault"].ConnectionString);
    }
    catch (Exception e)
    {
        throw new Exception(e.Message);
    }

    return true;
}

Now there doesn't seem to be anything wrong with that however I get an error saying that the method has 0 parameters but is invoked with 1 argument. I have seen countless examples showing it done in this way but for some reason it just won't let me do it. Any ideas?

UPDATE: Removed the semi colon as it wasnt supposed to be in the example I posted however this is not the issue. It simply won't accept any form of string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烈酒灼喉 2024-12-26 03:13:29

只需删除 ;在 ["OracleDefault"].ConnectionString 之后。

Just remove the ; after ["OracleDefault"].ConnectionString.

好倦 2024-12-26 03:13:29

使用

_myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings.["OracleDefault"].ConnectionString);

而不是因为

_myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings.["OracleDefault"].ConnectionString;);

构造函数包含需要字符串重载

Use

_myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings.["OracleDefault"].ConnectionString);

instead of

_myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings.["OracleDefault"].ConnectionString;);

as the constructor contains an overload that require string.

寻找我们的幸福 2024-12-26 03:13:29

更新:错误是由名为 OleDbConnection 的类隐藏 OleDbConnection 构造函数引起的,因此不提供重载方法。

UPDATE: Error was caused by a class named the OleDbConnection hiding the OleDbConnection constructors and therefore not presenting the overload method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文