0

这段代码真的让我发疯。

今天早些时候,在这个很棒的论坛上,我得到了使用以下代码将初始默认值加载到数据库中的帮助。

 s = "INSERT INTO tblTrainings (CourseId, tblLocations.LocationId, dateId,AvailableSeats) (SELECT @CosID, @LocID, @dat,Seating_Capacity FROM tblLocations, tblCourses WHERE tblCourses.locationId= tblLocations.LocationId and courseId = @cosId and tblLocations.locationID=@LocID)"

 s += "UPDATE tblTrainings SET AvailableSeats = AvailableSeats - 1 WHERE CourseID = @cosID AND LocationID = @locId AND dateId = @dat"

历史:

我有一个包含几行记录的 dataList 和一个链接,上面写着“单击此处注册。

当您将鼠标悬停在此链接上时,它会显示日期、课程、位置的 ID。

当用户单击该链接时,if 检查数据库以查看 dateId、CourseId、LocationId 和 AvailableSeats 是否为空。

问题:

我在上述解决方案中遇到的问题是,每次用户点击注册链接时,都会更新现有记录。这很好,但同时,一条新记录被插入到数据库中,这并不好。

因此,我决定先进行检查。

检查数据库中是否存在满足某些条件的记录(在下面的 SELECT 语句中指定)

如果availableSeats (RemainingSeates) 为null,dateId 为null,courseid 为null,locationid 为null,则执行INSERT 语句。

如果它们不为空,则执行 UPDATE 语句。

到目前为止我做了什么并不重要,只执行 UPDATE 语句。

任何想法如何解决这个问题?

下面是我正在使用的代码:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim username = Session.Item("Username").ToString
        Dim connStr As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
        Dim conn As New SqlConnection(connStr)
        Try
             Dim s As String
            Dim counter As Integer

            'If AvailableSeats already saved, then don't do an INSERT
            s = "SELECT Count(*) Counter FROM tblTrainings WHERE AvailableSeats Is Null and CourseId is null and LocationId is null and dateId is null"
            'Response.Write(s)
            'Response.End()
            Dim connSt As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
            Dim connc As New SqlConnection(connSt)
            Dim cmdc As New SqlCommand(s, connc)
            connc.Open()
            cmdc.ExecuteNonQuery()
            counter = cmdc.ExecuteScalar()


            '   Now let's see if we found existing record
            If counter > 0 Then
                s = "INSERT INTO tblTrainings (CourseId, tblLocations.LocationId, dateId,AvailableSeats) (SELECT @CosID, @LocID, @dat,Seating_Capacity FROM tblLocations, tblCourses WHERE tblCourses.locationId= tblLocations.LocationId and courseId = @cosId and tblLocations.locationID=@LocID)"
                s += "UPDATE tblTrainings SET AvailableSeats = AvailableSeats - 1 WHERE CourseID = @cosID AND LocationID = @locId AND dateId = @dat"
            Else
                s += "UPDATE tblTrainings SET AvailableSeats = AvailableSeats - 1 WHERE CourseID = @cosID AND LocationID = @locId AND dateId = @dat"

            End If

            Response.Write(s)
            Response.End()

            Dim cmd = New SqlCommand(s, conn)
            cmd.Parameters.AddWithValue("@cosID", Request.QueryString("cosId"))
            cmd.Parameters.AddWithValue("@locID", Request.QueryString("locid"))
            cmd.Parameters.AddWithValue("@dat", Request.QueryString("iddate"))
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            'Display some feedback to the user to let them know it was processed
            Label1.ForeColor = System.Drawing.Color.Green
            Label1.Text = "Record successfully saved!"


        Catch

            'If the message failed at some point, let the user know
            Label1.ForeColor = System.Drawing.Color.Red
            Label1.Text = "Your record failed to save, please try again."

        End Try
    End Sub


    s = "IF EXISTS (SELECT TrainingId FROM  tblTrainings WHERE AvailableSeats Is NOT NULL and CourseId is NOT NULL and LocationId is NOT NULL and dateId is NOT NULL) "
    s += " BEGIN " '***Record already exists in the tblTrainings table, update existing record instead instead***
    s += "UPDATE tblTrainings SET AvailableSeats = AvailableSeats - 1 WHERE CourseID = @cosID AND LocationID = @locId AND dateId = @dat "
    s += " End "
    s += " ELSE "
    s += " BEGIN " '***No record exists in the tblTrainings table; create one and update it at same time.***
    s += " INSERT INTO tblTrainings (CourseId, tblLocations.LocationId, dateId,AvailableSeats) (SELECT @CosID, @LocID, @dat,Seating_Capacity FROM tblLocations, tblCourses WHERE tblCourses.locationId= tblLocations.LocationId and courseId = @cosId and tblLocations.locationID=@LocID)"
    s += "UPDATE tblTrainings SET AvailableSeats = AvailableSeats - 1 WHERE CourseID = @cosID AND LocationID = @locId AND dateId = @dat"
    s += " End "
4

2 回答 2

0

使用过滤条件....

Dim cmd as SqlCommand
Dim s1 as String = "" '----> insert stat

s = "UPDATE tblTrainings SET AvailableSeats = AvailableSeats - 1 WHERE CourseID = @cosID AND LocationID = @locId AND dateId = @dat"

conn.Open()    
If counter = 0 Then
    'here is the code you can get value for availableseat 

    s1 = "INSERT INTO tblTrainings (CourseId, tblLocations.LocationId, dateId,AvailableSeats) VALUES (@CosID, @LocID, @dat, @Capacity)" 

    Response.Write(s1)
    Response.End()

    cmd = New SqlCommand(s1, conn)

    cmd.Parameters.AddWithValue("@cosID", Request.QueryString("cosId"))
    cmd.Parameters.AddWithValue("@locID", Request.QueryString("locid"))
    cmd.Parameters.AddWithValue("@dat", Request.QueryString("iddate"))
    cmd.Parameters.AddWithValue("@capacity",  ... ) '----------------> fill the value 

    cmd.ExecuteNonQuery()     
End If

Response.Write(s)
Response.End()

cmd = New SqlCommand(s, conn)

cmd.Parameters.AddWithValue("@cosID", Request.QueryString("cosId"))
cmd.Parameters.AddWithValue("@locID", Request.QueryString("locid"))
cmd.Parameters.AddWithValue("@dat", Request.QueryString("iddate"))

cmd.ExecuteNonQuery()
conn.Close()
于 2013-06-02T23:32:33.030 回答
-1

在 INSERT 语句的末尾需要一个分号:

If counter > 0 Then
    s = "INSERT INTO tblTrainings (CourseId, tblLocations.LocationId, 
    dateId,AvailableSeats) 
(SELECT @CosID, @LocID, @dat,Seating_Capacity FROM tblLocations, tblCourses 
WHERE tblCourses.locationId= tblLocations.LocationId 
and courseId = @cosId and tblLocations.locationID=@LocID);"
    s += "UPDATE tblTrainings SET AvailableSeats = AvailableSeats - 1 
WHERE CourseID = @cosID AND LocationID = @locId AND dateId = @dat"
Else
    s += "UPDATE tblTrainings SET AvailableSeats = AvailableSeats - 1 
WHERE CourseID = @cosID AND LocationID = @locId AND dateId = @dat"

End If
于 2013-06-02T23:36:42.233 回答