/// <summary> /// Prepare DML /// </summary> /// <param name="ids">SQL语句ID编号</param> /// <param name="objs">输入对象</param> /// <returns></returns> public string execPrepare(string id, Object obj) { try { resultSql = ""; if (id == null) { throw new Exception("参数不全"); } if (obj == null) { throw new Exception("参数不全"); } daoStruct = parseDao.ObtainConfig(id); if (daoStruct.DbType.Length <= 0 || daoStruct.ConStr.Length <= 0 || daoStruct.SqlStr.Length <= 0) { throw new Exception("配置参数不全"); } string sql = daoStruct.SqlStr; resultSql = sql; if (daoStruct.IsLog) { WriteLogin.writeDBLog(daoStruct.Desc, sql); } List <DTOClass> dtolist = setDto(obj); if (sql.Length <= 0) { throw new Exception("构造SQL语句出错"); } IDataSource dataSource = null; switch (daoStruct.DbType) { case "oracle": dataSource = new OracleDataSource(daoStruct); break; case "mysql": dataSource = new MySqlDataSource(daoStruct); break; case "sql": dataSource = new SqlDataSource(daoStruct); break; case "access": dataSource = new AccessDataSource(daoStruct); break; case "sqllite": dataSource = new SqlLiteDataSource(daoStruct); break; } dataSource.PrepareExcute(sql, dtolist); } catch (Exception ex) { throw new Exception(ex.Message, ex); } return(""); }
public static object GetConnection(DaoStruct daoStruct) { object dbconn = null; foreach (DictionaryEntry de in mapdb) { if (de.Key.ToString() == daoStruct.ConStr) { switch (daoStruct.DbType) { case "oracle": dbconn = (OracleConnection)de.Value; break; case "mysql": dbconn = (MySqlConnection)de.Value; break; case "sql": dbconn = (SqlConnection)de.Value; break; case "access": dbconn = (OleDbConnection)de.Value; break; case "sqllite": dbconn = (SQLiteConnection)de.Value; break; } break; } } return(dbconn); }
/// <summary> /// 单DML语句操作 /// </summary> /// <param name="id">SQL语句ID编号</param> /// <param name="obj">输入输出对象</param> /// <returns></returns> public string execDml(string id, Object obj) { resultSql = ""; if (id == "") { throw new Exception("参数不全"); } if (obj == null) { throw new Exception("参数不全"); } daoStruct = parseDao.ObtainConfig(id); if (daoStruct.DbType.Length <= 0 || daoStruct.ConStr.Length <= 0 || daoStruct.SqlStr.Length <= 0) { throw new Exception("配置参数不全"); } string sql = ""; sql = setSql(obj, daoStruct.SqlStr); resultSql = sql; //记录SQL if (daoStruct.IsLog) { WriteLogin.writeDBLog(daoStruct.Desc, resultSql); } if (sql.Length <= 0) { throw new Exception("构造SQL语句出错"); } IDataSource dataSource = null; switch (daoStruct.DbType) { case "oracle": dataSource = new OracleDataSource(daoStruct); break; case "mysql": dataSource = new MySqlDataSource(daoStruct); break; case "sql": dataSource = new SqlDataSource(daoStruct); break; case "access": dataSource = new AccessDataSource(daoStruct); break; case "sqllite": dataSource = new SqlLiteDataSource(daoStruct); break; } if (dataSource != null) { dataSource.SingleExecute(sql); } return(""); }
public SqlDataSource(DaoStruct daoStruct) { this.daoStruct = daoStruct; DBConnectionPool.initConnection(sqlConnection, daoStruct); sqlConnection = (SqlConnection)DBConnectionPool.GetConnection(daoStruct); sqlCommand = new SqlCommand(); sqlCommand.Connection = sqlConnection; }
/// <summary> /// 数据控件初始化 /// </summary> /// <param name="scon">数据连接字符</param> public OracleDataSource(DaoStruct daoStruct) { this.daoStruct = daoStruct; DBConnectionPool.initConnection(oracleConnection, daoStruct); oracleConnection = (OracleConnection)DBConnectionPool.GetConnection(daoStruct); oracleCommand = new OracleCommand(); oracleCommand.Connection = oracleConnection; }
public AccessDataSource(DaoStruct daoStruct) { this.daoStruct = daoStruct; DBConnectionPool.initConnection(sqlConnection, daoStruct); sqlConnection = (OleDbConnection)DBConnectionPool.GetConnection(daoStruct); sqlCommand = new OleDbCommand(); sqlCommand.Connection = sqlConnection; }
/// <summary> /// 获得数据,只有数据列少时用他 /// </summary> /// <param name="id"></param> /// <param name="objs"></param> /// <returns></returns> public IDataReader getDataRead(string id, Object objs) { resultSql = ""; daoStruct = parseDao.ObtainConfig(id); if (daoStruct.DbType.Length <= 0 || daoStruct.ConStr.Length <= 0 || daoStruct.SqlStr.Length <= 0) { throw new Exception("配置参数不全"); } string sql = setSql(objs, daoStruct.SqlStr); resultSql = sql; if (sql.Length <= 0) { throw new Exception("构造SQL语句出错"); } IDataSource dataSource = null; List <Object> objlist = new List <Object>(); switch (daoStruct.DbType) { case "oracle": dataSource = new OracleDataSource(daoStruct); break; case "mysql": dataSource = new MySqlDataSource(daoStruct); break; case "sql": dataSource = new SqlDataSource(daoStruct); break; case "access": dataSource = new AccessDataSource(daoStruct); break; case "sqllite": dataSource = new SqlLiteDataSource(daoStruct); break; } if (dataSource != null) { return(dataSource.GetDataRead(sql)); } else { return(null); } }
public void BackupFileData(string id, string destFilename) { daoStruct = parseDao.ObtainConfig(id); IDataSource dataSource = null; switch (daoStruct.DbType) { case "access": dataSource = new AccessDataSource(daoStruct); break; case "sqllite": dataSource = new SqlLiteDataSource(daoStruct); break; } string[] srcfiles = daoStruct.ConStr.Split(';'); string srcfile = srcfiles[0].Replace("Data Source=", ""); dataSource.Backup(srcfile, destFilename); }
public void disposeConn(string id) { try { daoStruct = parseDao.ObtainConfig(id); IDataSource dataSource; switch (daoStruct.DbType) { case "oracle": dataSource = new OracleDataSource(daoStruct); dataSource.DisposeConn(); break; case "mysql": dataSource = new MySqlDataSource(daoStruct); dataSource.DisposeConn(); break; case "sql": dataSource = new SqlDataSource(daoStruct); dataSource.DisposeConn(); break; case "access": dataSource = new AccessDataSource(daoStruct); dataSource.DisposeConn(); break; case "sqllite": dataSource = new SqlLiteDataSource(daoStruct); dataSource.DisposeConn(); break; } } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// 查询数据根据SQL语句 /// </summary> /// <param name="id">SQL编号</param> /// <param name="sql">SQL语句</param> /// <returns>数据结果</returns> public DataSet getDataSet(string id, string sql) { resultSql = sql; DataSet rtnDataSet = new DataSet(); IDataSource dataSource = null; daoStruct = parseDao.ObtainConfig(id); switch (daoStruct.DbType) { case "oracle": dataSource = new OracleDataSource(daoStruct); break; case "mysql": dataSource = new MySqlDataSource(daoStruct); break; case "sql": dataSource = new SqlDataSource(daoStruct); break; case "access": dataSource = new AccessDataSource(daoStruct); break; case "sqllite": dataSource = new SqlLiteDataSource(daoStruct); break; } if (dataSource != null) { return(dataSource.SelectExecute(sql)); } else { return(null); } }
/// <summary> /// 备份数据库 /// </summary> /// <param name="path"></param> /// <param name="database"></param> public void BackupData(string id, string path, string database) { daoStruct = parseDao.ObtainConfig(id); string backupsql = ""; IDataSource dataSource = null; switch (daoStruct.DbType) { case "oracle": dataSource = new OracleDataSource(daoStruct); break; case "sql": backupsql = "BACKUP DATABASE [" + database + "] TO DISK =N'" + path + "' WITH NOFORMAT, INIT, NAME=N'" + database + "-完整 数据库 备份', SKIP, NOREWIND, NOUNLOAD, STATS = 10"; dataSource = new SqlDataSource(daoStruct); break; case "mysql": dataSource = new MySqlDataSource(daoStruct); break; } dataSource.Backup(backupsql); }
/// <summary> /// Prepare DML /// </summary> /// <param name="ids">SQL语句ID编号</param> /// <param name="objs">输入对象</param> /// <returns></returns> public string execPrepare(List <string> ids, List <Object> objs) { try { resultSql = ""; List <string> idlist = new List <string>(); idlist = ids; List <Object> objectlist = new List <Object>(); objectlist = objs; if (idlist.Count <= 0) { throw new Exception("缺少参数"); } if (idlist.Count != objectlist.Count) { throw new Exception("参数数量不一致"); } List <string> sqls = new List <string>(); List <List <DTOClass> > dtolists = new List <List <DTOClass> >(); for (int i = 0; i < idlist.Count; i++) { daoStruct = parseDao.ObtainConfig(idlist[i]); if (daoStruct.DbType.Length <= 0 || daoStruct.ConStr.Length <= 0 || daoStruct.SqlStr.Length <= 0) { throw new Exception("配置参数不全"); } string sql = daoStruct.SqlStr; List <DTOClass> dtolist = setDto(objectlist[i]); if (sql.Length <= 0) { throw new Exception("构造SQL语句出错"); } sqls.Add(sql); dtolists.Add(dtolist); if (resultSql != "") { resultSql += "\r\n"; } resultSql += sql; } if (daoStruct.IsLog) { WriteLogin.writeDBLog(daoStruct.Desc, resultSql); } IDataSource dataSource = null; switch (daoStruct.DbType) { case "oracle": dataSource = new OracleDataSource(daoStruct); break; case "mysql": dataSource = new MySqlDataSource(daoStruct); break; case "sql": dataSource = new SqlDataSource(daoStruct); break; case "access": dataSource = new AccessDataSource(daoStruct); break; case "sqllite": dataSource = new SqlLiteDataSource(daoStruct); break; } dataSource.PrepareExcute(sqls, dtolists); } catch (Exception ex) { throw new Exception(ex.Message, ex); } return(""); }
/// <summary> /// 多DML语句操作 /// </summary> /// <param name="ids">SQL语句ID编号列表</param> /// <param name="objs">输入输出对象列表</param> /// <returns></returns> public string execTranctionDml(List <string> ids, List <Object> objs) { resultSql = ""; List <string> idlist = new List <string>(); idlist = ids; List <Object> objectlist = new List <Object>(); objectlist = objs; if (idlist.Count <= 0) { throw new Exception("缺少参数"); } if (idlist.Count != objectlist.Count) { throw new Exception("参数数量不一致"); } List <string> sqls = new List <string>(); for (int i = 0; i < idlist.Count; i++) { daoStruct = parseDao.ObtainConfig(idlist[i]); if (daoStruct.DbType.Length <= 0 || daoStruct.ConStr.Length <= 0 || daoStruct.SqlStr.Length <= 0) { throw new Exception("配置参数不全"); } string sql = ""; sql = setSql(objectlist[i], daoStruct.SqlStr); if (sql.Length <= 0) { throw new Exception("构造SQL语句出错"); } sqls.Add(sql); if (resultSql != "") { resultSql += "\r\n"; } resultSql += sql; } if (daoStruct.IsLog) { WriteLogin.writeDBLog(daoStruct.Desc, resultSql); } IDataSource dataSource = null; switch (daoStruct.DbType) { case "oracle": dataSource = new OracleDataSource(daoStruct); break; case "mysql": dataSource = new MySqlDataSource(daoStruct); break; case "sql": dataSource = new SqlDataSource(daoStruct); break; case "access": dataSource = new AccessDataSource(daoStruct); break; case "sqllite": dataSource = new SqlLiteDataSource(daoStruct); break; } dataSource.TranctionExcute(sqls); //记录SQL return(""); }
public DAOClass() { parseDao = ParseDaoConfig.createInstance(); daoStruct = new DaoStruct(); }
public static void initConnection(object dbconn, DaoStruct daoStruct) { if (!mapdb.ContainsKey(daoStruct.ConStr)) { switch (daoStruct.DbType) { case "oracle": if (dbconn == null) { dbconn = new OracleConnection(daoStruct.ConStr); } if (((OracleConnection)dbconn).State != ConnectionState.Open) { ((OracleConnection)dbconn).Open(); } break; case "mysql": if (dbconn == null) { dbconn = new MySqlConnection(daoStruct.ConStr); } if (((MySqlConnection)dbconn).State != ConnectionState.Open) { ((MySqlConnection)dbconn).Open(); } break; case "sql": if (dbconn == null) { dbconn = new SqlConnection(daoStruct.ConStr); } if (((SqlConnection)dbconn).State != ConnectionState.Open) { ((SqlConnection)dbconn).Open(); } break; case "access": if (dbconn == null) { dbconn = new OleDbConnection(daoStruct.ConStr); } if (((OleDbConnection)dbconn).State != ConnectionState.Open) { ((OleDbConnection)dbconn).Open(); } break; case "sqllite": if (dbconn == null) { dbconn = new SQLiteConnection(daoStruct.ConStr); } if (((SQLiteConnection)dbconn).State != ConnectionState.Open) { //((SQLiteConnection)dbconn).SetPassword("123"); //string ss = ((SQLiteConnection)dbconn).ConnectionString; ((SQLiteConnection)dbconn).Open(); //ss = ((SQLiteConnection)dbconn).ConnectionString; //((SQLiteConnection)dbconn).Close(); //string ss = ((SQLiteConnection)dbconn); } break; } mapdb.Add(daoStruct.ConStr, dbconn); } }
public static void disposeConnection(DaoStruct daoStruct) { object dbconn = null; foreach (DictionaryEntry de in mapdb) { if (de.Key.ToString() == daoStruct.ConStr) { dbconn = de.Value; break; } } switch (daoStruct.DbType) { case "oracle": if (dbconn != null) { if (((OracleConnection)dbconn).State != ConnectionState.Closed) { ((OracleConnection)dbconn).Close(); } ((OracleConnection)dbconn).Dispose(); dbconn = null; mapdb.Remove(daoStruct.ConStr); } break; case "mysql": if (dbconn != null) { if (((MySqlConnection)dbconn).State != ConnectionState.Closed) { ((MySqlConnection)dbconn).Close(); } ((MySqlConnection)dbconn).Dispose(); dbconn = null; mapdb.Remove(daoStruct.ConStr); } break; case "sql": if (dbconn != null) { if (((SqlConnection)dbconn).State != ConnectionState.Closed) { ((SqlConnection)dbconn).Close(); } ((SqlConnection)dbconn).Dispose(); dbconn = null; mapdb.Remove(daoStruct.ConStr); } break; case "access": if (dbconn != null) { if (((OleDbConnection)dbconn).State != ConnectionState.Closed) { ((OleDbConnection)dbconn).Close(); } ((OleDbConnection)dbconn).Dispose(); dbconn = null; mapdb.Remove(daoStruct.ConStr); } break; case "sqllite": if (dbconn != null) { if (((SQLiteConnection)dbconn).State != ConnectionState.Closed) { ((SQLiteConnection)dbconn).Close(); } ((SQLiteConnection)dbconn).Dispose(); dbconn = null; mapdb.Remove(daoStruct.ConStr); } break; } }