日期:2014-05-18 浏览次数:20722 次
SqlConnection conn=new Sqlconnection(); conn.ConnectionString = ConfigurationSettings.AppSettings.Get("connecionString"); SqlCommand cmd......
------解决方案--------------------
#region 连接字符串 const string CONNSTRING = "server=.;database=Library;Trusted_connection=true"; static SqlConnection conn = new SqlConnection(CONNSTRING); public static SqlConnection Conn { get { return DBsql.conn; } } #endregion #region 打开连接 public static void Open() { if (conn.State == ConnectionState.Closed) { conn.Open(); } else if(conn.State== ConnectionState.Broken) { conn.Close(); conn.Open(); } } #endregion #region 关闭连接 public static void Close() { if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken) { conn.Close(); } } #endregion public static void Ti(string sql,ref SqlDataReader reader) { SqlCommand comm = new SqlCommand(sql, conn); Open(); reader = comm.ExecuteReader(CommandBehavior.CloseConnection); }