使用ASP.net asp.aspx.vb程式碼 連接資料庫

摘要:ASP.net 使用vb程式碼 連接資料庫

程式碼是參考MSbl2000老師的書的,放在這裡日後查詢方便
 


'命名空間(NmaeSpace)

Imports System.Data  '不管使用哪種語法來撰寫ASP.NET,必須先參考System.Data這個命名空間。
Imports System.Data.SqlClient '允許你連接到MS SQL Server7.0以後版本(2000~2008)、執行命令並執行結果。
Imports System.Web.Configuration '宣告web config'

Partial Class shoptyle_style1_style1master
    Inherits System.Web.UI.MasterPage

    Public Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Sqldatasource1.DataSourceMode = SqlDataSourceMode.DataReader
   '== 如果 DataSourceMode 屬性設為 DataReader 值,則會傳回 IDataReader 物件。 
  '== 當完成讀取資料時,請關閉 IDataReader 物件。 

        Dim args As New DataSourceSelectArguments
   '== DataSourceSelectArguments 提供一項機制,讓資料繫結控制項於擷取資料時, 
    '== 用來向資料來源控制項要求資料相關的作業。 

        Dim I_DR As IDataReader = CType(Sqldatasource1.Select(args), IDataReader)
        Try
            I_DR.Read()  '*****  重點!! ***** 

        Catch ex As Exception
            Response.Write("找不到商家")
            Response.End()

        End Try

        Dim Style As String = I_DR.Item("style").ToString()
        Master_Store_NameLabel.Text = I_DR.Item("Store_Name").ToString()     '首頁標題
        Master_Store_NameLabe2.Text = I_DR.Item("Store_Name").ToString()
        Master_Store_Cover.ImageUrl = I_DR.Item("Photo_Cover").ToString()
        ' DropDownList1.SelectedItem.
        '== 當完成讀取資料時,請關閉 IDataReader 物件。 
        I_DR.Close()
        I_DR.Dispose()

        Dim Username As String = Request.Url.Query              '抓取 Request URL 


        HomeHyperLink.NavigateUrl = "index.aspx" & Username    '幫連結加上Username
        NewsHyperLink.NavigateUrl = "News.aspx" & Username
        ProHyperLink.NavigateUrl = "Promotion.aspx" & Username
        AlbumHyperLink.NavigateUrl = "Album.aspx" & Username
        MenuHyperLink.NavigateUrl = "Menu.aspx" & Username
        AboutHyperLink.NavigateUrl = "About.aspx" & Username
        HomeHyperLink2.NavigateUrl = "index.aspx" & Username
        NewsHyperLink2.NavigateUrl = "News.aspx" & Username
        ProHyperLink2.NavigateUrl = "Promotion.aspx" & Username
        AlbumHyperLink2.NavigateUrl = "Album.aspx" & Username
        MenuHyperLink2.NavigateUrl = "Menu.aspx" & Username
        AboutHyperLink2.NavigateUrl = "About.aspx" & Username

        '   Dim li As HtmlGenericControl = menu.FindControl("li1")
        '  If li IsNot Nothing Then li.Attributes.Add("class", "selected")

        MyStyleSheet.Attributes.Add("href", Style & "/style.css") '動態更改CSS樣式
    End Sub

 

  
    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        ' Sqldatasource1.UpdateParameters.Add("style", DropDownList1.SelectedValue)
        Sqldatasource1.Update()
        Dim Username As String = Request.Url.Query

        Response.Redirect("index.aspx" & Username)

    End Sub
End Class