????? C# ??? ??? COM + ????????? ?? ????? ???? ????

???? ?????? ???? ??????
???? ID: 816141 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ??? ?? ??? ???? ????? ?? ?? ???? COM + ?? ????? ???? ?? ??? ??? ????? C# ???? ??? (??? ????) ?????????? ??????? ???????? ???? ?? ?? ??? ?? ???? ???? ???? ??? ??? ??????? ??? ??, ??? ?? ???????? ???? ???? ??, ?? ???? ????????? ????? Latter ?????? ??? ???? ?? ??????? ???????? ?? ?????? ???? ??? ?? ??? ???? ????? ?????????? ??????? ?? ????

??????????

????? ?????? ?? ????? ???????? ?????????, ??????????, ??????? ??????, ???? ?? ????? ?? ???? ??? ?????? ???? ???:
  • Microsoft Windows 2000 ????? SP1
  • Microsoft Internet Information Services (IIS) version 4.0 or later
  • Microsoft Internet Explorer versions 5.0, 5.5, or 6.0
This article assumes that you are familiar with the following:
  • Transactional concepts and processing
  • COM+ (Component Services)

COM+ Transaction Services

You can implement transaction processing with theSystem.EnterpriseServicesnamespace in the Microsoft .NET Framework. To access COM+ transactional services, create a class. ??? ???? ?? ???, ????? ????? ?? ???? ????::
  1. Visual Studio .NET ?? Visual Studio 2005 ??????? ?????
  2. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????.
  3. ????? ????,????? C# ??????????? ???????????????? ???????? ????-????? ????, ?? ???? ???????? ??????????? ???????????????. ????????? ?? ???prjEnterprise.

    ???:In Visual Studio 2005, click????? C#?? ???????????????? ???????? ????-????? ????, ?? ???? ???????? ??????????? ???????????????. ????????? ?? ???prjEnterprise.
  4. By default, Class1 is created.
  5. ?????? Explorer ???, ???? ????????????? ????-????? ????, ?? ???? ????????? ??????.
  6. The?????? ??????????? ?????,. ?? ?????????? ???????? ????.NETtab, double-clickSystem.EnterpriseServices.
  7. ????????? ???? ?? ??System.EnterpriseServices?? ??????? ????? ???? ??????? ???. ????? ????,OK.
  8. Add the following code before to any other statements in the Class1.cs file:
    using System.EnterpriseServices;
    using System.Data.SqlClient;
  9. Add a new class that is namedclsESto the Class1.cs file.
  10. COM + Transactional ?????? ?? ?????, ???? ???? (???? ?? ???clsES) ?????? inherit ??????????? ServicedComponent ?? ?? ??? ???:
    public class clsES : ServicedComponent
  11. A???????????????? ???? ?? ??? transactional ?????? ?? ???? ?? ?? ??? ??? ????????? ???? ?? ??? ????? ???? ???? ??:
    [Transaction(TransactionOption.Required)]public class clsES : ServicedComponent
  12. ?? ?????? ??? ?????clsES????, ?? ??? ???dbAccess?? ??? ????? ???????? ???????? ??????? ???? ??? ???? ?? ???????? ???? ?? ?????? ID ?? ??????? ?? ?? ?????? ?? ???? ??????? ?? ???????? ???? ?? ?????? ID ?? ????? ??? ??????? ?? ?????? ?? ???? This method performs a set of database operations against these specified product IDs that are to be treated as a transaction:
    void dbAccess(int pID1,int onOrder, int pID2, int inStock)
  13. ?????dbAccessmethod, create a SQL connection object for the Northwind database, and then open the connection. Database operations occur by using the following database:

    ???:Remember to change the following connection string parameters to reflect the correct values for your SQL Server server.
    SqlConnection Conn = new SqlConnection("user id=<username>;password=<strong password>;Initial Catalog=northwind;Data Source=2E124\\SQL;");
    				Conn.Open();
    
  14. Set a???? ?? ?????? ????block to capture any exceptions that might occur during database processing. You must catch these exceptions to abort the transaction. The???? ?? ?????? ????block includes two database operations. ???????? ???????? ???????? ?? ????????? ?????? ??????? ??? ???? ???? ????? ?? ?????? ???? ???
     try { 
    
  15. ?????? ???????? ?? ??? ???? ?????? ???? ???? ??????UnitsonOrder????? ??onOrder??? ??? ???? ?? ????? ???????? ????????? ?? ??? ??? ID, ?? ??? ?????? ?? ??? ??? ?? ?????? ?? SQL ????? ?? ??? ?????????? SQL ???? ?? ????? ????:
    SqlCommand sqlCommand = new SqlCommand("UPDATE myProducts SET UnitsonOrder = " + onOrder + " WHERE productID = " + pID1, Conn);
    				sqlCommand.ExecuteNonQuery();
  16. ?????? ???????? ?? ??? ??? ???? ?????? ???? ???? ??????UnitsinStock????? ??inStock??? ??? ????? ?? ???? ????? ???????? ????????? ?? ??? ??? ID, ?? ??? ?????? ?? ??? ??? ?? ?????? ?? SQL ????? ?? ??? ?????????? SQL ???? ?? ????? ????:
    sqlCommand.CommandText = "UPDATE myProducts SET UnitsinStock = " + inStock + " WHERE productID = " + pID2;
    				sqlCommand.ExecuteNonQuery();
    
  17. Because these updates are part of a COM+ transaction, they are committed as a unit. ThesetComplete?? ??? ????contextUtilclass from theSystem.EnterpriseServicesnamespace is used to commit the transaction (in this case the two updates) if no errors were thrown:
    ContextUtil.SetComplete();
  18. The connection to the Northwind database is closed:
    Conn.Close();
    }
  19. You must catch any exceptions that occur while running the SQL commands so that you can abort the whole transaction:
    catch(Exception e){ 
  20. ThesetAbort?? ??? ????contextUtilclass from theSystem.EnterpriseServicesnamespace is used to abort the whole transaction. ??? ???? ?????? ??? ?? ?? ????? ?????? ???? ???? ??, ?? ?? ?????? ???? ?? ??? ?????? ?????? ????? ??? Caught ????? ?? ????? ???? ?? ????????? ??? ????, ???? thrown ??:
    ContextUtil.SetAbort();
    				throw e;
    }
  21. ?? ??? ??? ?? ????? ???? ?? ???, ?? ??? ??? ?? ?? ????? ??? ???? ?????? ??? ?? ????? ??? ????? ????, ?? ???? ??? ??????? ????? ??? ?? ??? ???? ?? ????? ??? ???? ?? ???, ????? ????? ?? ???? ????::
    1. Visual Studio .NET ????? ????????? ?? ?????SN.exe -k snEnterprise.snk???? ????? ????? ?????? ??? ??? ?? ??? ????????? assemblies ?? ???? ??? ???? ??????? ?? ??? .NET Framework SDK ?? ???????? ??????
    2. SnEnterprise.snk ???? ????????? ??????? ??? ????????? ??????
    3. ??? AssemblyInfo.vc, ???? ?? ???? ?? ???? ??????? ??????? ????? ?? ??? ??? ?? ????? ?????? ??????:
      [assembly: AssemblyKeyFileAttribute("..\\..\\snEnterprise.snk")]		
    4. ??????, ?? ???? ??? ???? ????????? ??????

