????? ??????? ????????? COM + ?? ???? Visual C++ .NET ?? ?? ???? Visual C++ 2005

?????? ????????? ?????? ?????????
???? ???????: 815814 - ??? ???????? ???? ????? ????? ??? ???????.
????? ???? | ?? ????

?? ??? ??????

????

???? ??? ??????? ???? ????? ????? ??????? COM + (????? ????????) ????????? ?? ??? Microsoft Visual C++ .NET ?? ?? ??? Microsoft Visual C++ 2005. ????? ?????? ?? ?????? ????? ???? ?????. ??? ?? ???? ???? ???????? ?? ?? ???? ??? ????? ????? ??? ???????? ???????. ?? ?????? ???????? ?? ??? ??? ??? ?????? ????? ???????? ???? ?? ?????? ??? ????? ???????? ????????.

???????

????? ??? ??????? ??? ??????? ??? ???????? ???????:
  • ?????? ????????? ???????
  • COM + (????? ????????)

????? ????????? COM +

????? ????? ???????? ?????? ?????? System.EnterpriseServices ?? Microsoft .NET Framework. ?????? ??? COM + ????? ????? ????????? ???. ?????? ????? ???? ??????? ???????:
  1. ??? ????? Visual Studio .NET ?? Visual Studio 2005.
  2. ?? ??????? ???? ??? ??? ???? ?? ???? ??? ???????.
  3. ??? ????? Project ???? ??? ?????? Visual C++ ? ?? ???? ??? ????? ??? (.NET) ??? ?????. ??? prjEnterprise ???????.

    ?????? ?? Visual Studio 2005 ? ???? ??? Visual C++ ??? ????? Project ?? ???? ??? ????? ?????? ??? ?????.
  4. ?? "?????? ??????" ???? ??? ?????? ?????? ??? ????? ??? ?? ???? ??? ????? ????.
  5. ???? ??? ????? ??????? NET ?? ???? ?????? ????? ??????.
  6. ???? ????? ??????? ??? System.EnterpriseServices ??? ??? ??????.
  7. ???? ?? ???? System.EnterpriseServices ??? ?????? ??????? ?? ???? ??? ?????.
  8. ?? ?????? ????????? ???????? ??????? ??? ??? ?????? ???? ?? ??? prjEnterprise.h:
    using namespace System;
    using namespace System::Data;
    using namespace System::Data::SqlTypes;
    using namespace System::Data::Common;
    using namespace System::EnterpriseServices;
    using namespace System::Data::SqlClient;
    
  9. ????? ??? ????? ???? clsES ??? ??? prjEnterprise.h.
  10. ???????? "?????" COM + ??????? ???? ?? ?? ????? ??????? (clsES) ??? ????? ?? ??? ServicedComponent ??? ???:
    public __gc class clsES:public ServicedComponent
    
  11. ??????? ??? ????????? ?????? ????? ??? ????????? ????? ??? ???:
    [Transaction(TransactionOption::Required,Timeout=5)]public __gc class clsES:public ServicedComponent
    
  12. ????? ????? ?? ??? clsES ????? ????? ?????? ????? ??? ????. ??? dbAccess ???. ???? ???????? ??????? ???? ???? ?????? ????? ???? ??????. ????? ?????? ????????? ???? ???? ??????? ???????? ??????? ???? ??????. ??? ??????? ?????? ?????? ?? ?????? ????? ???????? ?? ?????? ?????? ?????? ??? ???? ??? ??????? ??? ??? ??????:
    public:	void dbAccess(int pID1, int onOrder, int pID2, int inStock)
  13. ?? ????? dbAccess ????? ???? ????? SQL Northwind ????? ???????? ?? ?? ???? ???????. ???? ?????? ????? ???????? ???????? ????? ???????? ???????:
    SqlConnection * Conn = new SqlConnection("user id=<username>;password=<password>;Initial Catalog=northwind;Data Source=<Your SQL Server name>;");
    Conn->Open();
    ???? ?????? ?????? ?????? ????? ??????? ???? ????? ??????? ????? SQL Server.
  14. ????? ???? ???? ?????? ?? ??????????? ???? ?? ???? ????? ?????? ????? ????????. ??? ?? ???? ??? ??????????? ?????? ???????. ????? ???? ?????? ????? ?? ?????? ????? ????????. ?? ????? ????? ??? ????? ?? ??? ???? ?????? ????.
    try { 
    
  15. ????? ??????? ????? ??? ???? ????????. ?? ?????? ????? ???????_???????? ????? onOrder ?????? ?? ???? ? ??? ?? ???? ?? ?????? ??????? ???????. ??????? ??? SQL ?????? ?????? ??? ??????? SQL:
    SqlCommand * sqlCommand = new SqlCommand("UPDATE Products SET UnitsonOrder = @onOrderString WHERE productID = @pID1String ", Conn);
    sqlCommand->Parameters->Add(new SqlParameter("@onOrderString",SqlDbType::VarChar ,
    	40,ParameterDirection::Input,true,0,0,"Description",DataRowVersion::Current,onOrder.ToString()));
    sqlCommand->Parameters->Add(new SqlParameter("@pID1String", SqlDbType::VarChar,
    	40,ParameterDirection::Input,true,0,0,"Description",DataRowVersion::Current,pID1.ToString()));
    sqlCommand->ExecuteNonQuery();
    
  16. ????? ????? ??? ??? ???? ????????. ????? ????? ???????_??????? ?? inStock ???? ?????? ?? ???? ? ??? ?? ???? ?? ?????? ??????? ?????? ???????. ??????? ??? SQL ?????? ?????? ??? ??????? SQL: sqlCommand
    sqlCommand->Parameters->Add(new SqlParameter("@inStockString",SqlDbType::VarChar ,
    	40,ParameterDirection::Input,true,0,0,"Description",DataRowVersion::Current,inStock.ToString()));
    sqlCommand->Parameters->Add(new SqlParameter("@pID2String", SqlDbType::VarChar,
    	40,ParameterDirection::Input,true,0,0,"Description",DataRowVersion::Current,pID2.ToString()));
    sqlCommand->ExecuteNonQuery();
    
  17. ??? ??? ????????? ??? ?? ?????? COM + ? ??? ??????? ?????. ??? ??????? ????? setComplete ??? contextUtil ?? ????? ????? System.EnterpriseServices ???????? ???????? (?? ??? ?????? ????? ??????) ?? ???? ??? ???? ????? ?? ???:
    ContextUtil::SetComplete(); 
    
  18. ??????? ????????? ???????? ??????? ??? ????? ????? ????? ???????? Northwind:
    Conn->Close(); }
    
  19. ??? ?? ?????? ?? ????????? ???? ???? ????? ????? ????? SQL ???? ???? ????? ???????? ???????:
    catch(Exception * e){ 
    
  20. ??? ??????? ????? setAbort ??? contextUtil ?? ????? ????? System.EnterpriseServices ????? ???????? ???????. ??? ??? ??????? ????? ?????? ??? ??????? ?????? ?? ??? ?? ????? ??????? ??? ???? ????????. ?? ??? ??????? caught ?????? ???? ??? ??? ????????:
    ContextUtil::SetAbort();
    throw e; }
    
  21. ???? ?????? ????? ??? ?? ???? ?????? ??? ??? ????. ????? ??? ??? ?? ?? ?????? ??????? ?? ????? ??????. ?????? ????? ???? ??????? ???????:
    1. ?? ???? ??????? ????? ?? "Visual Studio .NET" ? ???? snEnterprise.snk ??? ???? sn.exe ?????? ??? ?????. ????? ?? ????????? ??? ????? ????????? ??? ????? ???? ???? ????? .NET Framework SDK.
    2. ??? snEnterprise.snk ??? ???? ???????.
    3. ?? AssemblyInfo.vc, ??? ????? ?????? ?? ????????? ???????? ??? ?? ??? ??? ?????? ??? ???????:
      [assembly:AssemblyKeyFileAttribute("..\\snEnterprise.snk")]; 
    4. ??? ??? ?? ????? ???????.

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

