数据库访问类,代码访问问题
写一个数据库访问类,程序代码:
public static class DBHelper { private static SqlConnection connection; public static SqlConnection Connection { get { //从配置文件中获取连接数据库的连接字符串 string connectionString=ConfigurationManager .ConnectionStrings ["HRConStr"].ConnectionString; if(connection==null ) { connection =new SqlConnection (connectionString ); connection.Open (); } else if(connection.State==System.Data .ConnectionState .Closed ) { connection .Open (); } else if(connection .State==System .Data.ConnectionState .Broken ) { connection.Close (); connection.Open (); } return connection ; } } //不带参数的执行命令 public static int ExecuteCommand(string safeSql) { SqlCommand cmd=new SqlCommand (safeSql,Connection ); int result=cmd.ExecuteNonQuery (); return result ; connection.Close();//提示connection不能访问,改成Connection也是那个提示,请问是怎么回事呢? } }