當我從SQL Server數據庫中讀取數據我有一個問題的數據。最主要的是我想從數據庫讀取數據並在Label控件中顯示數據。但擔心的是它無法讀取數據。我會告訴你代碼片段和任何意見/建議很樂意考慮。無法讀取從SQL Server數據庫
Option Explicit On
Imports System.Data
Imports System.Data.OleDb
Partial Class ViewDetail
Inherits System.Web.UI.Page
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim InstructorID As Integer
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
InstructorID = Request.QueryString("Instructor_ID")
Integer.TryParse(lblID.Text, InstructorID)
con = New OleDbConnection("Provider=SQLNCLI11;Data Source=ARIES-PC\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=SchoolDB")
con.Open()
cmd = New OleDbCommand("SelectData", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@id", InstructorID)
dr = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
lblID.Text = dr("Instructor_ID").ToString
lblFirstname.Text = dr("FirstName").ToString
lblLastname.Text = dr("LastName").ToString
lblAddress.Text = dr("Address").ToString
lblContact.Text = dr("Contact_Number").ToString
End While
End If
dr.Close()
cmd.Dispose()
con.Close()
End Sub
End Class
你不解釋什麼是錯誤。你有例外嗎?如果是,什麼是錯誤信息? – Steve
你還沒有提到你確切的問題。它不打開連接?或在ExecuteReader上給出錯誤?提供更多細節,以便有人可以更好地幫助你。 – par
繼'DR = cmd.ExecuteReader'應該是Dr = cmd.ExecuteReader()' – Mairaj