我正在開發一段代碼,它允許我提取數據庫的內容並將其顯示在網頁上。我正在開發使用vb.net和sql server 2008。提取數據庫內容並將其顯示在網頁上
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Declare the query string.
Dim queryString As String = _
"Select [id], [username], [password] From [userTD]"
' Run the query and bind the resulting DataSet
' to the GridView control.
Dim ds As DataSet = GetData(queryString)
If (ds.Tables.Count > 0) Then
AuthorsGridView.DataSource = ds
AuthorsGridView.DataBind()
Else
' Message.Text = "Unable to connect to the database."
Response.Write("<br>Unable to connect to the database.")
End If
End If
End Sub
Function GetData(ByVal queryString As String) As DataSet
' Retrieve the connection string stored in the Web.config file.
Dim connectionString As String
connectionString = ("Data Source=mypc-PC;Database=mytempDB;Integrated Security=true ")
Dim ds As New DataSet()
Try
' Connect to the database and run the query.
Dim connection As New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter(queryString, connection)
' Fill the DataSet.
adapter.Fill(ds)
Catch ex As Exception
' The connection failed. Display an error message.
' Message.Text = "Unable to connect to the database."
Response.Write("<br>Unable to connect to the database.")
End Try
Return ds
End Function
End Class
該代碼工作正常。 在我的情況下,我必須在default.aspx中聲明「AuthorsGridView」,但我的目標是在不修改default.aspx的情況下顯示數據。
**不要將密碼存放在純文本** 。 _Especially_不會顯示密碼。 – SLaks
你的意思是你想顯示查詢結果,但你不想通過添加GridView來改變ASPX標記? –
@YuriyGalanter是的,這是我想要的,但我不知道該怎麼做 – user2649088