![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
I use data adapters instead of table adapters. Here is some code that uses transactions - might give you some ideas ---------------------------------------------------------------------- /* Update all tables in the dataset */ internal static void Update(System.Data.DataSet ds, System.Collections.Generic.List<System.Data.SqlCli ent.SqlDataAdapter adapterList, System.Collections.Generic.Dictionary<string, string[] stringColumnDictionary, int eventID, Guid userGuid) { SqlConnection cn = adapterList[0].SelectCommand.Connection; try { cn.Open(); /* Use the first dataAdapter's connection to start the transaction */ using (SqlTransaction tran = adapterList[0].SelectCommand.Connection.BeginTransaction(Isolati onLevel.Ser*ializable)) { try { /* Enlist transaction for each adapter */ foreach (SqlDataAdapter da in adapterList) { if (da.UpdateCommand != null) da.UpdateCommand.Transaction = tran; if (da.DeleteCommand != null) da.DeleteCommand.Transaction = tran; if (da.InsertCommand != null) da.InsertCommand.Transaction = tran; } System.Data.DataSet dsDeleted = ds.GetChanges(System.Data.DataRowState.Deleted); System.Data.DataSet dsAdded = ds.GetChanges(System.Data.DataRowState.Added); System.Data.DataSet dsModified = ds.GetChanges(System.Data.DataRowState.Modified); /* Replace zero length strings with nulls */ if (stringColumnDictionary != null) { if (dsAdded != null) { ReplaceWithNulls(dsAdded, DataRowState.Added, stringColumnDictionary); } if (dsModified != null) { ReplaceWithNulls(dsModified, DataRowState.Modified, stringColumnDictionary); } } UpdateOperation(dsDeleted, adapterList, System.Data.DataRowState.Deleted); UpdateOperation(dsAdded, adapterList, System.Data.DataRowState.Added); UpdateOperation(dsModified, adapterList, System.Data.DataRowState.Modified); /* Check the event in */ string sql = "UPDATE dbo.CheckOut " + "SET Upload = GetUTCDate() " + "WHERE EventID = @EventID AND UserGUID = @UserGUID AND Upload Is Null"; SqlCommand cmd = new SqlCommand(sql, tran.Connection); cmd.Transaction = tran; SqlParameter param = cmd.Parameters.Add("@EventID", SqlDbType.Int); param.Value = eventID; param = cmd.Parameters.Add("@UserGUID", SqlDbType.UniqueIdentifier); param.Value = userGuid; cmd.ExecuteNonQuery(); cmd.Dispose(); /* Commit the transaction */ tran.Commit(); if (dsDeleted != null) ds.Merge(dsDeleted, false); if (dsAdded != null) ds.Merge(dsAdded, false); if (dsModified != null) ds.Merge(dsModified, false); ds.AcceptChanges(); } catch (Exception ex) { if (tran != null) tran.Rollback(); throw ex; } } } catch (Exception ex) { throw ex; } finally { if ((cn != null) && (cn.State == ConnectionState.Open)) cn.Close(); } } /* internal static void Update */ |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |