//读取配置文件,连接数据库语句 public static string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["Family"].ConnectionString; private static OleDbConnection oleConn = new OleDbConnection(strCon);//access database /// <summary> /// 根据命令增加 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int Insert(string sql) { int result = 0; try { oleConn.Open(); OleDbCommand oleCommand = new OleDbCommand(sql, oleConn); result = oleCommand.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { oleConn.Close(); } return result; } /// <summary> /// 根据命令查询表 /// </summary> /// <param name="sql">sql语句</param> /// <returns></returns> public static DataTable Select(string sql) { DataTable tb = new DataTable(); try { oleConn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(sql, oleConn); adapter.Fill(tb); } catch (Exception exception) { string message = exception.Message; } finally { oleConn.Close(); } return tb; } /// <summary> /// 根据命令删除 /// </summary> /// <param name="sql">sql语句</param> /// <returns></returns> public static int Delete(string sql) { int ifExecute = 0; try { oleConn.Open(); OleDbCommand comm = new OleDbCommand(sql); ifExecute = comm.ExecuteNonQuery(); } finally { oleConn.Close(); } return ifExecute; } /// <summary> /// 更新数据 /// </summary> /// <param name="sql">sql语句</param> /// <returns></returns> public static int Update(string sql) { int ifExecute = 0; try { oleConn.Open(); OleDbCommand comm = new OleDbCommand(sql, oleConn); ifExecute = comm.ExecuteNonQuery(); } finally { oleConn.Close(); } return ifExecute; } /// <summary> /// 执行sql返回对象 /// </summary> /// <param name="sql">sql语句</param> /// <returns></returns> public static object ExecuteSingle(string sql) { object obj = null; try { oleConn.Open(); OleDbCommand comm = new OleDbCommand(sql, oleConn); obj = comm.ExecuteScalar(); } finally { oleConn.Close(); } return obj; }