codebehind1.vb

来自「asp入门到精通的源代码」· VB 代码 · 共 45 行

VB
45
字号
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.OleDb

Public Class CodeBehind1 : Inherits Page
   'declare public variables for .aspx file to inherit
   public lblMessage as Label
   public DataGrid1 as DataGrid
   
   'declare connection
   private strConnString as string = "Provider=" & _
      "Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=C:\ASPNET\data\banking.mdb"
   private objConn as new OleDbConnection(strConnString)
   
   sub Page_Load(obj as Object, e as EventArgs) 
      if Not Page.IsPostBack then
         FillDataGrid()
      end if
   end sub

   private sub FillDataGrid(Optional EditIndex as integer=-1)
      'open connection
      dim objCmd as OleDbCommand = new OleDbCommand _
         ("select * from tblUsers", objConn)
      dim objReader as OleDbDataReader
       
      try
         objCmd.Connection.Open()
         objReader = objCmd.ExecuteReader
      catch ex as OleDbException
         lblMessage.Text = "Error retrieving from the database."
      end try
       
      DataGrid1.DataSource = objReader
      DataGrid1.DataBind()
       
      objReader.Close
      objCmd.Connection.Close()
   end sub
    
End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?