2013年4月16日 星期二

[VB.NET]使用System.Configuration命名空間處理自訂的應用程式組態檔Config

在WinForm組態檔來說,較常使用的app.config,當專案建置後app.config會被編譯成(xxxx.exe.config)。

但是這樣只有此專案能使用這個app.config而已,如果當有多個專案要共用的話,其他專案也都加入此app.config,才能讀取得到。

當然有很多種方法可以解決這個問題,這裡只提供其中一種方法當作範例。

1.先在專案中加入一個自訂的應用程式組態檔 Sample.config
 


  
    
  
2.加入參考System.Configuration命名空間

3.讀取Sample.config
Imports System.Configuration

Module Module1
    Sub Main()
        Dim configFileMap As ExeConfigurationFileMap
        Dim config As Configuration
        Dim configKeyValue As KeyValueConfigurationElement
        Try
            configFileMap = New ExeConfigurationFileMap
            configFileMap.ExeConfigFilename = "Sample.config"
            config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
            configKeyValue = config.AppSettings.Settings("Test")
            If configKeyValue Is Nothing Then
                Console.WriteLine("指定Key錯誤。")
            End If
            Console.WriteLine("Test=" + configKeyValue.Value)
            Console.ReadKey()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.ReadKey()
        Finally
            configFileMap = Nothing
            config = Nothing
            configKeyValue = Nothing
        End Try
    End Sub
End Module

2012年8月1日 星期三

[VB.NET]程式中使用SQL字串IN參數化設定

 

一,寫程式時在使用SQL字串時,WHERE條件的參數,我們需要參數化,以防止SQL Injection發生,

ADO.Net 有提供相關參數化設定,但是當我們遇到使用IN子句時,要如何使用參數化?

 
Dim parameString As String = "123,456,789"
Dim parameArray() As String
Dim temp As String
comm.CommandText = "SELECT * FROM TEST WHERE ID IN (@ID)"


parameArray = parameString.Split(",")


For i = 0 To parameArray.Length - 1
temp &= String.Format("@ID{0},", i)
Next
comm.CommandText = comm.CommandText.Replace("@ID", temp.Substring(0, temp.Length - 1))
For j = 0 To parameArray.Length - 1
comm.Parameters.Add(String.Format("@ID{0}", j), SqlDbType.VarChar).Value = parameArray(j)
Next
comm.ExecuteReader()

2012年5月30日 星期三

[AOP]Crosscutting Concerns實作

Crosscutting Concerns 使用System.Runtime.Remoting.Proxies.RealProxy類別來實作

一,說明Hello這類別就是用來繼承MarshalByRefObject類別,在AOP中表示Pointcut。
Hello.vb
 
Public Class Hello
    Inherits MarshalByRefObject
End Class

二,說明IHello這類別就是定義一個Interface,在AOP中表示Weave。
IHello.vb
 
Public Interface IHello
    Sub hello(ByVal name As String)
End Interface

三,說明LogHandler這類別就是用來做動態Proxy Pattern,繼承RealProxy類別,在AOP中表示Advices。
LogHandler.vb
 
Imports System.Runtime.Remoting.Proxies
Imports System.Runtime.Remoting.Messaging
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Services

Public Class LogHandler
    Inherits RealProxy

    Private myMarshalByRefObject As MarshalByRefObject

    Public Sub New(ByVal myType As Type)
        MyBase.New(myType)
        myMarshalByRefObject = CType(Activator.CreateInstance(myType), MarshalByRefObject)
    End Sub

    Public Overrides Function Invoke(ByVal myIMessage As IMessage) As IMessage
        Dim calls As IMethodCallMessage = myIMessage
        Dim back As IMethodReturnMessage = Nothing

        Console.WriteLine("呼叫方法的名稱:" + calls.MethodName)

        back = RemotingServices.ExecuteMessage(myMarshalByRefObject, calls)

        Console.WriteLine("顯示的結果:" + back.ReturnValue.ToString())

        Return back
    End Function
End Class

四,說明HelloSpeaker這類別就是用來這類別就是用來繼承Hello類別並實作IHello介面,在AOP中表示Joinpoint。
HelloSpeaker.vb
 
Public Class HelloSpeaker
    Inherits Hello
    Implements IHello

    Public Sub hello(name As String) Implements IHello.hello
        Console.WriteLine("Hello:" + name)
    End Sub

End Class

五,執行
ProxyDemo.vb
 
Module ProxyDemo
    Sub Main()
        Dim proxy As LogHandler = New LogHandler(New HelloSpeaker().GetType())
        Dim Hello As IHello = proxy.GetTransparentProxy()
        Hello.hello("123")
        Console.ReadKey()
    End Sub
End Module

SyntaxHighlighter Manual

 
 您的程式碼請貼此處。 
SyntaxHighlighter/manual/brushes/
Brush nameBrush aliasesFile name
ActionScript3as3, actionscript3shBrushAS3.js
Bash/shellbash, shellshBrushBash.js
ColdFusioncf, coldfusionshBrushColdFusion.js
C#c-sharp, csharpshBrushCSharp.js
C++cpp, cshBrushCpp.js
CSScssshBrushCss.js
Delphidelphi, pas, pascalshBrushDelphi.js
Diffdiff, patchshBrushDiff.js
Erlangerl, erlangshBrushErlang.js
GroovygroovyshBrushGroovy.js
JavaScriptjs, jscript, javascriptshBrushJScript.js
JavajavashBrushJava.js
JavaFXjfx, javafxshBrushJavaFX.js
Perlperl, plshBrushPerl.js
PHPphpshBrushPhp.js
Plain Textplain, textshBrushPlain.js
PowerShellps, powershellshBrushPowerShell.js
Pythonpy, pythonshBrushPython.js
Rubyrails, ror, rubyshBrushRuby.js
ScalascalashBrushScala.js
SQLsqlshBrushSql.js
Visual Basicvb, vbnetshBrushVb.js
XMLxml, xhtml, xslt, html, xhtmlshBrushXml.js