Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 / 31 ADO.NET, l'utilizzo pratico nelle applicazioni Silvano Coriani Developer & Platform Evangelism MCTrainer MCSDeveloper MCADeveloper MCSEInternet.

Similar presentations


Presentation on theme: "1 / 31 ADO.NET, l'utilizzo pratico nelle applicazioni Silvano Coriani Developer & Platform Evangelism MCTrainer MCSDeveloper MCADeveloper MCSEInternet."— Presentation transcript:

1 1 / 31 ADO.NET, l'utilizzo pratico nelle applicazioni Silvano Coriani Developer & Platform Evangelism MCTrainer MCSDeveloper MCADeveloper MCSEInternet MCDBA

2 2 / 31 Da dove iniziare...

3 3 / 31 Agenda

4 4 / 31 Popolare il DataSet MissingSchemaActionMissingSchemaAction

5 5 / 31 Popolare il DataSet unicounaunicotutteunicounaunicotutte

6 6 / 31 Popolare il DataSet

7 7 / 31 Popolare il DataSet ExecuteXmlReader()ExecuteXmlReader()

8 8 / 31 Popolare il DataSet

9 9 / 31 Popolare il DataSet

10 10 / 31 Semplici aggiornamenti in ADO.NET DataAdapter [C#] SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers",nwindConn); SqlCommandBuilder custCB = new SqlCommandBuilder(custDA); nwindConn.Open(); DataSet custDS = new DataSet(); custDA.Fill(custDS, "Customers"); // Modifico il dataset // Senza il SqlCommandBuilder lUpdate fallirebbe custDA.Update(custDS, "Customers"); nwindConn.Close(); [C#] SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers",nwindConn); SqlCommandBuilder custCB = new SqlCommandBuilder(custDA); nwindConn.Open(); DataSet custDS = new DataSet(); custDA.Fill(custDS, "Customers"); // Modifico il dataset // Senza il SqlCommandBuilder lUpdate fallirebbe custDA.Update(custDS, "Customers"); nwindConn.Close();

11 11 / 31 Semplici aggiornamenti in ADO.NET valori originaliCommandBuilder li ricava dal comando di Select Noi conosciamo la logica di aggiornamento dei dati meglio del CommandBuilder Costruiamoli da codice o facciamoli costruire da VS.NET

12 12 / 31 Come funziona il metodo Update? DB Select Insert Update Delete CustomerDataTable Customers

13 13 / 31 Creazione manuale dei comandi del DataAdapter RowVersion

14 14 / 31 Comando di Update del DataAdapter string strSQL = "SELECT CustomerID, CompanyName FROM Customers; SqlConnection cnn = new SqlConnection(Server=(local);...); SqlDataAdapter = new SqlDataAdapter(str,cnn); string strSQLUpd = "UPDATE Customers SET CustomerID = ?, CompanyName = ? WHERE CustomerID = ? AND CompanyName = ?; da.UpdateCommand = new SqlCommand(strSQLUpd, cnn) da.UpdateCommand.Parameters.Add(new SqlParameter("NewCustomerID", SqlDbType.WChar, 5,ParameterDirection.Input, false, 0, 0, "CustomerID", DataRowVersion.Current, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("NewCompanyName", SqlDbType.VarWChar, 40, ParameterDirection.Input, false, 0, 0, "CompanyName", DataRowVersion.Current, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("OldCustomerID", SqlDbType.WChar, 5, ParameterDirection.Input, false, 0, 0, "CustomerID", DataRowVersion.Original, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("OldCompanyName", SqlDbType.VarWChar, 40, ParameterDirection.Input, false, 0, 0, "CompanyName", DataRowVersion.Original, null)); string strSQL = "SELECT CustomerID, CompanyName FROM Customers; SqlConnection cnn = new SqlConnection(Server=(local);...); SqlDataAdapter = new SqlDataAdapter(str,cnn); string strSQLUpd = "UPDATE Customers SET CustomerID = ?, CompanyName = ? WHERE CustomerID = ? AND CompanyName = ?; da.UpdateCommand = new SqlCommand(strSQLUpd, cnn) da.UpdateCommand.Parameters.Add(new SqlParameter("NewCustomerID", SqlDbType.WChar, 5,ParameterDirection.Input, false, 0, 0, "CustomerID", DataRowVersion.Current, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("NewCompanyName", SqlDbType.VarWChar, 40, ParameterDirection.Input, false, 0, 0, "CompanyName", DataRowVersion.Current, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("OldCustomerID", SqlDbType.WChar, 5, ParameterDirection.Input, false, 0, 0, "CustomerID", DataRowVersion.Original, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("OldCompanyName", SqlDbType.VarWChar, 40, ParameterDirection.Input, false, 0, 0, "CompanyName", DataRowVersion.Original, null));

15 15 / 31 [SQL Code] CREATE PROCEDURE UpdateCustomer nvarchar(40)) AS UPDATE Customers SET CustomerID CompanyName WHERE CustomerID AND CompanyName [ADO.NET Code] da.UpdateCommand.CommandText = "UpdateCustomer; da.UpdateCommand.CommandType = CommandType.StoredProcedure; da.UpdateCommand.Parameters.Add(new SqlParameter("NewCustomerID", SqlDbType.WChar, 5,ParameterDirection.Input, false, 0, 0, "CustomerID", DataRowVersion.Current, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("NewCompanyName", SqlDbType.VarWChar, 40, ParameterDirection.Input, false, 0, 0, "CompanyName", DataRowVersion.Current, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("OldCustomerID", SqlDbType.WChar, 5, ParameterDirection.Input, false, 0, 0, "CustomerID", DataRowVersion.Original, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("OldCompanyName", SqlDbType.VarWChar, 40, ParameterDirection.Input, false, 0, 0, "CompanyName", DataRowVersion.Original, null)); [SQL Code] CREATE PROCEDURE UpdateCustomer nvarchar(40)) AS UPDATE Customers SET CustomerID CompanyName WHERE CustomerID AND CompanyName [ADO.NET Code] da.UpdateCommand.CommandText = "UpdateCustomer; da.UpdateCommand.CommandType = CommandType.StoredProcedure; da.UpdateCommand.Parameters.Add(new SqlParameter("NewCustomerID", SqlDbType.WChar, 5,ParameterDirection.Input, false, 0, 0, "CustomerID", DataRowVersion.Current, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("NewCompanyName", SqlDbType.VarWChar, 40, ParameterDirection.Input, false, 0, 0, "CompanyName", DataRowVersion.Current, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("OldCustomerID", SqlDbType.WChar, 5, ParameterDirection.Input, false, 0, 0, "CustomerID", DataRowVersion.Original, null)); da.UpdateCommand.Parameters.Add(new SqlParameter("OldCompanyName", SqlDbType.VarWChar, 40, ParameterDirection.Input, false, 0, 0, "CompanyName", DataRowVersion.Original, null));

16 16 / 31 Aggiornamenti sulloggetto DataTable Per inserire un record sulloggetto DataTable –AddRow()Detached –Rows.Add(DataRow)Added –AcceptChanges() esegue la commit di tutte le modifiche nella cache locale. Riporta a UnChanged le righe inserite e modificate. Rimuove le eliminate. –RejectChanges()

17 17 / 31 Aggiornamenti sulla DataTable Current HasVersion()RowUpdated

18 18 / 31 Aggiornamenti sulla DataTable RowState –RowErrorHasErrorstrue

19 19 / 31 Aggiornamenti sulla DataTable Select()DataRows –DataViewRowState

20 20 / 31 Merge di DataSet stato

21 21 / 31 Gestione dei conflitti

22 22 / 31 Gestione della concorrenza con ADO.NET Timestamp

23 23 / 31 Transazioni in ADO.NET Transazioni distribuite –Utilizzando gli EnterpriseServices di.NET o COM+ –La connessione al singolo DB viene arruolata dal MS DTC allinterno del contesto di una transazione –Utilizza two-phase commit su Ole Transaction o XA per i vari Resource Manager –Da usare solo quando veramente necessario Transazioni locali –Allinterno del codice SQL delle Stored Procedure BEGIN/COMMIT TRAN –SqlTransaction

24 24 / 31 Transazioni in ADO.NET [C#] SqlConnection conn = new SqlConnection(); conn.Open("..."); SqlTransaction tx = conn.BeginTransaction(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "..."; cmd.Transaction = tx; cmd.ExecuteNonQuery(); tx.Commit();

25 25 / 31 Concorrenza ottimistica in SQL Server ts_equal()

26 26 / 31 Concorrenza ottimistica in ADO.NET Update()DBConcurrencyExceptionContinueUpdat eOnErrorHasError

27 27 / 31 Concorrenza ottimistica in ADO.NET RowUpdated [C#] daMain.RowUpdated += new SqllRowUpdatedEventHandler(HandleRowUpdated); … private void HandleRowUpdated(object sender, SqlRowUpdatedEventArgs e) { if ((e.Status == UpdateStatus.ErrorsOccurred) && (e.Errors.GetType == typeof(DBConcurrencyException)) { daConflict.SelectCommand.Parameters[0].Value = e.Row["ID"]; int intRowsReturned = ConflictAdapter.Fill(ConflictDataSet); if (intRowsReturned == 1) e.Row.RowError =The row has been modified by another user."; else e.Row.RowError = "The row no longer exists in the database."; e.Status = UpdateStatus.Continue;// determina se proseguire o no }

28 28 / 31 Re-fetching di una riga dopo lupdate UpdateRowSource INSERT INTO Orders (CustomerID, EmployeeID, OrderDate) VALUES (?, ?, GetDate()); SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM Orders WHERE OrderID = INSERT INTO Orders (CustomerID, EmployeeID, OrderDate) VALUES (?, ?, GetDate()); SELECT OrderID, CustomerID, EmployeeID, OrderDate FROM Orders WHERE OrderID =

29 29 / 31 Aggiornamento di dati gerarchici Select()DataRowState.AddedDataRowState.Delet ed

30 30 / 31 Lavorare con i campi di tipo IDENTITY AutoIncrementSeedAutoIncrementStepDataTable Select()DataRowUpdate()

31 31 / 31 Link utili

32 32 / 31 News

33 33 / 31


Download ppt "1 / 31 ADO.NET, l'utilizzo pratico nelle applicazioni Silvano Coriani Developer & Platform Evangelism MCTrainer MCSDeveloper MCADeveloper MCSEInternet."

Similar presentations


Ads by Google