HighTechTalks DotNet Forums  

Working with multiple typed datasets under same transaction

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss Working with multiple typed datasets under same transaction in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
bz
 
Posts: n/a

Default Working with multiple typed datasets under same transaction - 11-06-2007 , 10:33 AM






Hi,

I have two typed datasets I use on the same form. Each contains some
TableAdapters and corresponding data tables. The TableAdapters
contains, beside default Fill methods (to populate the DataTable) some
queries which returns scalar or some action queries (insert / delete)
which return nothing, just work on database

What I need is to perform some actions as above (various processing on
both datasets) but under the same transaction.

Typed dataset does not expose the Connection or Transaction property,
but I enhanced the Table Adapter classes for the tableadapters I want
to work with under same transaction as below, to assign a Connection:

namespace AssurantieService.BL.DataSets.AppSecurityDSTableAd apters
{

public partial class as_ExtAppSecurityTA :
System.ComponentModel.Component
{
/// <summary>
/// Set the connection to be able to work with transactions
/// </summary>
public void SetConnection(IDbConnection parConn)
{
foreach (SqlCommand cmd in _commandCollection)
cmd.Connection = (SqlConnection)parConn;
}
}
}

And I created a SessionManager class which basically creates a
connection, start transaction, I want to use the connection from this
class to assign to TA's Connection prop, and process all inside a
using statement

The SessionManager basically looks as below

public SessionManager(string connectionString)
{
try
{
prvDataConnection = new
SqlConnection(connectionString);
if (prvDataConnection.State != ConnectionState.Open)
prvDataConnection.Open();
}
catch (Exception ex)
{
ClearSession();
ErrorHandler.LogError(ex);
throw new SessionManagerException("ConnError"), ex);
}
}

public SessionManager(string connectionString, bool
useTransaction)
: this(connectionString)
{
if (useTransaction)
try
{
if (prvTransaction == null)
{
prvTransaction =
prvDataConnection.BeginTransaction();
prvInTransaction = true;
prvUseTransaction = true;
}
}
catch (Exception ex)
{
ClearSession();
ErrorHandler.LogError(ex);
throw new SessionManagerException("TranError"),
ex);
}
}

However, this is not working. When I execute some action queries or
scalar from tableadapters I get various error messages - Connection
for Command object is not set, etc

Can anyone help me? Or is there any other way to create a transaction
across different connections?

Thanks you


Reply With Quote
  #2  
Old   
Jim Rand
 
Posts: n/a

Default Re: Working with multiple typed datasets under same transaction - 11-06-2007 , 11:18 AM






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.Serializable))
{

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 */



Reply With Quote
  #3  
Old   
bz
 
Posts: n/a

Default Re: Working with multiple typed datasets under same transaction - 11-08-2007 , 05:56 PM



Thanks, I'll study it and see if I get something that might help.

Regards,
Bogdan

On 6 Noi, 18:18, "Jim Rand" <jimr... (AT) ix (DOT) netcom.com> wrote:
Quote:
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 */



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.