我正在尝试使用以下代码将记录插入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代码中,我有两个TextBox来保存名字和姓氏,以及一个在点击时调用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" />
我的访问数据库包含一个名为table1的表,其中包含ID,F_Name和L_Name字段,其DataType分别为AutoNumber,Text和Text。
我的问题是当我点击btnSubmit时,它会在我的数据库中为F_Name和L_Name添加空白记录而不是TextBox中的内容,并在每次浏览器刷新时继续添加空白记录。
发生了什么事?
答案 0 :(得分: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)
答案 1 :(得分: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