在C#中打开的SqlConnection时也不例外 [英] No exception when opening SqlConnection in C#

查看:277
本文介绍了在C#中打开的SqlConnection时也不例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个应用程序连接到本地SQL Server数据库,我使用下面的方法来创建异常连接字符串和测试。

I am making an application to connect to a local SQL Server database, and I am using the following method to create the connection string and test for exceptions.

public void connect(string user, string password, string server, string database)
    {
        connectString = "user id=" + user + ";" +
                        "password=" + password + ";" +
                        "server=" + server + ";" +
                        "Trusted_Connection=yes;" +
                        "database=" + database + ";" +
                        "connection timeout=5";
        myConnection = new SqlConnection(connectString);
        try
        {
            myConnection.Open();
            isConnected = true;
        }
        catch (SqlException)
        {
            isConnected = false;
        }
        catch (InvalidOperationException)
        {
            isConnected = false;
        }
    }

从我的研究应该有,如果抛出的异常连接不能打开,但如果我通过废话凭证或空字符串不会抛出异常。如果我在传递正确的信息,我可以连接并使用在连接字符串中提取数据。与坏连接字符串后,我得到一个异常试图填补与数据一个SqlDataAdapter时。

From my research there should be an exception thrown if the connection cannot be opened, but if I pass nonsense credentials or empty strings no exception is thrown. If I pass in correct information I am able to connect and use the connection string to pull in data. With the bad connection string I get an exception later when trying to fill a SqlDataAdapter with the data.

此外,在胡说连接字符串myConnection.State是开放的,当我调试并检查。

Also, with the nonsense connection strings myConnection.State is open when I debug and check that.

有没有用我的如何异常捕获应该工作的理解有问题?我在想,当我无法连接到数据库,因为一个坏的凭据SqlConnection的国家不应该是开放的正确?

Is there a problem with my understanding of how the exception catching should work? Am I correct in thinking that the SqlConnection State should not be open when I am unable to connect to the database because of a bad credentials?

推荐答案

在连接字符串你有Trusted_Connection =是;将使用当前的Windows帐户进行身份验证,而忽略用户名,在连接字符串中提供的密码。

In your connection string you have "Trusted_Connection=yes;" that will use the current windows account for authentication and will ignore the username, password supplied in the connection string.

请参阅:的集成安全性 - 或 - Trusted_Connection的从SqlConnection.ConnectionString地产

See: Integrated Security -or- Trusted_Connection from SqlConnection.ConnectionString Property

当假的,用户ID和密码在连接中指定。当
真,当前的Windows帐户凭据用于
认证。可识别的值为真,假,是的,没有了,SSPI
(强烈推荐),这相当于为true。

When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication. Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.

如果用户ID和密码指定并集成安全性设置为true,用户ID和密码将被忽略并集成
股票将被使用。 SqlCredential是为使用SQL Server身份验证
(集成安全性= FALSE)的连接指定
凭据更安全的方式。

If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used. SqlCredential is a more secure way to specify credentials for a connection that uses SQL Server Authentication (Integrated Security=false).

这篇关于在C#中打开的SqlConnection时也不例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