数据库调用,常用类,喜欢用的一种形式,可以根据自己的情况修改使用。
namespace DAL { public class db { public static string connstr() { return ConfigurationManager.ConnectionStrings["dbcon"].ToString(); } public static string connstrAll() { return ConfigurationManager.ConnectionStrings["dbconall"].ToString(); } public static string connstr_xls(string path2) { string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + HttpContext.Current.Server.MapPath(path2) + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; return strConn; } public static string connstr_xlsx(string path2) { string strConn = "Provider=Microsoft.ACE.Oledb.12.0;Data Source=" + HttpContext.Current.Server.MapPath(path2) + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'"; return strConn; } public static string connstr_xls_HDRNo(string path2) { string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + HttpContext.Current.Server.MapPath(path2) + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1;'"; return strConn; } public static string connstr_xlsx_HDRNo(string path2) { string strConn = "Provider=Microsoft.ACE.Oledb.12.0;Data Source=" + HttpContext.Current.Server.MapPath(path2) + ";Extended Properties='Excel 12.0;HDR=No;IMEX=1;'"; return strConn; } public static void ConnOpen(SqlConnection conn) { //try //{ if (conn.State == ConnectionState.Closed) { //conn.ConnectionString = connstr(); conn.Open(); } else { conn.Close(); conn.Open(); } } public static SqlConnection Conn() { SqlConnection conn = new SqlConnection(connstr()); return conn; } public static void ConnClose(SqlConnection conn) { conn.Close(); } } }