N-Tiers development mode (how to use VB.NET to write a COM + component)

Abstract: N-Tiers development mode (how to use VB.NET to write a COM + component)


Small meow written COM + components began in VB6 years of writing and registered way still is not very complicated, but with the entry of the .NET world, the process of writing has become a lot more complicated.

Here are the steps written

It is assumed to be written in a database DDMO data table (TDMOMenu) data access component, to produce a component Package Name Services for KDMO1000 [], which has two inner components PDMOMenu.dll COM + components are
Project.Class

  1. PDMOMenu.CDMOMenu1 (no Transaction): for reading data
  2. PDMOMenu.CDMOMenu2 (there Transaction): to maintain data

Write steps:


Imports System.Runtime.InteropServices
Imports System.Data.SqlClient
Imports System.Data
Imports System.IO



  
  
   
    _
Public Class CDMODEMO1

    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 RUser As String) As String
        '*
        '     撰写者:topcat(topcat)     撰写日期:2008/7/23
        '     用途:  1.
        '     做法:
        '             1.
        '     注意事项:
        '             1.
        '             2.
        '     维护记录:
        '         维护者:姓名(员工代号)     维护日期:日期
        '         维护项目:
        '                 1.
        '                 2.
        '         做法:  1.
        '                 2.
        '         注意事项:
        '                 1.
        '*

        Dim ConnStr As String = GetConnStr(DBName)

        Try

            Test1 = "Success"

        Catch ex As Exception
            Test1 = "False"
            Throw New Exception(ex.Message)

        Finally

        End Try
    End Function

    Public Function GetConnStr(ByVal DBName As String) As String
	    '*
	    '     撰写者:topcat(topcat)     撰写日期:2006/2/8
	    '     用途:  1.读取Connection String的方式
	    '     做法:
	    '             1.传入参数DataBase Name DBName
	    '             2.透过Stream将文字档读出
	    '             3.将取得的数据返回
	    '             4.关闭相关对象
	    '     注意事项:
	    '             1.
	    '             2.
	    '     维护记录:
	    '         维护者:姓名(员工代号)     维护日期:日期
	    '         维护项目:
	    '                 1.
	    '                 2.
	    '         做法:  1.
	    '                 2.
	    '         注意事项:
	    '                 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
  
  


Imports System.Runtime.InteropServices
Imports System.Data.SqlClient
Imports System.Data
Imports System.IO



  
  
   
    _
Public Class CDMODEMO2
    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 RUser As String) As String
        '*
        '     撰写者:topcat(topcat)     撰写日期:2008/7/23
        '     用途:  1.
        '     做法:
        '             1.
        '     注意事项:
        '             1.
        '             2.
        '     维护记录:
        '         维护者:姓名(员工代号)     维护日期:日期
        '         维护项目:
        '                 1.
        '                 2.
        '         做法:  1.
        '                 2.
        '         注意事项:
        '                 1.
        '*

        Dim ConnStr As String = GetConnStr(DBName)

        Try


            Test2 = "Success"
        Catch ex As Exception
            Test2 = "False"
            Throw New Exception(ex.Message)

        Finally

        End Try
    End Function

    Public Function GetConnStr(ByVal DBName As String) As String
	    '*
	    '     撰写者:topcat(topcat)     撰写日期:2006/2/8
	    '     用途:  1.读取Connection String的方式
	    '     做法:
	    '             1.传入参数DataBase Name DBName
	    '             2.透过Stream将文字档读出
	    '             3.将取得的数据返回
	    '             4.关闭相关对象
	    '     注意事项:
	    '             1.
	    '             2.
	    '     维护记录:
	    '         维护者:姓名(员工代号)     维护日期:日期
	    '         维护项目:
	    '                 1.
	    '                 2.
	    '         做法:  1.
	    '                 2.
	    '         注意事项:
	    '                 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

   
   
  
  

  1. Construction project:
    1. Where appropriate directory, create a directory of your project components [PDMOMenu]  

    2. Establish project (Create Project), select [library (Class Library)], set the directory to the directory of your project, name] is set to [PDMOMenu  

    3. program Explorer (Solution Explorer) in tap Class1.vb, change the file name (file name) properties (properties) in the range [CDMOMenu1.vb] (no TranSaction), [CDMOMenu2.vb ] (with the Transaction)  

    4. reference (reference) was added right → reference, increase System.EnterpriseServices []  

     

    → step two
  2. Project Description:
    1.点选项目(Project)右键→属性(Properties)
    2.点选【签署(Signing)】,勾选【签署组件】
    	在选择强势名称金钥档(Choose a strong name key file)
    	选择新增(New)→输入【PDMOMenu】
    3.点选【应用程序(Application)】
    	检查【组件名称(Assembly Name)】、【根命名空间(Root NameSpace)】是否是【PDMOMenu】
    4.按【组件资讯】,将【让组件成为COM-Visiable(Make assembly COM-Visiable)】打勾
    →步骤三

  3. AssemblyInfo.vb:
    1.请在最上方Imports加入:
    Imports System.EnterpriseServices
    
    2.请在最下方加入以操作:
    
        
        
         
          '*设定组件服务的项目名称
    
         
         
    
          
          
         
         
        
        

  4. Class content:
    None Transaction Class (CDMOMenu1.vb):

  5. 有Transaction Class(CDMOMenu2.vb):

After writing about the program, followed by Build project, then it will have dll in accordance with the position of Project Property settings


Supplement, thanks to friends little magic to inform the content is wrong, get to make up the database connection Function

Description:

Data access will be through ConnString settings to specify which database is accessed on the Which Server, use the authentication is in what way. If ConnString written in the assembly, then when the Server address change, even change the name of the database, then we have to program components called out one by one to modify, and then compile registration, this process is too complicated

So how to set ConnString detached components, small meow approach is to create a .ini text file, on the C: DataLink, which stood on the content ConnString, and then read through the assembly System.IO the contents of text files.

Thus, in the future there is a database transaction (position, name), you may not need to rewrite components, directly modify configuration files.


The following is the signature:

  • Welcome to the site posted the article, but the gist of your posts on the plus - posted, and attach Benpian hyperlinks station name in the article [topcat extreme cohesion between the Shan dance], thanks you for your cooperation.
  • Most small meow meow small article will be familiar language to write VB.NET, C # if you need the Code, perhaps you can try online conversion tool, providing several reference here
    • http://converter.telerik.com/
    • http://www.carlosag.net/tools/codetranslator/
    • http://www.developerfusion.com/tools/convert/vb-to-csharp/

Microsoft MVP
Visual Studio and Development Technologies
(2005~Now)

topcat
Blog:http://www.dotblogs.com.tw/topcat


Original: Big Box  N-Tiers development mode (how to use VB.NET to write a COM + component)


Guess you like

Origin www.cnblogs.com/chinatrump/p/11505398.html