我试图向数据库添加评论。我开始使用Access数据库,以确保在进行下一步之前我的代码是正确的。 我使用的两个文件是Comments.aspx和代码文件Comments.aspx.vb。
这是我到目前为止所拥有的。 Comments.aspx的内容是:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Comments.aspx.vb" Inherits="Comments" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Leave Your Comments</title>
<style type="text/css">
.style2 { width: 250px; }
.style3 { color: #793300; }
</style>
</head>
<body>
<form id="frmComments" runat="server">
<div style="text-align: center">
<h1 class="style3">Please leave your comments below.</h1>
<table align="center">
<tr><td class="style3">
First Name : </td>
<td class="style2">
<asp:TextBox ID="txtFName" runat="server" Width="250px"></asp:TextBox></td> </tr>
<tr> <td class="style3">
Last Name : </td>
<td class="style2">
<asp:TextBox ID="txtLName" runat="server" Width="250px"></asp:TextBox></td></tr>
<tr> <td class="style3">
E-Mail : </td>
<td class="style2">
<asp:TextBox ID="txtEmail" runat="server" Width="250px"></asp:TextBox></td></tr>
<tr> <td class="style3"> Comments : </td>
<td class="style2">
<asp:TextBox ID="txtComments" runat="server" TextMode = "MultiLine" Height="60px" Width="250px"></asp:TextBox></td></tr>
</table>
<br /><asp:ImageButton ID="btnContactUs" runat="server" Height="50px"
ImageUrl="~/Images/Dark_Continue.gif" />
</div>
</form>
</body>
</html>
Comments.aspx.vb的内容是:
Imports System.Data.OleDb
Partial Class Comments
Inherits System.Web.UI.Page
Private Property FNameParam As Object
Private Property LNameParam As Object
Private Property CommentsParam As Object
Private Property EMailParam As OleDbParameter
Sub ImageButtonRun_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=/Test.mdb"
Dim dbConnection As New OleDbConnection(ConnectionStringSettings)
dbConnection.Open()
Dim commandString As String = "INSERT INTO Contacts(FName, LName, EMail, Comments) " & _
"Values(@FName, @LName, @EMail, @Comments)"
Dim dbCommand As New OleDbCommand(commandString, dbConnection)
Dim FNameParam As New OleDbParameter("@FName", OleDbType.VarChar, 50)
FNameParam.Value = txtFName.Text
dbCommand.Parameters.Add(FNameParam)
Dim LNameParam As New OleDbParameter("@LName", OleDbType.VarChar, 50)
LNameParam.Value = txtLName.Text
dbCommand.Parameters.Add(LNameParam)
Dim EMailParam As New OleDbParameter("@EMail", OleDbType.VarChar, 255)
EMailParam.Value = txtEmail.Text
dbCommand.Parameters.Add(EMailParam)
Dim CommentsParam As New OleDbParameter("@Comments", OleDbType.VarChar, 255)
CommentsParam.Value = txtComments.Text
dbCommand.Parameters.Add(CommentsParam)
dbCommand.ExecuteNonQuery()
dbConnection.Close()
End Sub
Private Function ConnectionStringSettings() As String
Throw New NotImplementedException
End Function
End Class
填写表单并单击图像按钮后,完全没有任何反应。我所阅读的大部分内容都指向了我这个方向&#39;但这显然是错的。请帮忙。
答案 0 :(得分:0)
假设您的网页发布了某些内容并实际进入课程,请尝试更改
OleDbConnection(ConnectionStringSettings)
到
OleDbConnection(connectionString)
ConnectionStringSettings
是一个立即抛出异常的函数(您可能会在F12工具中直接看到它)
connectionString
是包含连接字符串