このコードは本当に私を夢中にさせています。
今日、この素晴らしいフォーラムで、次のコードを使用して初期デフォルト値をデータベースにロードする方法を教えてもらいました。
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が表示されます。
ユーザーがそのリンクをクリックすると、データベースをチェックして、dateId、CourseId、LocationId、および AvailableSeats が null であるかどうかを確認します。
問題:
上記のソリューションで私が抱えていた問題は、ユーザーが登録リンクにアクセスするたびに、既存のレコードが更新されることです。これは良いことですが、同時に新しいレコードがデータベースに挿入され、これは良くありません。
したがって、最初にCHECKを実行することにしました。
データベースをチェックして、特定の基準を満たす既存のレコードを確認します (以下の SELECT ステートメントで指定)
availableSeats (RemainingSeates) が null で、dateId が null で、courseid が null で locationid が null の場合は、INSERT ステートメントを実行します。
null でない場合は、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 "