??? ??????? ?????

???:???? SQL Server ????? ?? ??? ??? ??? ?? ??????????? ???? ?? ??? ????? ??????? ???????? ?????????? ?? ????????? ???? ?? ??? ??? ?????
using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.Common;
using System.EnterpriseServices;
using System.Data.SqlClient;

namespace prjEnterprise
{
	
	[Transaction(TransactionOption.Required)]public class clsES:ServicedComponent
	{
		public SqlConnection Conn;

		public void dbAccess(int pID1, int onOrder, int pID2, int inStock)
		{
			try
			{			
				SqlConnection Conn = new SqlConnection("user id=<username>;password=<strong password>;Initial Catalog=northwind;Data Source=2E124\\SQL;");
				Conn.Open();
				
SqlCommand sqlCommand = new SqlCommand("UPDATE myProducts SET UnitsonOrder = " + onOrder + " WHERE productID = " + pID1, Conn);
				sqlCommand.ExecuteNonQuery();
				
				sqlCommand.CommandText = "UPDATE myProducts SET UnitsinStock = " + inStock + " WHERE productID = " + pID2;
				sqlCommand.ExecuteNonQuery();

				ContextUtil.SetComplete();
				Conn.Close();
			}
			catch(Exception e)
			{
				ContextUtil.SetAbort();
			
				throw e;
			}
			finally
			{

			}
		}
	}	
}

???? ???? ?? ??? ???

