TO HOW: ??????? ?? .NET ??????? ?? ??? ?? ?????? ?????? ??? ????? C# .NET ServicedComponent ?? ????? ????

???? ?????? ???? ??????
???? ID: 316247 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

????????? ???? ?? ?? ??? ?? ??? ???? ?? ???? ?????? ????????? ?? ??? ?? .NET ??????? ?? ????? ???? ??? ????????? ????ServicedComponent????? ?? ???? ?? Microsoft SQL Server ????? ?? ??????? SqlClient .NET ??????? ?? ????? ???? ??, ?????? ?? ODBC ?? OLE DB .NET ???????? ??????? ?? ????? ?? ?????

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ?????? ?? ?????? ??? ?? ???? ???:
  • Microsoft Windows 2000 Professional, Microsoft Windows 2000 ?????, Microsoft Windows 2000 ????? ?????
  • Microsoft Visual Studio .NET
  • Microsoft SQL Server 7.0 ?? Microsoft SQL Server 2000

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

??? .NET Framework ???? ?? ?????? ?? ????: ????????? ??? ??? ?? ???? ??? ??? ?? ??? ???? ?? ??? ???? ????? ????? ???????? ??????, ?? ?? ???? ?? ??????? ?? ???????? ?? ??????? enlists ????????? ??? ??? ????????? ?????? ?? ???, ??? ???????? ??? ?? ?? ?? ??????? ??? ???? ???? ?? ????? ???? ?? ??? ADO.NET ?? ????? ???? ??, ?? ??????? ?? ??? ?????? ??????? ???? ?? ???????? ??? ????????? ??? ????? ??? ??? ???????? ??? ????????? ??? ????? ????? ??, ?? ?????? ??????? ?? ???????? ??? ?? enlists ????????? ??? ????????

??? ??? ??? ????: ????????? ?? ??? ?? ???? ?? ????? ???? ?? ??? ????? ????????? ?? ????? ????:
  1. ???? ????TransactionAttribute???? ??? ?????? ????: ????????? ?????? ?? ????????? ???? ?? ??? ???? ???? ?? ??? ???

    ????????? ?? ?????? ?? ????? ???? ?????TransactionOption????
  2. ???? ???? ?? deriveServicedComponent?????ServicedComponentCOM + ?????? ?? ????? ???? ???? ??? ???? ?? ???? ???? ???
  3. ????????? ???? ?? ??????? ??? ???? ??????? ????? ???? ???? ?? ??? ??? ??????? ??? ?? ??? ??????? ???? ?? ?????
  4. COM + ?????? ?? ??? ???? ???? ??? ??????? ??????? ?????

    ???:: ??? ???? ???? ?? ?? ???????? ??? ??????? ??????? ???? ?????? ?? ?????? ???????? ?? ???? ??, ??????? ???? ?? ??? ?? ?? ??? ???? ??? ?? ????????? ???? ????? ?? ?? ???? ???? ?? ?????? ??? ?? ??? ?????? ??? ??????? ?? ???????? ??? ?? ??????? ???? ?? ??? .NET ?????? ??????? ????? (Regsvcs.exe) ?? ????? ?????