?????? Remember to change your connection string parameters to reflect the correct values for your SQL Server server.
#pragma once

using namespace System;
using namespace System::Data;
using namespace System::Data::SqlTypes;
using namespace System::Data::Common;
using namespace System::EnterpriseServices;
using namespace System::Data::SqlClient;

namespace prjEnterprise
{
	
	[Transaction(TransactionOption::Required,Timeout=5)]
	public __gc class clsES:public ServicedComponent
	{
	public:	SqlConnection * Conn;

	public:	void dbAccess(int pID1, int onOrder, int pID2, int inStock)
		{
			try
			{			
				SqlConnection * Conn = new SqlConnection("user id=<username>;password=<password>;Initial Catalog=northwind;Data Source=<Your SQL Server name>;");
				Conn->Open();
				
				SqlCommand * sqlCommand = new SqlCommand("UPDATE Products SET UnitsonOrder = @onOrderString WHERE productID = @pID1String ", Conn);
				sqlCommand->Parameters->Add(new SqlParameter("@onOrderString",SqlDbType::VarChar ,
					40,ParameterDirection::Input,true,0,0,"Description",DataRowVersion::Current,onOrder.ToString()));
				sqlCommand->Parameters->Add(new SqlParameter("@pID1String", SqlDbType::VarChar,
					40,ParameterDirection::Input,true,0,0,"Description",DataRowVersion::Current,pID1.ToString()));
				sqlCommand->ExecuteNonQuery();
				
				sqlCommand->CommandText = "UPDATE Products SET UnitsinStock = @inStockString WHERE productID = @pID2String" ;
				sqlCommand->Parameters->Add(new SqlParameter("@inStockString",SqlDbType::VarChar ,
					40,ParameterDirection::Input,true,0,0,"Description",DataRowVersion::Current,inStock.ToString()));
				sqlCommand->Parameters->Add(new SqlParameter("@pID2String", SqlDbType::VarChar,
					40,ParameterDirection::Input,true,0,0,"Description",DataRowVersion::Current,pID2.ToString()));
				sqlCommand->ExecuteNonQuery();

				ContextUtil::SetComplete();
				Conn->Close();
			}
			catch(Exception * e)
			{
				ContextUtil::SetAbort();
			
				throw e;
			}			
		}
	};


}
?????? ??? ???? ????? ??? ????? ????? ?????? ????? ?????? ??????? (/ clr:oldSyntax) ?? Visual C++ 2005 ????? ????? ????????? ???????? ?????? ?????. ?????? ??????? ??? ??? ??????? ??? ?????? ??????? ??????? ?????? ?? Visual C++ 2005 ???? ??????? ???????:
  1. ???? ??? Project ?? ???? ??? <ProjectName> ?????.

    ??????<ProjectName> ?????? ?????? ??? ???????.
  2. ?? ?????? ????? ????? ?? ???? ??? ???.
  3. ???? ?????? ??? ????? ????? ?????? ??? ? ?????? ??????? (/ clr:oldSyntax) ?? ??????? ??????? ????? "??? ????? ????? ??????" ?? ????? ?????? ???? ??? ????? ?? ???? ??? ?????.
