C# 使用C连接数据库的正确方法#

C# 使用C连接数据库的正确方法#,c#,sql-server,database-design,connection,database-connection,C#,Sql Server,Database Design,Connection,Database Connection,我目前正在使用一个连接到我机器上本地SQL Server数据库的应用程序。我有许多SQL查询目前使用不同的方法执行得很好。我的问题是关于不同方法之间数据库连接的打开/关闭 我有两种类似这样的方法: Class MyClass { string connectionString = "myConnectionString"; public void Method1() { SqlConnection con = new SqlConnection(conn

我目前正在使用一个连接到我机器上本地SQL Server数据库的应用程序。我有许多SQL查询目前使用不同的方法执行得很好。我的问题是关于不同方法之间数据库连接的打开/关闭

我有两种类似这样的方法:

Class MyClass
{
    string connectionString = "myConnectionString";

    public void Method1()
    {
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        string sqlStr = "my SQL query";
        SqlCommand com = new SqlCommand(sqlStr, con);
        com.ExecuteNonQuery();
        con.Close();
    }

    public void Method2()
    {
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        string sqlStr = "my SQL query";
        SqlCommand com = new SqlCommand(sqlStr, con);
        com.ExecuteNonQuery();
        con.Close();
    }
}
如果我调用这些方法,它们工作得很好,没有例外。但这是处理数据库连接的正确方法吗?例如,我可以使用一个在
MyClass
初始化后立即初始化的静态连接吗?像这样

