C# 测试 SQL 连接字符串可用性的最有效方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17853371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:39:10  来源:igfitidea点击:

Most efficient way to test SQL connection string availibility

c#sqlsql-serversqlconnection

提问by Marek

I have this code down which I tried to make it Test SQL string connectivity, but I dont know how to handle the part with connection.Open = truewould you please help me to solve this out? Thank you so much for your time.

我有这段代码,我试图让它测试 SQL 字符串连接,但我不知道如何处理这个部分,connection.Open = true你能帮我解决这个问题吗?非常感谢您的参与。

  private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection("Data Source='" + textBox1.Text + "';Initial Catalog='" + textBox2.Text + "';User ID='" + textBox3.Text + "';Password='" + textBox4.Text + "'"))
            {
                try
                {
                    connection.Open();
                    if (connection.Open == true) // if connection.Open was successful
                    {
                        MessageBox.Show("You have been successfully connected to the database!");
                    }
                    else
                    {
                        MessageBox.Show("Connection failed.");
                    }
                }
                catch (SqlException) { }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Chyba v p?ihlá?ení: " + ex);
        }
        finally
        {

        }
    }

It says: "Cannot asign 'open' because it is a 'methoud group' " I know that this code might be totaly bad, but I need to handle this somehow and have no idea what is the right way. Thank you.

它说:“不能分配'开放',因为它是一个'方法组'”我知道这段代码可能非常糟糕,但我需要以某种方式处理这个问题并且不知道什么是正确的方法。谢谢你。

This is what is not actually working for not-opened connection:

这实际上不适用于未打开的连接:

using (SqlConnection connection = new SqlConnection("Data Source='" + textBox1.Text + "';Initial Catalog='" + textBox2.Text + "';User ID='" + textBox3.Text + "';Password='" + textBox4.Text + "'"))
        {

             connection.Open();

            if (connection.State == ConnectionState.Open)
            {

                MessageBox.Show("Spojení s databázi problěhlo úspě?ně.");
            }
            connection.Close();
            if (connection.State == ConnectionState.Closed)
            {
                MessageBox.Show("Spojení selhalo");
            }
        }

采纳答案by DGibbs

You're using connection.Open = trueas if it were a property.

您正在使用connection.Open = true,好像它是一个属性。

It's a method: connection.Open()

这是一种方法: connection.Open()

Use the ConnectionStateenum to determine if the connection is open or not, eg:

使用ConnectionState枚举来确定连接是否打开,例如:

connection.State == ConnectionState.Open

回答by Kamil Budziewski

You need to check if it's open by this code:

您需要通过以下代码检查它是否打开:

if(connection.State == ConnectionState.Open)
{
  ...
}