DbHelper クラス

  更新履歴



2008/06/12 初期

↓ダウンロードページ
http://winofsql.jp/VA003334/dnettool080612191802.htm



  コード



  
Public Class DbHelper

	Public Shared dic As New Dictionary(Of Object, Object)

	' ******************************************************
	' ディクショナリに文字列を値として追加する
	' ******************************************************
	Public Shared Sub AddValue(ByVal str As String)

		dic.Add(dic.Count, str)

	End Sub

	' ******************************************************
	' テキストファイルより文字列を取得
	' ******************************************************
	Public Shared Function GetSql(ByVal path As String) _
	  As String

		Dim hReader As New System.IO.StreamReader(path, System.Text.Encoding.Default)
		Dim myQuery As String = hReader.ReadToEnd()
		hReader.Close()

		myQuery = System.Text.RegularExpressions.Regex.Replace( _
		 myQuery, "([^\n-]*)--[^\n]*\n", "$1" & vbLf _
		)

		Return myQuery

	End Function

	' ******************************************************
	' テキストファイルより文字列を取得( 配列置き換え )
	' 第二引数が Nothing の場合は、置き換えをせずにそのまま
	' 第二引数は文字列の配列
	' ******************************************************
	Public Shared Function GetSql(ByVal path As String, ByVal rpl As String()) _
	 As String

		Dim hReader As New System.IO.StreamReader(path, System.Text.Encoding.Default)
		Dim myQuery As String = hReader.ReadToEnd()
		hReader.Close()

		myQuery = System.Text.RegularExpressions.Regex.Replace( _
		 myQuery, "([^\n-]*)--[^\n]*\n", "$1" & vbLf _
		)

		If rpl Is Nothing Then
			Return myQuery
		Else
			Return String.Format(myQuery, rpl)
		End If

	End Function

	' ******************************************************
	' テキストファイルより文字列を取得( 配列置き換え )
	' 第二引数が Nothing の場合は、置き換えをせずにそのまま
	' 第二引数は Dictionary
	' ******************************************************
	Public Shared Function GetSqlDic(ByVal path As String, _
	 ByVal rpl As Dictionary(Of Object, Object)) As String

		Dim hReader As New System.IO.StreamReader(path, System.Text.Encoding.Default)
		Dim myQuery As String = hReader.ReadToEnd()
		hReader.Close()

		myQuery = System.Text.RegularExpressions.Regex.Replace( _
		 myQuery, "([^\n-]*)--[^\n]*\n", "$1" & vbLf _
		)

		If rpl Is Nothing Then
			Return myQuery
		Else
			Dim rplString As String() = New String(rpl.Count - 1) {}
			Dim valueColl As _
			 Dictionary(Of Object, Object).ValueCollection = _
			  rpl.Values
			Dim cnt As Integer = 0
			For Each str As Object In valueColl
				rplString(cnt) = str.ToString()
				cnt += 1
			Next
			Return String.Format(myQuery, rplString)
		End If

	End Function

	' ******************************************************
	' テキストファイルより文字列を取得( 配列置き換え )
	' 置き換えは、クラス変数(Dictionary)より行う
	' ******************************************************
	Public Shared Function GetSqlDic(ByVal path As String) As String

		Dim hReader As New System.IO.StreamReader(path, System.Text.Encoding.Default)
		Dim myQuery As String = hReader.ReadToEnd()
		hReader.Close()

		myQuery = System.Text.RegularExpressions.Regex.Replace( _
		 myQuery, "([^\n-]*)--[^\n]*\n", "$1" & vbLf _
		)

		Dim rplString As String() = New String(dic.Count - 1) {}
		Dim valueColl As _
		 Dictionary(Of Object, Object).ValueCollection = _
		  dic.Values
		Dim cnt As Integer = 0
		For Each str As Object In valueColl
			rplString(cnt) = str.ToString()
			cnt += 1
		Next
		Return String.Format(myQuery, rplString)

	End Function

End Class
  



  SQL 文字列取得サンプル

以下のサンプルでの 0,1 はコメントです。

実際の順序通りに指定する必要はありませんが、ソースコードを見やすくする為に一致させています。
番号が必要なければ、DbHelper.AddValue( "文字列" ) で設定します

  
' ******************************************************
' select 文を外部ファイルより読み出す
' ******************************************************
DbHelper.dic.Clear()
DbHelper.dic.Add(0, Me.社員コード.DbText)
DbHelper.dic.Add(1, "社員マスタ")
Dim Query As String = DbHelper.GetSqlDic("..\..\データ\select_main.txt")
  

select_main.txt
  
select {1}.社員コード
	,{1}.氏名
	,{1}.フリガナ
	,{1}.所属
	,{1}.性別
	,{1}.作成日
	,{1}.更新日
	,{1}.給与
	,{1}.手当
	,{1}.管理者
	,管理者参照.氏名 as 管理者名
	,{1}.生年月日 
 from {1}
 left outer join {1} 管理者参照
 on {1}.管理者 = 管理者参照.社員コード 
 where {1}.社員コード = {0}
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