数据库中相同记录的重复输入

时间:2016-02-27 04:50:05

标签: asp.net vb.net

我有一个表单,它从用户那里获取大量输入数据,包括图像。数据在表中正确插入,但它在数据表中重复输入相同的记录。请帮助我不理解为什么它会重复输入相同的数据。

Private Sub save_Click(sender As Object, e As EventArgs) Handles save.Click
        Dim name, businessCategory, subcategory, conPerson, website, email, phoneNo1, phoneNo2, mobileNumber, building, street, landpoint, area, city, stateName, zipCode, about, reg, foundationDate, founder, genBeds, icuBeds, mondayIn, mondayOut, tuesdayIn, tuesdayOut, wedIn, wedOut, thursdayIn, thursdayOut, fridayIn, fridayOut, saturdayIn, saturdayOut, sundayIn, sundayOut, visitIn, visitOut, fees, bestFor As String
        name = businessName.Text
        businessCategory = businessCat.Text
        subcategory = subCat.SelectedItem.ToString
        conPerson = contactPerson.Text
        website = websiteName.Text
        email = emailName.Text
        phoneNo1 = phone1.Text
        phoneNo2 = phone2.Text
        mobileNumber = mobile.Text
        building = buildingName.Text
        street = address.Text
        landpoint = landmark.Text
        area = areaName.Text
        city = suburb.Text
        stateName = state.Text
        zipCode = zip.Text
        about = overview.Text
        reg = regNo.Text
        foundationDate = foundation.Text
        founder = founderName.Text
        genBeds = GeneralBeds.Text
        icuBeds = ICU.Text
        mondayIn = mondayFrom.SelectedItem.ToString
        mondayOut = mondayTo.SelectedItem.ToString
        tuesdayIn = tuesdayFrom.SelectedItem.ToString
        tuesdayOut = tuesdayTo.SelectedItem.ToString
        wedIn = wedFrom.SelectedItem.ToString
        wedOut = wedTo.SelectedItem.ToString
        thursdayIn = thursdayFrom.SelectedItem.ToString
        thursdayOut = thursdayTo.SelectedItem.ToString
        fridayIn = fridayFrom.SelectedItem.ToString
        fridayOut = fridayTo.SelectedItem.ToString
        saturdayIn = saturdayFrom.SelectedItem.ToString
        saturdayOut = saturdayTo.SelectedItem.ToString
        sundayIn = sundayFrom.SelectedItem.ToString
        sundayOut = sundayTo.SelectedItem.ToString
        visitIn = visitFrom.SelectedItem.ToString
        visitOut = visitTo.SelectedItem.ToString
        fees = consultinfees.Text
        bestFor = bestknowFor.Text

        Try
            Dim filename As String = Path.GetFileName(profileDP.PostedFile.FileName)
            profileDP.SaveAs(Server.MapPath("assets/images/hospitals/" + filename))
            con.Open()

            Dim str1 As String = "INSERT INTO hospitals (`name`, `category`, `subcategory`, `contactPerson`, `websiteName`, `email`, `phone1`, `phone2`, `mobileNumber`, `buildingName`, `streetName`, `landmark`, `areaName`, `city`, `State`, `zipCode`, `thumbnail`, `consultancyFees`, `overview`, `Founder`, `establishment`, `registration`, `generalBed`, `icuBed`, `mondayFrom`, `mondayTo`, `tuesdayFrom`, `tuesdayTo`, `wednesdayFrom`, `wednesdayTo`, `thursdayFrom`, `thursdayTo`, `fridayFrom`, `fridayTo`, `saturdayFrom`, `saturdayTo`, `sundayFrom`, `sundayTo`, `visitFrom`, `visitTo`, `bestKnownFor`, `status`) values ('" + name + "', '" + businessCategory + "', '" + subcategory + "', '" + conPerson + "', '" + website + "', '" + email + "', '" + phoneNo1 + "', '" + phoneNo2 + "', '" + mobileNumber + "', '" + building + "', '" + street + "', '" + landpoint + "', '" + area + "', '" + city + "', '" + stateName + "', '" + zipCode + "', 'list-business/assets/images/hospitals/" + filename + "', '" + fees + "', '" + about + "', '" + founder + "', '" + foundationDate + "', '" + reg + "', '" + genBeds + "', '" + icuBeds + "', '" + mondayIn + "', '" + mondayOut + "', '" + tuesdayIn + "', '" + tuesdayOut + "', '" + wedIn + "', '" + wedOut + "', '" + thursdayIn + "', '" + thursdayOut + "', '" + fridayIn + "', '" + fridayOut + "', '" + saturdayIn + "', '" + saturdayOut + "', '" + sundayIn + "', '" + sundayOut + "', '" + visitIn + "', '" + visitOut + "', '" + bestFor + "', 'active' )"

            Dim str2 As MySqlDataReader
            Dim adapter As New MySqlDataAdapter
            Dim command As New MySqlCommand
            command.CommandText = str1
            command.Connection = con
            adapter.SelectCommand = command
            command.Parameters.AddWithValue("@Content_Type", "assets/images/hospitals/" + filename)
            command.ExecuteNonQuery()
            str2 = command.ExecuteReader
            con.Close()
            Response.Write("Hospital added successfully!")
        Catch ex As Exception
            Response.Write(ex)
        End Try
End Sub

2 个答案:

答案 0 :(得分:2)

请在command.ExecuteNonQuery()str2 = command.ExecuteReader之间选择一个。

  

ExecuteReader 用于将查询结果作为DataReader获取   宾语。它只是只读取记录的转发并使用它   select命令从第一个到最后一个读表。

     用于执行不返回任何查询的

ExecuteNonQuery   数据。它用于执行sql语句,如update,insert,   删除等ExecuteNonQuery执行命令并返回   受影响的行数。 - 来自site

如果您要插入记录,我不明白为什么要使用 ExecuteReader

干杯!

答案 1 :(得分:0)

如果使用INSERT,UPDATE,DELETE语句,则只应使用ExecuteNonQuery()且仅使用一次。

ExecuteReader()用于从数据库表中检索记录。

你正在做两件事,这就是为什么插入两次。