?????? ??? ???? ?? ????????? ??? ??? ????? ????? ?????? ????? ?????? ?????? ??????? ??????? ?? ?????? ???? Microsoft ?????? ??? ?????:
/ ??? (??????? ??? ????? ????? ??????)
http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

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

??????? ??? ???????? ???????? ????? ????? "????? ???? ??????" ???? ?????? ??? clsES. ?? ???? ????? ???? ?????? ?? ??? ????? ?????? ???????_???????? ? ???????_??????? ?????? ??????. ?? ?????? ??????? ???? ????? ??? ???????_???????? ???? ???? ??? ??? ????? ??? ???????_??????? ????? ??? ??? ?????? ?????? ??? ????? ?? ?????? "??????". ???? ??? ??? ?????? ????? ????? ????????.
  1. ??? Visual Studio .NET ?? Visual Studio 2005.
  2. ?? ??????? ???? ??? ??? ???? ?? ???? ??? ???????.
  3. ??? ????? Project ???? ??? ?????? Visual C++ ? ?? ???? ??? ????? ???? ?????? (.NET) ??? ?????.

    ?????? ?? Visual Studio 2005 ? ???? ??? Visual C++ ??? ????? Project ?? ???? ??? ????? ???? ?????? CLR ??? ?????.
  4. ?? ???? ???? ????? ? ???? testES. ???? ?? ????? ?????? ????? ??? ????.
  5. ???? ??? ????? ?????? ??? ??????? ??? ????.
  6. testES ??????? prjEnterprise ??? ?? ???? ?????? ????. ?? "?????? ??????" ???? ??? ?????? ?????? ??? "???????" ??? testES (???? ??? ???? ???????) ??? ?? ???? ??? ????? ????.
  7. ???? ???? ?????? ????? ??????. ??? ????? ??????? "????????" ? ???? ????? ??????? ??? prjEnterprise.
  8. ???? ???? ??? ??????? ??????. ???? ??? ????? ?????? ??? ?????? ??? ???????.
  9. ????? ???? ??? ??????? ??? ????? ????? System.EnterpriseServices. ?? "?????? ??????" ???? ??? ?????? ?????? ??? ????? ??? ?? ???? ??? ????? ????.
  10. ???? ???? ?????? ????? ??????. ??? ??? ?????? ??? ????? ??????? .NET ???? ????? ??????? ??? System.EnterpriseServices.
  11. ???? ?? ???? System.EnterpriseServices ??? ???????? ???????. ???? ??? ?????.
  12. ???? ??? ?????? ?????? ??? testES ??? ?? ???? ??? ????? ?? Project ??? ???????.
  13. ?? ???? ???????? ???????? ?????? ??????? ?? ???? _tmain() ??? testES: prjEnterprise::clsES
    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");
    	//Console::WriteLine("Error Message: {0}",e->Message);	 
    	//uncomment this line if you must get detailed error information
    }
    
  14. ???? F5 ?????? ????????? ???????? ????????.

    ???? ????????? ????? ??? dbAccess ?? ????????? ???????? ?? ?????? 7. ?????? 1 ? 2 ?????? ?? ?????? "??????". ??? ????? ????? onOrder ?? 1 ?????? 777 ? inStock ????? ?????? ??? ????? ?????? 2 ??? 888. ??? ???? ??? ???????? ????? ??????? ??????? ?? ???? ???????:
    ???????? ???? ????-
    ??? ??????? ??? ??? dbAccess. ????? ??? ??? ?? ????? ???? ??????? ?? dbAccess ??? ?????? "??????" ?????? ??????. ??? ????? ?? ?? ?????? 1 ?? ???? ??? ???????_???????? ????? ??? ????? ??? 5, ?? ???? ?? 2 ?????? ????? ?? ???????_??????? ????? ????? ??? -20. ????? ??? ???? ??? ?????? ?? ????? ?????? ?????? ??? ????? ???????_??????? ???? ??????? ???????. ????? ???? ??????? ??? dbAccess ? ??? ???????? ???????. ???? ?????? "??????" ??? ??? ??? ??????? dbAccess. ????? ????? ?????? ?????? ??? ?????? ?? dbAccess ?? ????? ????? ????? ??????? ?? ???? ???????:
    ??? ?? ????????
  15. ??? ??????? ?????? "?????? Northwind" ???????? ????? ??????? SQL Server. ??? ??? ?????? 1 ??? ???????_???????? ?????? 777. ??? ??? 2 ?????? ??? ???????_???????888. ????? ???? ??????? ??? ??? dbAccess (???? ???? ??? ?? ??? ??? ?????? ??????).

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

  • ???? ?? ???? ?? ????? ?????? ????? COM + ??? ??? ????.
  • ??? ?? ??? ??? ??? ?????? ????? + COM ?? ???? serviced. ???? ???? serviced ?? ????? ????? System.EnterpriseServices.
  • ????? ????? ? ?? ???? ?????? ??? ?? ??? ???????? ??? ?? ?? ?????. ????? ???? ??????? ????? ???? ??? ??? ????????. ?? ?????? ??????, ????? ??????? ??? ???????? 1,200 ????? ?????? ?? ????????? ??? ?????? ??????:
    [Transaction(TransactionOption::Required,Timeout=1200)]
    
  • ?? ???? ??? ????????? ??? ?????? ??? ????? ????????? ????????. ????? ???? ?? ????????? ??? ??? ??????????? uncomment ?????? ??????? ?? ?????? ??? 13:
    Console::WriteLine("Error Message: {0}",e->Message);
    uncomment this line if you must get detailed error information

?????

?????? ??? ???? ?? ?????????? ?? ?????? ????? ???? ????? Microsoft (MSDN) ?????? ??? ?????:
COM + ?????: ??? .NET Enterprise Services ???? ??????? ????? ????? ????????? ???????
?????? serviced

???????

???? ???????: 815814 - ????? ??? ??????: 04/?? ??????/1428 - ??????: 3.2
????? ???
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
????? ??????: 
kbmt kbsqlclient kbcomplusqc kbcode kbhowtomaster kbhowto KB815814 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????815814

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

 

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