将数据从asp.net插入到ms访问数据库

时间:2016-10-23 07:46:49

标签: sql asp.net ms-access

我正在尝试创建自己的网站,但我没有太多经验。我有一个问题是插入数据到MS访问。
我不太确定我的代码有什么问题,不知何故它不起作用。 下面是我的代码:

    <%@ Page Language="VB" Debug="true" enableEventValidation="false" EnableViewStateMac="false"  %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.Oledb" %>
    <%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="System.Net.Mail" %>
    <%@ Import Namespace="System.Web" %>




   <script runat="server">
   Dim LUsername As String
   Dim dpic As Image
   Dim MyCookieValue As String
   Dim objConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("\easytourtravelagency.mdb"))
   Dim dr As OleDbDataReader
   Dim ds1 As New DataSet()
   Dim ds2 As New DataSet()


   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

   Dim cmd124 As New OleDbCommand("INSERT INTO BookingDetails(CarID,Name,Innitial,EmailAddress,ContactNo,Gender,PickUpLocation,DateAndTime,NumberOfPassanger,SpecialRequest Values (@CarID, @Name, @EmailAddress, @ContactNo,@Gender,@PickUpLocation,@DateAndTime,@NumberOfPassanger,@SpecialRequest))", objConn)
    cmd124.Parameters.AddWithValue("@CarID", CarID.Text)
    cmd124.Parameters.AddWithValue("@Name", Name.Text)
    cmd124.Parameters.AddWithValue("@EmailAddress", EmailAddress.Text)
    cmd124.Parameters.AddWithValue("@ContactNo", ContactNo.Text)
    cmd124.Parameters.AddWithValue("@Gender", Gender.Text)
    cmd124.Parameters.AddWithValue("@PickUpLocation", PickUpLocation.Text)
    cmd124.Parameters.AddWithValue("@DateAndTime", DateAndTime.Text)
    cmd124.Parameters.AddWithValue("@NumberOfPassanger", NumberOfPassanger.Text)
    cmd124.Parameters.AddWithValue("@SpecialRequest", SpecialRequest.Text)
    cmd124.Connection.Open()
    cmd124.ExecuteNonQuery()
    cmd124.Connection.Close()
End Sub

1 个答案:

答案 0 :(得分:0)

虽然我不知道ASP,但是你的SQL有语法错误,因为它在列出列之后缺少INSERT INTO子句中的右括号,并且在VALUES子句之后有一个额外的括号:

INSERT INTO BookingDetails(CarID,Name,Innitial,EmailAddress,
              ContactNo,Gender,PickUpLocation,DateAndTime,
              NumberOfPassanger,SpecialRequest              -- MISSING PARERNTHESIS
VALUES (@CarID, @Name, @EmailAddress, @ContactNo,@Gender,@PickUpLocation,
        @DateAndTime,@NumberOfPassanger,@SpecialRequest))   -- EXTRA PARENTHESIS