Class MyClass
{
    string connectionString = "myConnectionString";
    SqlConnection con = new SqlConnection(connectionString);
    con.Open();

    public void Method1()
    {
        ...
    }
    etc.
还是有一种处理数据库连接的“更好”方法


我非常感谢您提供的任何信息。

无论何时使用
IDisposable
实例,您都宁愿使用

public void Method3() {
  string sqlStr = "my SQL query";

  // Do not forget to configure connection pull so that
  // establishing a connection will not be expensive 
  using (SqlConnection con = new SqlConnection(connectionString)) {
    con.Open();

    using (SqlCommand com = new SqlCommand(sqlStr, con)) {
      com.ExecuteNonQuery();
    }
  }
}
如果愿意,可以组合查询:

    public void Method4() {
      string sqlStr1 = "my SQL query 1";
      string sqlStr1 = "my SQL query 2";

      // Do not forget to configure connection pull so that
      // establishing a connection will not be expensive 
      using (SqlConnection con = new SqlConnection(connectionString)) {
        con.Open();

        // Think on having both queries executed in one transaction
        using (SqlCommand com1 = new SqlCommand(sqlStr1, con)) {
          com1.ExecuteNonQuery();
        }

        using (SqlCommand com2 = new SqlCommand(sqlStr2, con)) {
          com2.ExecuteNonQuery();
        } 
      }
    }

静态连接可能很难维护,特别是如果您正在实施多线程软件,这就是为什么您应该避免使用它们的原因。无论何时使用
IDisposable
实例,您都宁愿使用

public void Method3() {
  string sqlStr = "my SQL query";

  // Do not forget to configure connection pull so that
  // establishing a connection will not be expensive 
  using (SqlConnection con = new SqlConnection(connectionString)) {
    con.Open();

    using (SqlCommand com = new SqlCommand(sqlStr, con)) {
      com.ExecuteNonQuery();
    }
  }
}
如果愿意,可以组合查询:

    public void Method4() {
      string sqlStr1 = "my SQL query 1";
      string sqlStr1 = "my SQL query 2";

      // Do not forget to configure connection pull so that
      // establishing a connection will not be expensive 
      using (SqlConnection con = new SqlConnection(connectionString)) {
        con.Open();

        // Think on having both queries executed in one transaction
        using (SqlCommand com1 = new SqlCommand(sqlStr1, con)) {
          com1.ExecuteNonQuery();
        }

        using (SqlCommand com2 = new SqlCommand(sqlStr2, con)) {
          com2.ExecuteNonQuery();
        } 
      }
    }

静态连接可能很难维护,特别是如果您正在实施多线程软件,这就是为什么您应该避免使用它们的原因。无论何时使用
IDisposable
实例,您都宁愿使用

public void Method3() {
  string sqlStr = "my SQL query";

  // Do not forget to configure connection pull so that
  // establishing a connection will not be expensive 
  using (SqlConnection con = new SqlConnection(connectionString)) {
    con.Open();

    using (SqlCommand com = new SqlCommand(sqlStr, con)) {
      com.ExecuteNonQuery();
    }
  }
}
如果愿意,可以组合查询:

    public void Method4() {
      string sqlStr1 = "my SQL query 1";
      string sqlStr1 = "my SQL query 2";

      // Do not forget to configure connection pull so that
      // establishing a connection will not be expensive 
      using (SqlConnection con = new SqlConnection(connectionString)) {
        con.Open();

        // Think on having both queries executed in one transaction
        using (SqlCommand com1 = new SqlCommand(sqlStr1, con)) {
          com1.ExecuteNonQuery();
        }

        using (SqlCommand com2 = new SqlCommand(sqlStr2, con)) {
          com2.ExecuteNonQuery();
        } 
      }
    }

静态连接可能很难维护,特别是如果您正在实施多线程软件,这就是为什么您应该避免使用它们的原因。无论何时使用
IDisposable
实例,您都宁愿使用

public void Method3() {
  string sqlStr = "my SQL query";

  // Do not forget to configure connection pull so that
  // establishing a connection will not be expensive 
  using (SqlConnection con = new SqlConnection(connectionString)) {
    con.Open();

    using (SqlCommand com = new SqlCommand(sqlStr, con)) {
      com.ExecuteNonQuery();
    }
  }
}
如果愿意,可以组合查询:

    public void Method4() {
      string sqlStr1 = "my SQL query 1";
      string sqlStr1 = "my SQL query 2";

      // Do not forget to configure connection pull so that
      // establishing a connection will not be expensive 
      using (SqlConnection con = new SqlConnection(connectionString)) {
        con.Open();

        // Think on having both queries executed in one transaction
        using (SqlCommand com1 = new SqlCommand(sqlStr1, con)) {
          com1.ExecuteNonQuery();
        }

        using (SqlCommand com2 = new SqlCommand(sqlStr2, con)) {
          com2.ExecuteNonQuery();
        } 
      }
    }

静态连接可能很难维护,特别是如果您正在实施多线程软件,这就是为什么您应该避免使用它们的原因。如果不使用静态连接,请检查数据库连接字符串以启用连接池。是控制连接的连接池,将其保持打开状态直到超时(提高性能),并在不需要时将其关闭。
对每个一次性物品使用“using”子句

不要使用静态连接,请检查数据库连接字符串以启用连接池。是控制连接的连接池,将其保持打开状态直到超时(提高性能),并在不需要时将其关闭。
对每个一次性物品使用“using”子句

不要使用静态连接,请检查数据库连接字符串以启用连接池。是控制连接的连接池,将其保持打开状态直到超时(提高性能),并在不需要时将其关闭。
对每个一次性物品使用“using”子句

不要使用静态连接,请检查数据库连接字符串以启用连接池。是控制连接的连接池,将其保持打开状态直到超时(提高性能),并在不需要时将其关闭。
对每个一次性物品使用“using”子句

@Dmitry Bychenko我应该在使用(…{…})后关闭连接吗?@Marcus:不;使用(…){…}可以帮你。@Dmitry Bychenko我应该在使用(…){…}后关闭连接吗?@Marcus:不;使用(…){…}可以帮你。@Dmitry Bychenko我应该在使用(…){…}后关闭连接吗?@Marcus:不;使用(…){…}可以帮你。@Dmitry Bychenko我应该在使用(…){…}后关闭连接吗?@Marcus:不;使用(…){…}将为您完成此操作。