??? ??????? ?? ????? ??? ?? ??? ???? ?? ???? ?? ??? ???? ???? ?? ???? ??? ???? ??????? ?? ??? Microsoft .NET Framework ?????? ?? ???????????? ??? ????? ???? ?????:
??? ??????? ?? ????? ??? ?? ??? ???? ?? ????
HTTP://MSDN.Microsoft.com/en-us/library/xc31ft41.aspx
?? ????????? ?? ???? ??? ???? ??????? ?? ??? Microsoft .NET Framework ?????? ?? ???????????? ??? ????? ???? ?????:
????: ????????? ?? .NET Framework ????
HTTP://MSDN.Microsoft.com/en-us/library/ms123400.aspx

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

  1. ????? C# .NET ??? ??? ??? ????? ????????? ????????? ????? ?? ???, ????? ????? ?? ???? ????:
    1. Visual Studio .NET ?? ??????? ?????
    2. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????.
    3. ???????? ?????????????? ????? ???, ????? ????????? C# ??????????? ???????????????? ??????????? ????,????? ??????????? ????????????????? ????-????? ????, ?? ???? ???OK.
    4. ?????? Explorer ???, DistributedTransaction.cs ?? ??? ??? Class1.cs ????? ?? ??? ??????
  2. DistributedTransaction.cs ????? ?? ??? ??? ??????
  3. ????? ????????????????? ??,?????? ??????, ?? ???? ??? ????? ?????? ??????:
    • System.EnterpriseServices
    • System.Data.dll

  4. AssemblyInfo.cs ????? ??? ??? ?? ????? ????????? ???? ???????:
    [assembly: AssemblyKeyFile("")]
    [assembly: AssemblyKeyName("")]
    					
  5. DistributedTransaction.cs ????? ??? ????? ??? ??????:
    using System;
    using System.Data.SqlClient;
    using System.EnterpriseServices;
    using System.Runtime.CompilerServices;
    using System.Reflection;
    
    [assembly: ApplicationName("DistributedTransaction")]
    [assembly: AssemblyKeyFileAttribute("..\..\DistributedTransaction.snk")]
    
    
    namespace DistributedTransaction
    {
       /// <summary>
       /// Summary description for Class1.
       /// </summary>
       class Class1
       {
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main(string[] args)
          {
             try
             {
                DistributedTran myDistributedTran = new DistributedTran();
                myDistributedTran.TestDistributedTransaction();
             }
             catch (System.Data.SqlClient.SqlException e)
             {
                System.Console.WriteLine("Transaction Aborted: Error returned: " + e.Message);
             }         
          }
       }
       /// <summary>
       /// Summary description for TestApp.
       /// </summary>
       [Transaction(TransactionOption.Required)]
       public class DistributedTran: ServicedComponent 
       {
          public DistributedTran()
          {
          }
          [AutoComplete]
          public string TestDistributedTransaction()
          {
             // The following Insert statement goes to the first server.
             // This Insert statement does not produce any errors. 
             String insertCmdSql = "Insert Into TestTransaction (Col1, Col2) Values (1,'Sql Test')";
    
             // The following Delete statement goes to the second server.
             // Because the table does not exist, this code throws an exception.
             String exceptionCausingCmdSQL = "Delete from NonExistentTable";
                         
             // The following connection strings create instances of two SqlConnection objects
             // to connect to two different SQL Server servers in your environment.
             // Modify the connection strings as necessary for your environment.
             SqlConnection SqlConn1 = new SqlConnection("Server=Server_Name;uid=User_Id;database=DatabaseName;pwd=Password");
             SqlConnection SqlConn2 = new SqlConnection("Server=Server_Name;uid=User_Id;database=DatabaseName;pwd=Password");
    
             try
             {
                SqlCommand insertCmd = new SqlCommand(insertCmdSql,SqlConn1);
                SqlCommand exceptionCausingCmd = new SqlCommand(exceptionCausingCmdSQL,SqlConn2);
       
                // This command runs properly.
                insertCmd.Connection.Open();
                insertCmd.ExecuteNonQuery();
    
    
                // This command results in an exception, which automatically rolls back
                // the first command (the insertCmd command).
                exceptionCausingCmd.Connection.Open();
                int cmdResult = exceptionCausingCmd.ExecuteNonQuery();
                
                SqlConn1.Close();
                SqlConn2.Close();
    
                Console.WriteLine("Hello");
                
             }
             catch (System.Data.SqlClient.SqlException ex)
             {
                // After you catch the exception in this function, throw it. 
                // The service component receives this exception and 
                // aborts the transaction. The service component then
                // throws the same exception, and the calling function
                // receives the error message.
                Console.WriteLine (ex.Message);
                throw (ex);
             }
             finally 
             {
                // Close the connection.
                if (SqlConn1.State.ToString() == "Open")
                   SqlConn1.Close();
    
                if (SqlConn2.State.ToString() == "Open")
                   SqlConn2.Close();
             }
    
             return "Success";
           
          }
    
       }
    }
    					
  6. ????? ???????????? ??,??? ??????.
  7. ????? ????,????????? ????? ????,????????????? ????? ????,Microsoft Visual Studio .NET?? ????? ????,????? Studio .NET ??????? ????-????? ????, ?? ???? ???????? Studio .NET ????? ?????????.
  8. ???? ????????? ??? ??????? ?????, ?? ??? ??????? ?? ????? ??? ?? ??? ???? ?? ???? ?? ??? ????? ???? ?????:
    DistributedTransaction.snk SN -k
  9. ???? ????????? ??????
  10. ????? ?????? ??? ????? SQL Server ????? ??? ?????:
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestTransaction]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[TestTransaction]
    GO
    
    CREATE TABLE [dbo].[TestTransaction] (
    	[Col1] [int] NULL ,
    	[Col2] [varchar] (100) NULL 
    ) ON [PRIMARY]
    GO 
    					
  11. ???? ????????? ?????? ????? ??? ?? (?? ???????? ??????? ??) ????? ?????? ????? ????? ??:
    ?? ????????? ??????: ??????: ?????? ???????? ?? ??? 'NonExistentTable'?
  12. SQL ????? ?????? ???????? ?????, ????? ??? ??????, ?? ??? ?????? ?? ????? ?? ??? F5 ?????:
    USE NORTHWIND;
    SELECT * FROM TestTransaction WHERE Col1=1 AND Col2='Sql Test'
    						
    ??? ???? ?? ?????? ???? ???? ??? ????????? ??????? ????????? ?????? ???? ??? ???
  13. ????? ??? ?? ???? ????????? ????? C# ??? ?????:
    String exceptionCausingCmdSQL = "Delete from NonExistentTable";
    						
    ?? SQL ??? ?? ?????? ???? ?? ??? ????????? ?? ???? ???? ??? ????? ?????? ?? ?????? ?????? ?? ???::
    String exceptionCausingCmdSQL = "Select @@Identity from customers";
    					
  14. ?????? ???? ?? ????????? ?? ???: ????? ?? ??? F5 ??????
  15. ???? ?? ??? 12 ?????? ???????? ??? ???? ?????? ????? ?? ?????? ???? ?????? ???? ?? ??????? ??? ????????? ?? ??????????? ????? ???? ??? ????? ???
???:
  • ?? ?????? ?????? ???????? ????? ???? ???
  • SQL ????? ?? Microsoft ?????? ?????? Coordinator (MS DTC) ??? ??????? ?? ????? ?? ?? ???? ??????

??????

???????? ??????? ?? ???, ???? ?? ???? ?????? ?? ????? ?? ???? ?? Microsoft ???????? ??? ?????::
306296TO HOW: ????? Serviced .NET ??? ?? ?? ????????? ??? ????? C# .NET ?? ????? ???? ??

???

???? ID: 316247 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
??????: 
kbcompiler kbhowtomaster kbsqlclient kbsystemdata kbmt KB316247 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:316247

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

 

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