Use COM + to implement transaction control in C #

xiaoxiao2021-03-06  68

.NET technology is the next-generation platform technology that Microsoft vigorously promotes, since the official release of the .NET technology architecture beta2 version, this technology is gradually maturing and stabilizing. According to Microsoft's platform system, we are not difficult to imagine, in the next two years. NET technology must be trending the mainstream technical platform, and a new technical platform is the most important premise of rapid development is : He will not completely abandon the previous technology, this point is COM / COM technology for .NET technology. In general, in the IT technology circle and hardware industry, the upgrade speed of technology is very amazing, and the practice is all new technologies to follow the principle of downward compatibility, but .NET technology not only did this, .NET or even Implementation of each call between each other, this is very difficult to be valuable. That is, not only we can call COM components in the .NET component, but also call the .NET component in the COM component. The benefits of this point are obvious, on the one hand, we can maintain existing technical resources, on the other hand, in the existing resources, can take advantage of the various new technologies brought by .NET. The average database transaction control requires the operations in the transaction must be in the same database so that it can roll back (rllback) to the initial state when an error occurs. There is a problem. In distributed applications, we often need to operate multiple databases simultaneously, using the transaction processing of the database itself, is difficult to meet the requirements of transaction control. In COM , a complete service service is provided, we can use it to complete transaction control in distributed applications. The specific process is as follows: Generate a class library with vs.net.

2: Add a reference to System.EnterPristServices, specific steps menu: (Project - Add Reference - Select System.EnterPristServices - OK) 3: Build Class 1: Source Program Using System; Using System.EnterpriseServices; Using System.EnterpriseServices; .DATA.SQLCLIENT; Using System.reflection; Namespace ComplusSamples {// Indicates that requires transaction support [Transaction (TransactionOption.Required) // Declaration as a server application, you can also choose library, expressed as library application [Assembly: ApplicationActivation) ActivationOption.Server)] // description [assembly: description ( "sample")] public class TxCfgClass: ServicedComponent {private static string init1 = "user id = sa; password =; initial catalog = pubs; data source = (local) "; private static string init2 =" user id = sa; password =; initial catalog = NorthWind; data source = (local) "; private static string add1 =" insert into authors ( 'au_lname', 'au_fname') values ​​( ' TEST1 ',' TEST2 ') "; Private Static String Add2 =" Insert INTO SAMPLE VALUES (' Test1 ', 22) "; // The Error Sql Statement /////plic txcfgclass () {} private Void Execsql (String Init, String SQL) {SqlConnection Conn = New SqlConnection (in IT); SQLCommand cmd = conn.createCommand (); cmd.commandtext = SQL; conn.open (); cmd.ExecutenonQuery (); conn.close ();} // Add a record to database public void add () { Try {// Insert a record of execSQL (INIT1, ADD1) in a database; "The Operation In The Same Database Completely"); / / Insert two records // this execution in another database Is a wrong SQL statement execSql (init2, add2); console.writeline ("THE OPERATIONITELY"); Console.Writeline ("Record (s) added, press ..."); console.read ();

} Catch (Exception E) {// Transaction rollback contextutil.Setabort (); console.writeline ("Because There Some Errors in The Operation, SO Transcation Abort"; console.writeline ("The Error IS" E. Message); console.writeline ("Abort successful"); console.read ();}}}} 2: Program Description: Add Namespace for System.EnterpriseServices; because this program uses the ContextUtil class [Transaction. Required)] Description DLL requires transaction to support the TXCFGClass class of this program from the ServicesDComponent class, which does not affect the class, but only add two additional methods to this class, these two methods can make code sharing It is easy to use the SQL Server database that is easy to use in this machine, init1, and init2 are two connection strings for two connection databases, init connection Pubs database, ININ2 Connect the Northwind database, which is the sample database in SQL2000. Add1 and add2 are two SQL statements that add a record to a table of two databases, respectively. Note: Add2 is a wrong statement because there is no SAMPLE table at all, so that it will cause an exception when executed. (This is exactly what we expect) when executed, because it is wrong, so it will trigger an exception and go to the error handling statement. ContextUtil.setabort (); This statement allows all databases to roll back so that the records inserted by the add1 statement will not exist. (To achieve the expected target) 4: Strong Name 1: Creating a pair of keys to create a key to create a sharing tool called sn.exe. It is usually run by command prompt, which can perform various tasks to generate and extract the key. We need to run sn.exe in the following ways. Sn -k key.snk where the key.snk represents the name of the file that will save the key. Its name can be arbitrary, but it is accustomed to with a. SNK suffix name. 2: Signature signature is usually done at compile. When signing, the user can use the C # Properties to notify the compiler to sign the DLL using the correct key file. To do this user needs to open the AssemblyInfo.cs file in the project and modify it. [Assembly: askMBLYKEYFILE ("..//// Key.snk"] Note: Key.snk file and project files in the same folder 5: Compile into DLL (specific steps) menu: (Generate - Generate) If everything is normal, you will generate a DLL file. Six: Use regsvcs.exe to register a DLL to COM Services. We need to run the regsvcs.exe regsvcs dll file name in the following ways. If everything is normal, regsvcs.exe will input the DLL to COM SERVICES.

转载请注明原文地址:https://www.9cbs.com/read-121276.html

New Post(0)