0

我正在使用 Web Developer 2010、带有 VB.NET 编程语言和 MS-Access 数据库 (.mdb) 的 ASP.NET。

我想要做的是从一个名为 User 的表(即所有列)中获取所有数据,其中 Username = _ ___,然后将字段中的值放入文本框中,以便对其进行编辑。这是为了创建一个“我的帐户”页面,以便查看该网站的用户可以更改他的地址、密码等。

我坚持从每个特定用户名的字段中获取数据。

到目前为止我所做的是这样,但不起作用:

Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data

Partial Class Account
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load


        Dim tUsername As String = Session("Username")
        Dim tPassword As String = Session("Password")

        Dim conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\PetLandia\App_Data\db.mdb")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Email], [CustomerName], [CustomerSurname], [Address], [Country], [TelNo], [Username], [Password] FROM [User] WHERE Username=?", conn)

        DataSet(dst = New DataSet)

        cmd.Parameters.AddWithValue("@Username", tUsername)

        conn.Open()

        Dim reader As OleDbDataReader = cmd.ExecuteReader

        If (reader.Read()) Then

            lblName.Text = reader.GetValue(4).ToString
            lblSurname.Text = reader.GetValue(5).ToString
            lblUsername.Text = reader.GetValue(9).ToString
            txtAddress.Text = reader.GetValue(6).ToString
            txtCountry.Text = reader.GetValue(7).ToString
            txtEmail.Text = reader.GetValue(2).ToString

            conn.Close()

        End If





    End Sub


End Class

错误信息:

Parameter ?_1 has no default value. 

Dim reader As OleDbDataReader = cmd.ExecuteReader

有什么帮助吗?

4

2 回答 2

3

你需要给参数@Username不喜欢?

 Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Email], [CustomerName], [CustomerSurname], [Address], [Country], [TelNo], [Username], [Password] FROM [User] WHERE Username=@Username", conn)

处理连接时使用Using语句如下

Dim queryString As String = "SELECT OrderID, CustomerID FROM Orders"
Using connection As New OleDbConnection(connectionString)
    Dim command As New OleDbCommand(queryString, connection)

    connection.Open()

    Dim reader As OleDbDataReader = command.ExecuteReader()
    While reader.Read()
        Console.WriteLine(reader.GetInt32(0).ToString() + ", " _
           + reader.GetString(1))
    End While

    ' always call Close when done reading.
    reader.Close()
End Using

并且还要确保reader.GetValue(Index)索引始终低于选择列数。

于 2012-05-08T18:41:07.597 回答
0

您的索引号与您的查询语句不一致。

尝试

        lblName.Text = reader.GetValue(2).ToString 
        lblSurname.Text = reader.GetValue(3).ToString 
        lblUsername.Text = reader.GetValue(7).ToString 
        txtAddress.Text = reader.GetValue(4).ToString 
        txtCountry.Text = reader.GetValue(5).ToString 
        txtEmail.Text = reader.GetValue(0).ToString 
于 2012-05-08T18:29:07.980 回答