データアクセスをFunctionに一本化



web.donfigの内容
<connectionStrings>
<add name="ContentsLink" connectionString="Data Source=SQLサーバー名;Initial Catalog=カタログ名;
Persist Security Info=True;User ID=ID;Password=パスワード;Max Pool Size=適当に;"
providerName="System.Data.SqlClient" />
</connectionStrings>

Dim settings As ConnectionStringSettings =→
System.Configuration.ConfigurationManager.ConnectionStrings("ContentsLink")
Dim ConnectionStr As String = settings.ConnectionString

Protected Sub MainRooteng()
  LifeLinkRoad()
  'ここはいくらでも増やせる
End Sub

Protected Sub LifeLinkRoad()
  SelectTmp = "SELECT sub_cate,sub_name FROM [Zatugaku_category] " _ 
  & "WHERE cid=1;"
  'セレクトコマンドを作る
  LifeLinkReperter.DataSource = MainSelect(SelectTmp)
'リピーター、グリッドビュー、データビューなどへバインディング
  LifeLinkReperter.DataBind()
End Sub

Function MainSelect(ByVal SubCategory As String) As DataSet
  Dim con As New SqlClient.SqlConnection(ConnectionStr)
  Dim cmd As New SqlClient.SqlDataAdapter(SubCategory, con)
  Dim WebDataSet As New DataSet()
   con.Open()
   cmd.Fill(WebDataSet)
   cmd.Dispose()
   con.Close()
   con.Dispose()
   WebDataSet.Dispose()
  Return WebDataSet
End Function

こんな感じでデータアクセス部分は一つにまとめられます。

記事id:230 / 4016PV

関連記事