N-tiers development method (how to use VB. NET to write COM + components)

Source: Internet
Author: User

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

 

    1. Pdmomenu. cdmomenu1 (without transaction): used to read data
    2. Pdmomenu. cdmomenu2 (with transaction): used to maintain data

Writing steps:

  1. Project Creation:
      1. Create the directory [pdmomenu] of your component project under the appropriate directory]
      2. Create Project and select class library. Set the directory to the directory of your project and the name to pdmomenu]
      3. In Solution Explorer, click class1.vb. In properties, the file name is cdmomenu1.vb (without transaction) and cdmomenu2.vb (with transaction)
      4. Right-click reference> Add reference and add system. enterpriseservices]
  2. Project content:
      1. Right-click a project → Properties)
      2. Click sign and select sign component]
        1. Select the strong name key file (choose a strong name key file ):
        2. Select New> enter pdmomenu]
      3. Click [ApplicationProgram(Application )]
        Check whether assembly name and root namespace are pdmomenu]
      4. Click "component information" and tick "make component com-visiable (make assembly com-visiable )".
  3. Assemblyinfo. VB:
      1. Add imports at the top:
        Imports system. enterpriseservices
      2. 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)>
  4. Class content:
    1. No transaction class (cdmomenu1.vb ):
    2.  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 
    3. Transaction class (cdmomenu2.vb ):
    4.  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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.