教你一个好方法用一个,建一个类来做操作数据库的全部内容。而且打开数据时候根本不用判断。只需要定义一个Sqlconnection的属性就可以了。。
public SqlConnection Connection { get { if (connection == null) { connection = new SqlConnection("连接字符串"); } else if (connection.State == ConnectionState.Closed) { connection.Open(); } else if (connection.State == ConnectionState.Broken) { connection.Close(); connection.Open(); } return connection; } }