?? ??? ?? ??????? ?? ??? ?? clsES ????????? ?? ????? ???? ?? ????? ????????? ?????? ?? ?????? ??? ??? ????????? succeeds, ??onorder, ??instock????????? ?????? ?? ??? ??????? ?????? ?? ???? ???? ????? ?????? ???, ?? ??? ??????onOrder???? ????????? ?????? succeeds, ????? ?? ??? ?????? ?? ??? ?????inStockfield for a product fails because the specified product number does not exist in the Products table. This results in a transaction failure, and the transaction is ignored.
  1. In Visual Studio .NET or Visual Studio 2005, point to???????? ????????????, ?? ???? ????????????.
  2. ????? ????,????? C# ??????????? ???????????????? ???????? ????-????? ????, ?? ???? ???????? ??????????? ???????????????.

    ???:Visual Studio 2005, ??? ????? ????????? C#?? ???????????????? ???????? ????-????? ????, ?? ???? ???????? ??????????? ???????????????.
  3. ??????????? ????? ???, ??????testES. ????????? ???? ?? ??Add to Solution?????? ????? ???
  4. ????? ????,OKto add this project to the solution.
  5. ?? ???testESto testclsES, you must add a reference. ?????? Explorer ???, ???? ????????????? ???????testES(that you just added), and then click?????? ??????.
  6. The?????? ??????????? ?????,. ????? ???????????????? ??, ???-????? ????prjEnterprise.
  7. A reference appears underSelected Components. ????? ????,OKto add this reference to the project.
  8. Add a reference to the project to the System.EnterpriseServices library. ?????? Explorer ???, ???? ????????????? ????-????? ????, ?? ???? ????????? ??????.
  9. The?????? ??????????? ?????,. ?? ?????????? ???????? ????.NETtab, double-clickSystem.EnterpriseServices.
  10. ????????? ???? ?? ??System.EnterpriseServices?? ??????? ????? ???? ??????? ???. ????? ????,OK.
  11. Right-click the console application (testES), ?? ???? ??? ????? ????Set as Startup Project
  12. Paste the following source code in the?????function of theClass1class :
    	prjEnterprise.clsES myTest = new prjEnterprise.clsES();
    
    				try 
    				{
    					myTest.dbAccess(1, 777, 2, 888);
    					Console.WriteLine("TRANSACTION ONE -- SUCCESS");
    
    					myTest.dbAccess(1, 5, 2, -20);
    					Console.WriteLine("TRANSACTION TWO -- SUCCESS");
    				}
    				catch (Exception e)
    				{
    					Console.WriteLine("TRANSACTION FAILURE");
    				}
  13. Press F5 to run the test code.

    In the code in step 7, the first call todbAccesssucceeds. Product 1 and Product 2 are in the Products table. TheonOrderfield for Product 1 is updated to 777, and theinStockfield for Product 2 is updated to 888. Because this transaction succeeded, you receive the following message in the output window
    TRANSACTION ONE - SUCCESS


    The second call todbAccess???? ?? ???? ??? Therefore, neither update statement indbAccessto the Products table is posted to the database. Although Product 1 could have itsonOrderfield updated to5, Product 2 cannot have itsinStockfield set to-20. Because of a constraint that is defined in the Product table definition,inStockis not permitted to have negative numbers.

    Therefore, this call todbAccessfails, and the whole transaction fails. The Products table remains as it was before the call todbAccess. The???statement handles notification of the transaction failure fromdbAccess, and you receive the following error message in the output window:
    ????????? ??????
  14. SQL ????? ?????????? ??????? ?? ????? ???? ????????? ?????? ?????? ?? ??????? ?? ??????? ????? ?? ?? ?????? 1 ?? ???, ?????onOrder????? 777 ?? ????? ??? ?? ?? ?????? 2, ?????instock????? ?? 888 ??? ?????, ????? ??? ???? ?? ???dbAccess???? (????? ???????????? ?? ????????? ?? ??? ????? ???)?

?????? ??????

  • ????????? ???? ?? ??? ????????? COM + ?????? ?? ????? ???? ?? ?? ?? ????? ??? ???
  • COM + ?????? ?? ????? ???? ?? ?? ???? ?? ???? ?? serviced ??? inherit ???? ?????? ??? Serviced ??? ???? ??System.EnterpriseServices??? ????? ???
  • ???????, ???? ??? ??? ????????? ?? ???? ?? ??? ???? ?? ???? ??? ???? ?? ?????? ???? ??? ??? ??? ??????? ?? ????, ?? ??? ????????? ??????? ?? ??? ??????? ??? ?? ?????? ????? ?????? ???, ??????? ?????? ?? ??? ?? ???? ??? ????????? ?? ???? ???? ?? ??? 1,200 ?????:
    [Transaction(TransactionOption.Required,timeout=1200)]

??????

???????? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ?????? ?? ????:
COM + ???????: ???? .NET ?????????? ???? ?? ???? ??? ???? ????? ????????? ?????? ????
HTTP://MSDN.Microsoft.com/msdnmag/issues/01/10/complus/default.aspx
Serviced ???
(VS.71) http://msdn2.Microsoft.com/en-us/library/Aa289839 .aspx

???

???? ID: 816141 - ????? ???????: 05 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
??????: 
kbcomplusobj kbsqlprog kbsqlclient kbdatabase kbhowtomaster kbmt KB816141 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:816141

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com