Exporting an ADO.NET Connection to a Third-Party .NET
Assembly
To export an ADO.NET connection from a PowerBuilder application,
use the GetAdoConnection method:
|
1 |
oleobject GetAdoConnection() |
The method returns an instance of IAdoConnectionProxy. The proxy’s
ADO connection object is assigned to property
IAdoConnectionProxy.Connection.
When a transaction starts, the proxy’s active Transaction object
is assigned to property IAdoConnectionProxy.Transaction, and
AutoCommit is false. When AutoCommit is true, the exported
IAdoConnectionProxy.Transaction is null..
The method returns null if the connection fails, and false if the
operation fails.
To use the shared connection, your third-party assembly must
reference the exported connection proxy and manage the transaction. To
be notified when the active transaction is changed, you can subscribe
the IAdoConnection.TransactionChanged event . Remember to close the
connection.
Sample PowerScript Code
|
1 2 3 4 5 6 7 8 |
//Sample PowerScript code SQLCA.DBMS = "ADO.NET" SQLCA.AutoCommit = false SQLCA.DBParm = "Namespace='System.Data.Odbc', DataSource='SQL Anywhere 11 Demo'" Connect Using SQLCA; emp.ConnectionProxy = SQLCA.GetAdoConnection() // db operations disconnect using SQLCA; |
Sample C# Code
Here is an example of C# code in the third-party assembly:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Manage the transaction public class Emp { ... IAdoConnectionProxy proxy; IDbTransaction trans; ... public object ConnectionProxy { get { return proxy; } set { proxy = value as IAdoConnectionProxy; ... proxy.TransactionChanged += new EventHandler(proxy_TransactionChanged); } } void proxy_TransactionChanged(object sender, EventArgs e) { ... trans = sender as IDbTransaction; ... } ... } |