2013-04-18 38 views
0

我有我的保存按鈕和更新按鈕的代碼,但有沒有辦法將兩個 命令組合在一個按鈕中?保存新紀錄並在數據編輯時更新

例如當我點擊「保存/更新按鈕」時,它將保存在數據庫中,如果它是新記錄,並且它會更新,如果系統發現數據庫中已經有記錄並保存編輯後的數據的話

代碼以進行保存按鈕

Dim sqlconn As New SqlClient.SqlConnection 
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _ 
    "Database = EOEMS;integrated security=true" 
    Try 
     Dim myCommand As New SqlCommand 
     sqlconn.Open() 
     myCommand = New SqlCommand("INSERT INTO tblOfficeEquipmentProfile(OE_Category,OE_SubCategory,OE_ID,OE_Name,OE_User,OE_Brand,OE_Model,OE_Specs,OE_SerialNo,OE_PropertyNo,OE_MacAddress,OE_Static_IP,OE_Vendor,OE_PurchaseDate,OE_WarrantyInclusiveYear,OE_WarrantyStatus,OE_Status,OE_Dept_Code,OE_Location_Code,OE_Remarks) VALUES('" & cmbCategory.Text & "','" & cmbSubCategory.Text & "','" & txtOEID.Text & "','" & txtName.Text & "','" & txtUser.Text & "','" & cmbBrand.Text & "','" & cmbModel.Text & "','" & txtSpecs.Text & "','" & txtSerialNo.Text & "','" & txtPropertyNo.Text & "','" & txtMacAddress.Text & "','" & txtStaticIp.Text & "','" & txtVendor.Text & "','" & txtPurchaseDate.Text & "','" & txtWarrantyInclusiveYear.Text & "', '" & txtWarrantyStatus.Text & "','" & txtStatus.Text & "','" & cmbDeptCode.Text & "','" & cmbLocationCode.Text & "','" & txtRemarks.Text & "')", sqlconn) 
     myCommand.ExecuteNonQuery() 
     MessageBox.Show("Office Equipment Profile Successfully Added") 
     sqlconn.Close() 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 

CODE FOR UPDATE按鈕(注:但是我的更新按鈕一些錯誤仍然試圖修復)

Dim sqlconn As New SqlClient.SqlConnection 
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _ 
    "Database = EOEMS;integrated security=true" 

    Dim myCommand As New SqlCommand 


    Try 

     'update command 
     sqlconn.Open() 

     myCommand = New SqlCommand("UPDATE tblOfficeEquipmentProfile SET OE_Category = '" & cmbCategory.Text & "',OE_SubCategory = '" & cmbSubCategory.Text & "', OE_Name = '" & txtName.Text & "', OE_User = '" & txtUser.Text & "', OE_Brand = '" & cmbBrand.Text & "', OE_Model = '" & cmbModel.Text & "', OE_Specs = '" & txtSpecs.Text & "', OE_SerialNo = '" & txtSerialNo.Text & "', OE_PropertyNo = '" & txtPropertyNo.Text & "', OE_MacAddress = '" & txtMacAddress.Text & "', OE_Static_IP = '" & txtStaticIp.Text & "', OE_Vendor = '" & txtVendor.Text & "', OE_PurchaseDate = '" & txtPurchaseDate.Text & "', OE_WarrantyInclusiveYear = '" & txtWarrantyInclusiveYear.Text & "', OE_WarrantyStatus = '" & txtWarrantyStatus.Text & "', OE_Status = '" & txtStatus.Text & "', OE_Dept_Code = '" & cmbDeptCode.Text & "', OE_Location_Code = '" & cmbLocationCode.Text & "', OE_Remarks ='" & txtRemarks.Text & "' WHERE OE_ID ='" & txtOEID.Text & "'", sqlconn) 
     Dim iCnt As Integer = myCommand.ExecuteNonQuery() 
     MessageBox.Show("Office Equipment Profile Successfully Updated " & iCnt & " Records") 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 

回答

0

你可以創建一個包含狀態的枚舉:

Enum DataState 
     Editing 
     Adding 
     None 
End Enum 

然後設置一個類級變量:

private mDataState as DataState 

然後設置這個根據是否要添加或編輯記錄,然後在Save_Click子例程,使用If-Then。

0

編寫一個存儲過程來檢查記錄是否存在。如果是,請更新它。否則,插入它。

從您的vb.net代碼調用此存儲過程。此外,更改.net代碼,以便它將查詢參數發送到您的存儲過程。

0

你是一個常年最喜歡的問題在這裏SO:如何更新現有行,否則將其插入?答案總是錯誤的。

標準SQL來做到這一點沒有一個if聲明。該標準的方法是插入該行受它不存在約束,然後更新:

insert into T values (...) 
where not exists (
    select 1 from T as t 
    where T.key = t.key 
) 

可選檢查行數,只有更新,如果是0

update T set ... 
where T.key = value 

如果你不不檢查insert的行數,update是多餘的。

最好是把所有的存儲過程,但是,即使你不這樣做,你應該能夠把兩個語句在一個準備好的聲明,並附加一個按鈕,你的GUI。

相關問題