ADO.NET 连接字符串错误

发布于 2024-11-08 13:19:45 字数 1031 浏览 0 评论 0原文

首先,我是 C# 编程新手。 我创建了一个专用类,用于从 Visual Studio 2010 中 Web 服务应用程序的 app.config 获取连接字符串,如下代码所示。

在构建代码时,我通过 catch 块收到以下错误:

“名称‘连接’在当前上下文中不存在”。

显然连接超出了范围。

  1. 我该如何避免这个错误?
  2. 这里使用的 Dispose 方法是否正确?

public class FCSConnection : IDisposable
{
    public string GetDefaultConnectionString()
    {   
        string DefaultConnectionString = null;
        try
        {
            DefaultConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
            SqlConnection connection = new SqlConnection(DefaultConnectionString);
            connection.Open();
            return DefaultConnectionString;
        }
        catch (Exception)
        {
            if (DefaultConnectionString != null)
            {
                connection.Dispose();
            }
        }
        return DefaultConnectionString;
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }        
}

Firstly, am new to C# programming.
I have created a dedicated class to get the connection string from the app.config of a Web Services application in Visual Studio 2010 as per the code below.

On building the code I get the following error via the catch block:

"The name 'connection' does not exist in the current context".

Obviously connection is going out of scope.

  1. How do I avoid this error?
  2. Is the Dispose method being used correctly here?

public class FCSConnection : IDisposable
{
    public string GetDefaultConnectionString()
    {   
        string DefaultConnectionString = null;
        try
        {
            DefaultConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
            SqlConnection connection = new SqlConnection(DefaultConnectionString);
            connection.Open();
            return DefaultConnectionString;
        }
        catch (Exception)
        {
            if (DefaultConnectionString != null)
            {
                connection.Dispose();
            }
        }
        return DefaultConnectionString;
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }        
}

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

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

发布评论

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

评论(2

人心善变 2024-11-15 13:19:45

确切的编译器消息引用您的 catch 语句:

connection.Dispose();

这里,connection 是一个未知名称,因为它是在 try 块内声明的。

至于你的整个代码,我认为它也是错误的。如果您希望您的 FCSConnection 类封装 SQL 连接,您应该将 connection 声明为私有成员,然后将其处置在您的 Dispose() 中方法。

The exact compiler message refers to your catch statement:

connection.Dispose();

Here, connection is an unknown name, because it's declared inside the try block.

As for your entire code, I think it's also wrong. If you want your FCSConnection class to encapsulate the SQL connection, you should declare connection as a private member and then dispose it in your Dispose() method.

夜无邪 2024-11-15 13:19:45
public class FCSConnection : IDisposable
{
    private SqlConnection connection = null;

    public string GetDefaultConnectionString()
    {   
        string defaultConnectionString = null;
        try
        {
            defaultConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
            connection = new SqlConnection(defaultConnectionString);
            connection.Open(); // are you sure want to keep the connection being opened??
        }
        catch
        {
            Dispose();
        }
        return defaultConnectionString;
    }

    public void Dispose()
    {
        if (connection != null)
        {
            connection.Dispose();
            connection = null; // to avoid repeat dispose
        }
    }        
}
public class FCSConnection : IDisposable
{
    private SqlConnection connection = null;

    public string GetDefaultConnectionString()
    {   
        string defaultConnectionString = null;
        try
        {
            defaultConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
            connection = new SqlConnection(defaultConnectionString);
            connection.Open(); // are you sure want to keep the connection being opened??
        }
        catch
        {
            Dispose();
        }
        return defaultConnectionString;
    }

    public void Dispose()
    {
        if (connection != null)
        {
            connection.Dispose();
            connection = null; // to avoid repeat dispose
        }
    }        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文