Torna al Thread

//a valore di livello di modulo metto pubbliche la stringa di connessione e l'oggetto connection private string vConnectionString = ConfigurationSettings.AppSettings["connectionString"].ToString(); private OdbcConnection vConnection = new OdbcConnection(vConnectionString); //qui leggo e mi restituisce un dataset (mi sebra più carino usare una funzione per restituire il dataset piuttosto che passarlo per riferimento) // ho tolto pTableName che non veniva usato public DataSet DBRead(string pReadCondition) { //se la connessione è chiusa la apre if (vConnection.State == ConnectionState.Closed) vConnection.Open(); } OdbcCommand vCommand = new OdbcCommand(pReadCondition,(OdbcConnection)vConnection); OdbcDataAdapter vcAdapter = new OdbcDataAdapter(); vcAdapter.SelectCommand = vCommand; //-- le seguenti servono solo in caso è possibile l'aggiornamento -- OdbcCommandBuilder cb = new OdbcCommandBuilder(vcAdapter); cb.RefreshSchema(); vcAdapter.DeleteCommand = cb.GetDeleteCommand(); vcAdapter.InsertCommand = cb.GetInsertCommand(); vcAdapter.UpdateCommand = cb.GetUpdateCommand(); //------------------------------------------------------------------ DataSet vDataSet = new DataSet(); try { vcAdapter.Fill(vDataSet); } catch(Exception e) { Console.Writeline(e.Message); } finally { //chide la connsessione ma non metto a null vConnection.Close(); } return = vDataSet; } //questa aggiorna le righe e restituisce il totale delle righe aggiornate public int DBUpdate(DataSet pDataset, out bool pStatus) { int rowInserted = 0; int rowUpdated = 0; int rowDeleted = 0; pStatus = true; DataTable dt = pDataset.Tables[0]; try { rowInserted = da.Update(dt.Select(null,null,DataViewRowState.Added)); rowUpdated = da.Update(dt.Select(null,null,DataViewRowState.ModifiedCurrent)); rowDeleted = da.Update(dt.Select(null,null,DataViewRowState.Deleted)); pDataset.AcceptChanges(); } catch(Exception e) { ds.RejectChanges(); pStatus = false } finally {cn.Close(); } return rowInserted + rowUpdated + rowDeleted; } //libero le risorse qaundo ho finito di utiliz<zare la classe public void Dispose() { cn = null; vConnectionString = null; }
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5