0

我正在尝试使用以下代码将记录插入到我的 Access 数据库中:

<script runat="server">
    Dim dbConnection As OleDbConnection
    Dim dbCommand As OleDbCommand
    Dim dbReader As OleDbDataReader
    Dim sqlString As String

    Sub page_load()
        Try
            txtFName.Text = ""
            txtLName.Text = ""

            dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=" & Server.MapPath("MyDatabase.accdb"))
            dbConnection.Open()

            sqlString = "SELECT * FROM table1 ORDER BY ID"
            dbCommand = New OleDbCommand(sqlString, dbConnection)
            dbReader = dbCommand.ExecuteReader()

            While dbReader.Read()
                Dim lblName As Label = New Label()
                lblName.ID = dbReader("ID")
                lblName.Text = "<b>Name: </b>" & dbReader("F_Name") & " " & dbReader("L_Name") & "<br/><br/>"
                lblName.EnableViewState = False
                nameArea.Controls.Add(lblName)
            End While

            dbReader.Close()
        Finally
            dbConnection.Close()
        End Try
    End Sub

    Sub addToDatabase()
        Try
            dbConnection.Open()
            sqlString = "INSERT INTO table1 (F_Name,L_Name) VALUES (@FName, @LName)"
            dbCommand.CommandText = sqlString
            dbCommand.Parameters.AddWithValue("@FName", txtFName.Text)
            dbCommand.Parameters.AddWithValue("@LName", txtLName.Text)
            dbCommand.ExecuteNonQuery()
        Finally
            dbConnection.Close()
        End Try
    End Sub
</script>

在下面的 asp 代码中,我有两个文本框来保存名字和姓氏,还有一个在单击时将调用 addToDatabase 的按钮,以及一个用于显示从数据库中提取的每条记录的占位符:

<asp:TextBox ID="txtFName" runat="server" />
<asp:TextBox ID="txtLName" runat="server" />
<asp:Button ID="btnSubmit" Text="Submit" OnClick="addToDatabase" runat="server" />
<br />
<asp:PlaceHolder ID="nameArea" runat="server" />

我的 access 数据库包含一个名为 table1 的表,其中包含一个 ID、F_Name 和 L_Name 字段,它们的 DataTypes 分别是 AutoNumber、Text 和 Text。

我的问题是,当我单击 btnSubmit 时,它会将空白记录添加到我的数据库中,用于 F_Name 和 L_Name 而不是 TextBoxes 中的内容,并在每次浏览器刷新时继续添加空白记录。

到底是怎么回事?

4

2 回答 2

0

page_load从您的方法中删除以下行。

        txtFName.Text = ""
        txtLName.Text = ""

在方法末尾添加以下内容addToDatabase。然后,这将在页面中向您显示新名称,而无需调用数据库。

Dim lblName As Label = New Label()
lblName.ID = dbReader("ID")
lblName.Text = "<b>Name: </b>" & txtFName.Text & " " & txtLName.Text & "<br/><br/>"
lblName.EnableViewState = False
nameArea.Controls.Add(lblName)
于 2013-10-29T03:46:11.767 回答
0

我移动了我的代码并添加了一个新的 Sub 来获取名称,然后在初始 page_load 和调用 addToDatabase 之后调用该方法。我将 asp 下面的所有其他内容保持不变。

Dim dbConnection As OleDbConnection
Dim dbCommand As OleDbCommand
Dim dbReader As OleDbDataReader
Dim sqlString As String

Sub page_load()
    dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=" & Server.MapPath("MyDatabase.accdb"))
    dbCommand = New OleDbCommand("", dbConnection)

    If Not Page.IsPostBack Then
        showNames()
    End If
End Sub

Sub showNames()
    Try
        dbConnection.Open()

        sqlString = "SELECT * FROM table1 ORDER BY ID"
        dbCommand.CommandText = sqlString
        dbReader = dbCommand.ExecuteReader()

        While dbReader.Read()
            Dim lblName As Label = New Label()
            lblName.ID = dbReader("ID")
            lblName.Text = "<b>Name: </b>" & dbReader("F_Name") & " " & dbReader("L_Name") & "<br/>"
            lblEntry.EnableViewState = False
            nameArea.Controls.Add(lblName)
        End While

        dbReader.Close()
    Finally
        dbConnection.Close()
    End Try
End Sub

Sub addToDatabase(src As Object, args as EventArgs)
    Try
        dbConnection.Open()
        sqlString = "INSERT INTO table1 (F_Name,L_Name) VALUES (@FName, @LName)"
        dbCommand.CommandText = sqlString
        dbCommand.Parameters.AddWithValue("@FName", txtFName.Text)
        dbCommand.Parameters.AddWithValue("@LName", txtLName.Text)
        dbCommand.ExecuteNonQuery()
    Finally
        dbConnection.Close()
    End Try

    showNames()
End Sub
于 2013-10-29T16:02:56.943 回答