如何将客户详细信息插入Orders表

时间:2012-05-04 17:35:47

标签: sql vb.net e-commerce

我有一项任务是建立一个基本的数据库驱动的电子商务网站。我有一个主页,一个产品页面,一个订单页面,一个订单确认页面,一个购物车页面和一个查看当前订单页面。该站点使用具有三个表的Access数据库。

  

包含所有客户详细信息的Customer表(FirstName,LastName,EmailAdd,CardNo,CardEx,SortCode,DeliveryAdd,Postcode)

     

产品表,包含所有产品信息,(ProductID,ProductName,Price,ProductType,Images,ProductDescription)。

     

包含CustomerID和ProductID的Orders表。

我尝试在订单页面上创建INSERT语句,以便当客户插入其详细信息并按下提交按钮时,customers表将插入新记录。我还想在订单表中创建一个条目,并将客户端重定向到订单确认页面,该页面将显示订单的详细信息。

这是我在订单表单上单击提交按钮时运行的代码。

编辑我用缺失的撇号修正了错误。尝试使用两个sql命令插入,因为我已经告诉访问数据库不能同时处理两个。虽然仍然有错误。

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

    Dim strFirstName As String
    Dim strLastName As String
    Dim strEmailAdd As String
    Dim intCardNo As String
    Dim strCardEx As String
    Dim intSortCode As String
    Dim strDeliveryAdd As String
    Dim strPostCode As String
    Dim intProductID As Integer
    strFirstName = tbxFirstName.Text
    strLastName = tbxLastName.Text
    strEmailAdd = tbxEmailAdd.Text
    intCardNo = tbxCardNo.Text
    strCardEx = tbxCardEx.Text
    intSortCode = tbxSortCode.Text
    strDeliveryAdd = tbxDeliveryAdd.Text
    strPostCode = tbxPostcode.Text
    intProductID = ddlProduct.SelectedValue

    Dim strDatabaseNameAndLocation As String
    strDatabaseNameAndLocation = Server.MapPath("KingToots.mdb")
    Dim strSQLCommand As String
    strSQLCommand = "INSERT INTO Customer(FirstName, LastName, EmailAdd, CardNo, CardEx, SortCode, DeliveryAdd, Postcode) " & _
        "Values ('" & strFirstName & "', '" & strLastName & "', '" & strEmailAdd & "', '" & intCardNo & "', '" & strCardEx & "', '" & intSortCode & "', '" & strDeliveryAdd & "', '" & strPostCode & "');"
    Dim objOleDbConnection As System.Data.OleDb.OleDbConnection
    objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)
    objOleDbConnection.Open()
    Dim objOleDbCommand As System.Data.OleDb.OleDbCommand
    objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection)
    objOleDbCommand.ExecuteNonQuery()
    objOleDbConnection.Close()
    strSQLCommand = "INSERT INTO Orders(ProductID) " & "Values ('" & intProductID & "');"
    objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)
    objOleDbConnection.Open()
    objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection)
    objOleDbCommand.ExecuteNonQuery()
    objOleDbConnection.Close()
    strSQLCommand = "SELECT Customer.* FROM Customer ORDER BY Customer.CustomerID DESC;"
    objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)
    objOleDbConnection.Open()
    objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection)
    Dim objOleDbDataReader As System.Data.OleDb.OleDbDataReader
    objOleDbDataReader = objOleDbCommand.ExecuteReader()
    Dim datDataTable As System.Data.DataTable
    datDataTable = New System.Data.DataTable()
    datDataTable.Load(objOleDbDataReader)
    objOleDbConnection.Close()
    tbxFirstName.Text = ""
    tbxLastName.Text = ""
    tbxEmailAdd.Text = ""    
    tbxCardNo.Text = ""
    tbxCardEx.Text = ""
    tbxSortCode.Text = ""
    tbxDeliveryAdd.Text = ""
    tbxPostcode.Text = ""

End Sub

2 个答案:

答案 0 :(得分:1)

你错过了这一行末尾的收尾报价:

strSQLCommand = "INSERT INTO Customer(FirstName, LastName, EmailAdd, CardNo, CardEx, SortCode, DeliveryAdd, Postcode) " & _
    "Values ('" & strFirstName & "', '" & strLastName & "', '" & strEmailAdd & "', '" & intCardNo & "', '" & strCardEx & "', '" & intSortCode & "', '" & strDeliveryAdd & "', '" & strPostCode & ");"

关于明显的SQL注入问题,切换到参数将是最好的方法(如果你这样做,你就永远不会有原始问题,参数不使用引号),但至少运行一个替换你的字符串以用'替换'',这样你的程序就不会因为得到一个名叫O'Neil的客户而死亡。

答案 1 :(得分:0)

他是对的,你不想这样做你会得到sql注入。但无论如何,这里都是你问题的解决方案。

问题不在最后一个sql语句中,而是在前一个sql语句中。

'“& strPostCode&”缺少最后一个单引号。

应该是:

'“& strPostCode&”');