データ読み出しのSQLとコードは、次のようになります。IStockMarketインタフェースは、株式市場を表すもので、独自に作成したインタフェースです。
Const GET_VALUE_SQL_BASE As String = "select datetime, value from t_value " & _
" where term = :term and company_id = :company_id and market_id = " & GET_MARKET_ID_SQL & _
" and category_id = " & GET_CATEGORY_ID_SQL & " and item_id = " & GET_ITEM_ID_SQL
Const GET_VALUE_SQL As String = GET_VALUE_SQL_BASE & " ORDER BY datetime DESC;"
Protected _readCom As NpgsqlCommand
Sub New()
_readCom = New NpgsqlCommand(GET_VALUE_SQL, _conn)
_readCom.Parameters.Add(New NpgsqlParameter("term", DbType.String))
_readCom.Parameters.Add(New NpgsqlParameter("company_id", DbType.Int32))
_readCom.Parameters.Add(New NpgsqlParameter("market_name", DbType.String))
_readCom.Parameters.Add(New NpgsqlParameter("category_name", DbType.String))
_readCom.Parameters.Add(New NpgsqlParameter("item_name", DbType.String))
_readCom.Prepare()
End Sub
Function FindList(ByVal term As String, ByVal company_id As Integer, ByVal market As Entity.Market.IStockMarket, ByVal category_name As String, _
ByVal item_name As String) As List(Of SeqValue)
_readCom.Parameters("term").Value = term
_readCom.Parameters("company_id").Value = company_id
_readCom.Parameters("market_name").Value = market.DBItemName
_readCom.Parameters("category_name").Value = category_name
_readCom.Parameters("item_name").Value = item_name
Dim dr As NpgsqlDataReader = _readCom.ExecuteReader()
Dim list As List(Of SeqValue) = GetData(dr, term, company_id, market, category_name, item_name)
dr.Close()
Return list
End Function
Private Shared Function GetData(ByVal dr As NpgsqlDataReader, ByVal term As String, ByVal company_id As Integer, ByVal market As IStockMarket, ByVal category_name As String, ByVal item_name As String) As List(Of SeqValue)
Dim list As New List(Of SeqValue)
Do While dr.Read
Dim sv As New SeqValue
sv.term = term
sv.company_id = company_id
sv.market = market
sv.category_name = category_name
sv.item_name = item_name
sv.datetime = DirectCast(dr.Item("datetime"), Date)
sv.value = DirectCast(dr.Item("value"), Double)
list.Add(sv)
Loop
Return list
End Function
Dim dao As New SeqValueDAO
Dim list As List(Of SeqValue) = dao.FindList(term ,company_id, market, category_name, item_name)
検索ワード:SQL、select、where
スポンサーリンク