Skip to content

Instantly share code, notes, and snippets.

@ryosms
Last active December 14, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryosms/27597cf17665b61828b2 to your computer and use it in GitHub Desktop.
Save ryosms/27597cf17665b61828b2 to your computer and use it in GitHub Desktop.
死ねばいいのに
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' ...
' いろいろ処理
Dim dataTable As DataTable = getDbData("hoge")
' ...
' いろいろ処理
End Sub
Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button2.Click
' ...
' いろいろ処理
Dim htPrm As New Hashtable
htPrm.Add("@are", "are")
htPrm.Add("@sore", "sore")
Dim dataTable As DataTable = getDbData("fuga", htPrm)
' ...
' いろいろ処理
End Sub
Private Sub Button3_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button3.Click
' ...
' いろいろ処理
Dim htPrm As New Hashtable
htPrm.Add("@are", "are")
htPrm.Add("@kore", "kore")
Dim htPrc As New Hashtable
htPrc.Add("#where#", "are = @are AND kore = @kore")
Dim dataTable As DataTable = getDbData("hage", htPrm, htPrc)
' ...
' いろいろ処理
End Sub
Public Function getDbData(ByVal mode As String, Optional ByVal prm As Hashtable = Nothing, Optional ByVal prc As Hashtable = Nothing) As Object
Dim sqlCommand as New SqlCommand
Dim databaseHelper as DatabaseHelperClass ' DBアクセス用のHelperクラス
Dim dataTable As DataTable
Dim sql As String = setSql(mode)
If prc IsNot Nothing Then
For Each key As String In prc.Keys
sql = sql.Replace(key, prc(key))
Next
End If
If prm IsNot Nothing Then
For Each key As String In prm.Keys
If key.IndexOf("@") = 0 Then
sqlCommand.Parameters.AddWithValue(key, prm(key))
End If
Next
End If
Try
databaseHelper.open(true)
sqlCommand.Connection = databaseHelper.gConnection ' ←DBのHelperクラスのPublicなフィールドに直でアクセス
' ...
' DBからデータ取得処理をゴニョゴニョ
Return dataTable
Catch ex As Exception
Throw
Finally
databaseHelper.open(false)
End Try
End Function
Private Function setSql(ByVal mode As String) As String
Dim sql as New StringBuilder()
With sql
Select Case mode
Case "hoge"
.Append("SELECT")
.Append("...")
.Append("FROM ...")
Case "fuga"
.Append("SELECT")
.Append("...")
.Append("FROM ...")
.Append("WHERE")
.Append(" are = @are")
.Append(" AND sore = @sore")
Case "hage"
.Append("SELECT")
.Append("...")
.Append("FROM ...")
.Append("WHERE")
.Append("#where#")
End Select
End With
Return sql.toString()
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment