首页 > 代码库 > FCL 系列 - 2. FCL.Database.dll
FCL 系列 - 2. FCL.Database.dll
基于 oledb 的数据库访问,可用于 Oracle、MS SqlServer、Access、Excel ... 的数据库访问。
[DeveloperInfo("Wagwei")] public class DataCenterOp : IWagDataCenter { string _sConn = string.Empty; Dictionary<string, ConnAndTrans> _dicConnAndTrans; DBType _DBType = DBType.ACCESS; public DBType DatabaseType; public void Init(string conn, DBType dbType); public void TryToConnect(); #region 构造(多态) public DataCenterOp() { } public DataCenterOp(string conn, DBType dbType); public void InitSqlServerOp(string dataSource, string password, string initialCatalog); public void InitSqlServerOp(string dataSource, string initialCatalog); public void InitAccessOp(string dataSource, string password); public void InitAccessOpNew(string dataSource, string password); public void InitText(string file); public void InitExcelOp(string dataSource, string pwd, string hdr, string imex); public void InitOracleOp(string userId, string dataSource, string pwd, string distibtx); #endregion #region 事务: 创建, 提交, 回滚, 获取 public string CreateTran(); public void CommitTran(string transGuid); public void RollbackTran(string transGuid); #endregion #region 执行sql语句 public object ExecScalar(string sql, string transGuid = ""); public object ExecScalar(SqlBuilder sqlB, string transGuid); public object ExecScalar(SqlBuilder[] arraySqlB, string transGuid); public int ExecNonQuery(string sql, string transGuid); public int ExecNonQuery(string sql); public int ExecNonQuery(SqlBuilder sqlB, string transGuid); public int ExecNonQuery(SqlBuilder sqlB); public int[] ExecNonQuery(SqlBuilder[] arraySqlB, string transGuid); #endregion #region 获取dt, ds public DataTable GetDt(string sql, string tableName, bool isSchema, string transGuid); public DataTable GetDt(SqlBuilder sqlB, bool isSchema, string transGuid); public DataSet GetDs(string sqls, string tableNames, bool isSchema, string transGuid); public DataSet GetDs(SqlBuilder[] arraySqlB, bool schema, string transGuid); #endregion #region 更新dt, ds public void UpdateTable(DataTable dt, string tableName, string keyFields, string updateFields, string transGuid); public void UpdateTableWithoutConflict(DataTable dt, string tableName, string keyFields, string updateFields, string transGuid); #endregion #region 更新数据(根据DataInfo) public void UpdateDataInfo(List<DataInfo> listDataInfo, string transGuid); #endregion #region 更新数据(插入, 删除, 更新), 根据FieldAndValue //插入 public int InsertData(string tableName, FieldAndValue[] keyFieldsAndValue, FieldAndValue[] UpdateFieldsAndValue, string transGuid); //删除 public int DeleteData(string tableName, FieldAndValue[] FieldsAndValues, string transGuid); //更新 public int UpdateData(string tableName, FieldAndValue[] keyFieldsAndValue, FieldAndValue[] UpdateFieldsAndValue, string transGuid); #endregion #region 常用数据库操作 public DataTable GetDmExecSessions(int type = 0); public void InitializeDatabase(); public string GetGenCode(string id); #region 将T_Sys_Variables(var_key, var_value, remark)表作为配置信息存储媒介 public string CfgGetValue(string key); public bool CfgSetValue(string key, string value); #endregion #endregion }//end class
简单示例代码:
DataCenterOp mDCO = new DataCenterOp();
DataTable mdt;
mDCO.InitSqlServerOp(@"JOHN-PC\DAVID", "DB_WagTest");
mDCO.InitSqlServerOp(@"JOHN-PC\DAVID", "1122", "DB_WagTest");
//使用sql语句查询
this.mdt = mDCO.GetDt("select * from t_products", "", false, "");
//直接将 DataTable 数据更新到数据库(根据行的状态、Cell 的状态)
mDCO.UpdateTable(mdt, "t_products", "*pid", "", "");
mdt.AcceptChanges();
//插入数据
SqlBuilder sb = new SqlBuilder("insert into t_log(filename) values(?)");
//sb = new SqlBuilder("insert into t_log(filename) values(?)", "值");
sb.AppendParams(DateTime.Now.ToString("yyyyMMddHHmmssffff"));
mDCO.ExecNonQuery(sb);
//更多示例代码将在后期贴上...
FCL 系列 - 2. FCL.Database.dll