财政年度从4月1日开始到明年3月底结束。所以我需要插入数据才会成为新的财政年度,否则只需更新数据。 这是我的源代码:
Dim nowYear As Integer
nowYear = Date.Now.Year
Dim nowMonth As Integer
nowMonth = Date.Now.Month
Dim NEWYEAR As Integer
Dim reccount As Integer = 0
If nowMonth >= 4 Then
NEWYEAR = nowYear + 1
End If
cmd = New SqlCommand
cmd.CommandText = "SELECT COUNT(*) FROM ScanningData WHERE ASSET_NUMBER=" & AssetTxt.Text & " AND LOC_DEPT=" & DeptCodeTxt.Text & " and UPDATE_DATE='" & NEWYEAR & "';"
cmd.Connection = Conn
rdmysql = cmd.ExecuteReader
If rdmysql.Read = True Then
reccount = Val(rdmysql.GetInt32(0))
End If
cmd.Dispose()
rdmysql.Close()
If reccount = 0 Then
strsql = "INSERT INTO ScanningData"
strsql += "(ASSET_NUMBER,LOC_DEPT,DEPT_DESCRIPTION,MAJOR_CATEGORY,DATE_PLACED_IN_SERVICE,LOC_AREA,TAG_NUMBER,CURRENT_UNITS,DESCRIPTION,MYR_COST_BY_UNIT,COST_BY_UNIT,DEPRN_RESERVE_BY_UNIT,DATE_RETIRED,UPDATE_DATE,UPDATE_BY) VALUES "
strsql += "(@ID, @CODE, @DEPT, @CATEGORY, @SERVICEDATE, @AREA, @TAG, @UNIT, @DESCRIPTION, @MYRCOST, @USDCOST, @DEPR, @RETIREDDATE, @UPDATEDATE, @NAME);"
cmd = New SqlCommand
cmd.Connection = Conn
cmd.Parameters.AddWithValue("@ID", AssetTxt.Text)
cmd.Parameters.AddWithValue("@CODE", DeptCodeTxt.Text)
cmd.Parameters.AddWithValue("@DEPT", DeptTxt.Text)
cmd.Parameters.AddWithValue("@CATEGORY", CategoryTxt.Text)
cmd.Parameters.AddWithValue("@SERVICEDATE", ServiceTxt.Text)
cmd.Parameters.AddWithValue("@AREA", LocTxt.Text)
cmd.Parameters.AddWithValue("@TAG", TagTxt.Text)
Dim unit As Decimal = Decimal.Parse(UnitTxt.Text)
cmd.Parameters.AddWithValue("@UNIT", unit)
cmd.Parameters.AddWithValue("@DESCRIPTION", DescTxt.Text)
Dim MYRCost As Decimal = Decimal.Parse(CostMYRTxt.Text)
cmd.Parameters.AddWithValue("@MYRCOST", MYRCost)
Dim USDCost As Decimal = Decimal.Parse(CostUSDTxt.Text)
cmd.Parameters.AddWithValue("@USDCOST", USDCost)
Dim Depr As Decimal = Decimal.Parse(DeprTxt.Text)
cmd.Parameters.AddWithValue("@DEPR", Depr)
cmd.Parameters.AddWithValue("@RETIREDDATE", RetiredDateTxt.Text)
cmd.Parameters.AddWithValue("@UPDATEDATE", ServerDate)
cmd.Parameters.AddWithValue("@NAME", UserIDLabel.Text)
cmd.CommandText = strsql
cmd.ExecuteNonQuery()
End If
目前我的代码总是插入,尽管它在同一个财政年度。只有在同一财政年度才应该更新
答案 0 :(得分:0)
我没有看到所有日期的重点。如果存在复合主键的记录,则必须更新它。如果它不存在则必须插入。这是正确的,无论日期如何。
Private Sub UpateOrInsertAssets()
Dim strsql As String
Dim cn As New SqlConnection("Your connection string")
Dim cmd As New SqlCommand("SELECT COUNT(*) FROM ScanningData WHERE ASSET_NUMBER= @Asset AND LOC_DEPT= @Dept;", cn)
cmd.Parameters.AddWithValue("@Asset", AssetTxt.Text)
cmd.Parameters.AddWithValue("@Dept", DeptCodeTxt.Text)
cn.Open()
Dim reccount As Integer = CInt(cmd.ExecuteScalar)
cn.Close()
cmd.Dispose()
cmd = New SqlCommand()
cmd.Connection = cn
cmd.Parameters.AddWithValue("@ID", AssetTxt.Text)
cmd.Parameters.AddWithValue("@CODE", DeptCodeTxt.Text)
cmd.Parameters.AddWithValue("@DEPT", DeptTxt.Text)
cmd.Parameters.AddWithValue("@CATEGORY", CategoryTxt.Text)
cmd.Parameters.AddWithValue("@SERVICEDATE", ServiceTxt.Text)
cmd.Parameters.AddWithValue("@AREA", LocTxt.Text)
cmd.Parameters.AddWithValue("@TAG", TagTxt.Text)
Dim unit As Decimal = Decimal.Parse(UnitTxt.Text)
cmd.Parameters.AddWithValue("@UNIT", unit)
cmd.Parameters.AddWithValue("@DESCRIPTION", DescTxt.Text)
Dim MYRCost As Decimal = Decimal.Parse(CostMYRTxt.Text)
cmd.Parameters.AddWithValue("@MYRCOST", MYRCost)
Dim USDCost As Decimal = Decimal.Parse(CostUSDTxt.Text)
cmd.Parameters.AddWithValue("@USDCOST", USDCost)
Dim Depr As Decimal = Decimal.Parse(DeprTxt.Text)
cmd.Parameters.AddWithValue("@DEPR", Depr)
cmd.Parameters.AddWithValue("@RETIREDDATE", RetiredDateTxt.Text)
cmd.Parameters.AddWithValue("@UPDATEDATE", ServerDate)
cmd.Parameters.AddWithValue("@NAME", UserIDLabel.Text)
If reccount = 0 Then 'The record doesn't exist
strsql = "INSERT INTO ScanningData"
strsql += "(ASSET_NUMBER,LOC_DEPT,DEPT_DESCRIPTION,MAJOR_CATEGORY,DATE_PLACED_IN_SERVICE,LOC_AREA,TAG_NUMBER,CURRENT_UNITS,DESCRIPTION,MYR_COST_BY_UNIT,COST_BY_UNIT,DEPRN_RESERVE_BY_UNIT,DATE_RETIRED,UPDATE_DATE,UPDATE_BY) VALUES "
strsql += "(@ID, @CODE, @DEPT, @CATEGORY, @SERVICEDATE, @AREA, @TAG, @UNIT, @DESCRIPTION, @MYRCOST, @USDCOST, @DEPR, @RETIREDDATE, @UPDATEDATE, @NAME);"
cmd.CommandText = strsql
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
ElseIf reccount = 1 Then 'The record exists
cmd.CommandText = "Update ScanningData Set ASSET_NUMBER = @ID,LOC_DEPT = @CODE,DEPT_DESCRIPTION = @DEPT,
MAJOR_CATEGORY = @CATEGORY,DATE_PLACED_IN_SERVICE = @SERVICEDATE,LOC_AREA = @AREA,TAG_NUMBER = @TAG,
CURRENT_UNITS = @UNIT,DESCRIPTION = @DESCRIPTION,MYR_COST_BY_UNIT = @MYRCOST, COST_BY_UNIT = @USDCOST,
DEPRN_RESERVE_BY_UNIT = @DEPR, DATE_RETIRED = @RETIREDDATE, UPDATE_DATE = @UPDATEDATE,UPDATE_BY = @NAME;"
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
End If
End Sub
您需要添加适当的Using语句和/或Try Catch Finally,以确保即使出现错误也可以关闭和处理对象。