/// <summary> /// Cria um novo comando que encapsula uma chamada a uma Stored Procedure. /// </summary> /// <param name="conn">A conexão que será utilizada</param> /// <param name="procedureName">O nome da Stored Procedure</param> public StoredProcedureCommand(AdpConnection conn, string procedureName) { this.cmd = conn.CreateCommand(); this.cmd.CommandType = CommandType.StoredProcedure; this.cmd.CommandText = procedureName; this.cmd.CommandTimeout = DefaultCommandTimeout; }
/// <summary> /// Cria um novo comando que encapsula uma chamada a uma Stored Procedure. /// </summary> /// <param name="conn">A conexão que será utilizada</param> /// <param name="procedureName">O nome da Stored Procedure</param> public StoredProcedureCommand(AdpConnection conn, string procedureName) { this.cmd = conn.CreateCommand(); this.cmd.CommandType = CommandType.StoredProcedure; this.cmd.CommandText = procedureName; this.cmd.CommandTimeout = DefaultCommandTimeout; }
/// <summary> /// Executa o salvamento propriamente dito. /// </summary> public void Execute() { sum.Reset(); try { if (dbConn != null) { CommitChanges(); } else { using (dbConn = conn.CreateConnection()) { dbConn.Open(); using (trans = dbConn.BeginTransaction(IsolationLevel.Serializable)) CommitChanges(); } } } catch (ConnectorException) { throw; } catch (Exception ex) { throw ConnectorExceptionFactory.FromDatabaseException(ex); } }
protected internal virtual AdpConnection CreateConnection(bool open) { string cs = null; try { cs = ConnectionString; cs = cs.Replace("${user}", DatabaseUser); cs = cs.Replace("${password}", DatabasePassword); // para compatibilidade cs = cs.Replace("{0}", DatabaseUser); cs = cs.Replace("{1}", DatabasePassword); AdpConnection conn = new AdpConnection(cs); if (open) { conn.Open(); } return(conn); } catch (Exception ex) { throw new ConnectorException(String.Format("Error while creating connection.{0}ConnectionString = {1}{0}User = {2}{0}Password=*******", Environment.NewLine, ConnectionString, DatabaseUser), ex); } }
/// <summary> /// Atualiza as informações das linhas especificadas no banco de dados. /// </summary> /// <param name="rows">As linhas a atualizar</param> private void CommitChanges(AdpConnection conn, AdpTransaction trans, params DataRow[] rows) { using (CommitRowChangesCommand cmd = new CommitRowChangesCommand(this, conn, trans)) { cmd.Rows = rows; cmd.Execute(); } }
/// <summary> /// Cria o comando de salvamento de alterações para o <see cref="ConnectorBase"/> /// especificado. /// </summary> /// <param name="conn">O <see cref="ConnectorBase"/> a ter seus dados salvos.</param> /// <param name="dbConn">A conexão a ser utilizada</param> /// <param name="trans">A transação ativa</param> public CommitChangesCommand(ConnectorBase conn, AdpConnection dbConn, AdpTransaction trans) : this(conn) { this.disposeDBObjects = false; this.dbConn = dbConn; this.trans = trans; }
/// <summary> /// Cria o comando de salvamento de alterações para o <see cref="ConnectorBase"/> /// especificado. /// </summary> /// <param name="conn">O <see cref="ConnectorBase"/> a ter seus dados salvos.</param> /// <param name="dbConn">A conexão a ser utilizada</param> /// <param name="trans">A transação ativa</param> public CommitChangesCommand(ConnectorBase conn, AdpConnection dbConn, AdpTransaction trans) : this(conn) { this.disposeDBObjects = false; this.dbConn = dbConn; this.trans = trans; }
/// <summary> /// Atualiza as informações das linhas especificadas no banco de dados. /// </summary> /// <param name="rows">As linhas a atualizar</param> protected void CommitChanges(params DataRow[] rows) { try { using (AdpConnection conn = CreateConnection()) CommitChanges(conn, null, rows); } catch (Exception ex) { HandleException(ex); } }
/// <summary> /// Preenche apenas as tabelas de dados especificadas. /// </summary> /// <param name="sources">Os nomes das origens. Formato de cada uma: <c>dataTable:tabela_ou_view</c></param> public int FillData(params string[] sources) { try { int c = 0; using (AdpConnection conn = CreateConnection()) { foreach (string rawInfo in sources) { FillInfo fi = new FillInfo(rawInfo); using (AdpDataAdapter da = new AdpDataAdapter()) { if (fi.Source == fi.DataTable && ds.Tables.Contains(fi.DataTable)) { DataTable dt = ds.Tables[fi.DataTable]; lock (dt) { string old = dt.TableName; dt.TableName = GetPhysicalTableName(dt); new AdpCommandBuilder(da, conn, dt, AdpCommandOperation.Select); c += da.Fill(dt); dt.TableName = old; } } else { da.SelectCommand = new AdpCommand("SELECT * FROM " + fi.Source, conn); if (!ds.Tables.Contains(fi.DataTable)) { Debug.WriteLine(String.Format("WARN: DataTable {0} doesn't exist.", fi.DataTable)); } c += da.Fill(ds, fi.DataTable); } } } } return(c); } catch (Exception ex) { throw new ConnectorException("Erro ao realizar o preenchimento dos dados", ex); } }
/// <summary> /// Finaliza os objetos necessários. /// </summary> protected virtual void Dispose(bool disposing) { if (disposing) { if (disposeDBObjects) { if (trans != null) { trans.Dispose(); trans = null; } if (dbConn != null) { dbConn.Dispose(); dbConn = null; } } } }
/// <summary> /// Preenche a tabela de dados especificada, com os critérios especificados. /// </summary> /// <param name="dataTable">O nome da tabela no DataSet que deverá ser preenchida.</param> /// <param name="query">A consulta de origem dos dados</param> public int FillData(string dataTable, ISqlQuery query) { string sql = null; try { #if DEBUG Debug.WriteLine("FillData"); Debug.IndentLevel++; Debug.WriteLine("Data Table: " + dataTable); Debug.WriteLine("SQL: " + Regex.Replace(query.ToString().Replace(Environment.NewLine, " "), "\\s+", " ")); Debug.IndentLevel--; #endif sql = query.ToString(); DataTable dt = ds.Tables[dataTable]; if (dt != null) { sql = Regex.Replace(sql, "FROM\\s+" + dataTable, "FROM " + GetPhysicalTableName(dt), RegexOptions.IgnoreCase); } using (AdpConnection conn = CreateConnection()) using (AdpDataAdapter da = new AdpDataAdapter()) { da.SelectCommand = new AdpCommand(sql, conn); return(da.Fill(ds, dataTable)); } } catch (Exception ex) { if (sql == null) { throw new ConnectorException("Erro ao realizar o preenchimento dos dados", ex); } else { throw ConnectorExceptionFactory.FromDatabaseException(ex, sql); } } }
protected internal virtual AdpConnection CreateConnection(bool open) { string cs = null; try { cs = ConnectionString; cs = cs.Replace("${user}", DatabaseUser); cs = cs.Replace("${password}", DatabasePassword); // para compatibilidade cs = cs.Replace("{0}", DatabaseUser); cs = cs.Replace("{1}", DatabasePassword); AdpConnection conn = new AdpConnection(cs); if (open) conn.Open(); return conn; } catch (Exception ex) { throw new ConnectorException(String.Format("Error while creating connection.{0}ConnectionString = {1}{0}User = {2}{0}Password=*******", Environment.NewLine, ConnectionString, DatabaseUser), ex); } }
protected internal virtual bool BuildCommands(AdpDataAdapter da, AdpConnection conn, DataTable dataTable) { string procINS = GetStoredProcedureForCommiting(dataTable, AdpCommandOperation.Insert), procUPD = GetStoredProcedureForCommiting(dataTable, AdpCommandOperation.Update), procDEL = GetStoredProcedureForCommiting(dataTable, AdpCommandOperation.Delete); if (procINS != null || procUPD != null || procDEL != null) { new AdpCommandBuilder(da, conn, dataTable); if (procINS != null) { new AdpCommandBuilder(da, conn, dataTable, AdpCommandOperation.Insert, true); da.InsertCommand.CommandType = CommandType.StoredProcedure; da.InsertCommand.CommandText = procINS; } if (procUPD != null) { new AdpCommandBuilder(da, conn, dataTable, AdpCommandOperation.Update, true); da.UpdateCommand.CommandType = CommandType.StoredProcedure; da.UpdateCommand.CommandText = procUPD; } if (procDEL != null) { new AdpCommandBuilder(da, conn, dataTable, AdpCommandOperation.Delete, true); da.DeleteCommand.CommandType = CommandType.StoredProcedure; da.DeleteCommand.CommandText = procDEL; } return(true); } return(false); }
/// <summary> /// Finaliza os objetos necessários. /// </summary> protected virtual void Dispose(bool disposing) { if (disposing) { if (disposeDBObjects) { if (trans != null) { trans.Dispose(); trans = null; } if (dbConn != null) { dbConn.Dispose(); dbConn = null; } } } }
protected internal virtual bool BuildCommands(AdpDataAdapter da, AdpConnection conn, DataTable dataTable) { string procINS = GetStoredProcedureForCommiting(dataTable, AdpCommandOperation.Insert), procUPD = GetStoredProcedureForCommiting(dataTable, AdpCommandOperation.Update), procDEL = GetStoredProcedureForCommiting(dataTable, AdpCommandOperation.Delete); if (procINS != null || procUPD != null || procDEL != null) { new AdpCommandBuilder(da, conn, dataTable); if (procINS != null) { new AdpCommandBuilder(da, conn, dataTable, AdpCommandOperation.Insert, true); da.InsertCommand.CommandType = CommandType.StoredProcedure; da.InsertCommand.CommandText = procINS; } if (procUPD != null) { new AdpCommandBuilder(da, conn, dataTable, AdpCommandOperation.Update, true); da.UpdateCommand.CommandType = CommandType.StoredProcedure; da.UpdateCommand.CommandText = procUPD; } if (procDEL != null) { new AdpCommandBuilder(da, conn, dataTable, AdpCommandOperation.Delete, true); da.DeleteCommand.CommandType = CommandType.StoredProcedure; da.DeleteCommand.CommandText = procDEL; } return true; } return false; }
public CommitTableChangesCommand(ConnectorBase conn, AdpConnection dbConn, AdpTransaction trans) : base(conn, dbConn, trans) { }
/// <summary> /// Executa o salvamento propriamente dito. /// </summary> public void Execute() { sum.Reset(); try { if (dbConn != null) CommitChanges(); else { using (dbConn = conn.CreateConnection()) { dbConn.Open(); using (trans = dbConn.BeginTransaction(IsolationLevel.Serializable)) CommitChanges(); } } } catch (ConnectorException) { throw; } catch (Exception ex) { throw ConnectorExceptionFactory.FromDatabaseException(ex); } }
public CommitRowChangesCommand(ConnectorBase conn, AdpConnection dbConn, AdpTransaction trans) : base(conn, dbConn, trans) { }
/// <summary> /// Atualiza as informações das linhas especificadas no banco de dados. /// </summary> /// <param name="rows">As linhas a atualizar</param> private void CommitChanges(AdpConnection conn, AdpTransaction trans, params DataRow[] rows) { using (CommitRowChangesCommand cmd = new CommitRowChangesCommand(this, conn, trans)) { cmd.Rows = rows; cmd.Execute(); } }