Since the age of VB6, the method of writing and registering COM + components was not very complex, but it entered. net World, the process of writing has become more complicated.
Follow these steps:
Assume that you want to write a data access component of the data table (tdmomenu) in the database ddmo. You need to generate a package named [kdmo1000] in the component service, which contains a component pdmomenu. the dll contains two COM + components:
Project. Class
- Pdmomenu. cdmomenu1 (without transaction): used to read data
- Pdmomenu. cdmomenu2 (with transaction): used to maintain data
Writing steps:
-
- Project Creation:
- Create the directory [pdmomenu] of your component project under the appropriate directory]
- Create Project and select class library. Set the directory to the directory of your project and the name to pdmomenu]
- In Solution Explorer, click class1.vb. In properties, the file name is cdmomenu1.vb (without transaction) and cdmomenu2.vb (with transaction)
- Right-click reference> Add reference and add system. enterpriseservices]
-
- Project content:
- Right-click a project → Properties)
- Click sign and select sign component]
- Select the strong name key file (choose a strong name key file ):
- Select New> enter pdmomenu]
- Click [ApplicationProgram(Application )]
Check whether assembly name and root namespace are pdmomenu]
- Click "component information" and tick "make component com-visiable (make assembly com-visiable )".
-
- Assemblyinfo. VB:
- Add imports at the top:
Imports system. enterpriseservices
- Add the following command at the bottom:
<Assembly: applicationname ("kdmo1000")> '* sets the project name of the component service.
<Assembly: applicationaccesscontrol (false)>
<Assembly: applicationactivation (activationoption. Server)>
-
- Class content:
-
- No transaction class (cdmomenu1.vb ):
Imports System. enterpriseservices Imports System. runtime. interopservices Imports System. Data. sqlclient Imports System. Data Imports System. Io 'Guid, please generate a new component when the component is created for the first time, and do not need to be changed later "838a5fe4-2dcc-43df-8658-0697e8fb7c0f" ), _ Eventtrackingenabled ( True )> _ Public Class Cdmomenu1 Inherits Servicedcomponent Const Dbname As String = "Ddmo" Public Function Test1 ( Byval In1 () As String , Byval Out1 () As String , Byref P As String , Byref R As String , Byval User As String )As String '*************************************** ********************************** *** Written by: topcat: 2006/8/9 '** Purpose: 1. '** Practice: '** 1. '** Note: '** 1. '** 2. '** Maintenance record: '** Maintainer: Name maintenance Date: Date '** Maintenance project: '** 1. '** 2. '** Practice: 1. '** 2. '** Note: '** 1. '*************************************** ********************************** Dim Connstr As String = Getconnstr (dbname) using Conn As New Sqlconnection (connstr) Try Test1 = "Success" Catch Ex As Exception test1 = "False" Throw New Exception (ex. Message) End Try End Using End Function Public Function Getconnstr ( Byval Dbname As String ) As String '*************************************** ********************************** *** Written by: topcat: 2006/2/8 '** Purpose: 1. Read the connection string '** Practice: '** 1. input the database name dbname parameter. '** 2. Read text files through stream '** 3. Return the obtained information '** 4. Close related objects '** Note: '** 1. '** 2. '** Maintenance record: '** Maintainer: Name (employee code) Maintenance Date: Date '** Maintenance project: '** 1. '** 2. '** Practice: 1. '** 2. '** Note: '** 1. '*************************************** ********************************** Dim Strmrd As New Streamreader ( "C: \ datalink \" + Dbname + ". Ini" ) Dim Line As String = "" Dim Connstr As String = "" Try Do Line = strmrd. Readline () If Line <> "" Then Connstr + = line End If Loop Until Line Is Nothing Getconnstr = connstr Catch Ex As Exception Throw New Exception (ex. Message. tostring) Finally Strmrd. Close () strmrd. Dispose () strmrd = Nothing End Try End Function End Class
-
- Transaction class (cdmomenu2.vb ):
Imports System. enterpriseservices Imports System. runtime. interopservices Imports System. Data. sqlclientImports System. Data Imports System. Io "28549732-9a0f-4731-9688-10cee644500e" ) _, Transaction (transactionoption. Required) _, synchronization (synchronizationoption. Required) _, justintimeactivation ( True ) _, Eventtrackingenabled ( True )> _ Public Class Cdmomenu2 Inherits Servicedcomponent Const Dbname As String = "Ddmo" _Public Function Test2 ( Byval In1 () As String , Byval Out1 () As String , Byref P As String , Byref R As String , Byval User As String )As String '*************************************** ********************************** *** Written by: topcat: 2006/8/9 '** Purpose: 1. '** Practice: '** 1. '** Note: '** 1. '** 2. '** Maintenance record: '** Maintainer: Name (employee code) Maintenance Date: Date '** Maintenance project: '** 1. '** 2. '** Practice: 1. '** 2. '** Note: '** 1. '*************************************** ********************************** Dim Connstr As String = Getconnstr (dbname) using Conn As New Sqlconnection (connstr) Try Test2 = "Success" Catch Ex As Exception Test2 = "False" Throw New Exception (ex. Message) End Try End Using End Function Public Function Getconnstr ( Byval Dbname As String ) As String '*************************************** ********************************** *** Written by: topcat: 2006/2/8 '** Purpose: 1. Read the connection string '** Practice: '** 1. input the database name dbname parameter. '** 2. Read text files through stream '** 3. Return the obtained information '** 4. Close related objects '** Note: '** 1. '** 2. '** Maintenance record: '** Maintainer: Name (employee code) Maintenance Date: Date '** Maintenance project: '** 1. '** 2. '** Practice: 1. '** 2. '** Note: '** 1. '*************************************** ********************************** Dim Strmrd As New Streamreader ( "C: \ datalink \" + Dbname + ". Ini" ) Dim Line As String = "" Dim Connstr As String = "" Try Do Line = strmrd. Readline () If Line <> "" Then Connstr + = line End If Loop Until Line Is Nothing Getconnstr = connstr Catch Ex As Exception Throw New Exception (ex. Message. tostring) Finally Strmrd. Close () strmrd. Dispose () strmrd = Nothing End Try End Function End Class
After writing the relevant program, build the project, and then generate the DLL according to the position set by the Project property.