2012-12-26 35 views
0

我正在開發一個信息系統,這裏是我更新的語法,它沒有顯示錯誤,但它不更新我的表。任何人都可以幫忙解決這件事? 順便說一句,我使用的是2010 VB.Net和MS Access 2007代碼不起作用,但沒有錯誤

Try 
     Dim conn As New OleDbConnection(gConnectionString) 
     If conn.State = ConnectionState.Closed Then 
      conn.Open() 
     End If 
     Try 
      Dim comm As New OleDbCommand("UPDATE PropertiesPayors SET [PayorName][email protected],[LotNumber][email protected],[LotArea][email protected],[DateOfAward][email protected],[DateDueForFullPayment][email protected],[PurchasePrice][email protected],[ReAppraisedValue][email protected],[AmountDue][email protected],[TotalAmountPaid][email protected],[AmountUnpaid][email protected],[PropertyRemarks][email protected] WHERE [PropertyID][email protected] ", conn) 
      With comm 
       With .Parameters 
        .AddWithValue("@PropertyPropertyID", Val(propertyPayorSessionID.ToString)) 
        .AddWithValue("@PayorName", txtPayorName.Text) 
        .AddWithValue("@LotNumber", txtLotNumber.Text) 
        .AddWithValue("@LotArea", Val(txtLotArea.Text)) 
        .AddWithValue("@DateOfAward", txtDateOfAward.Text.ToString) 
        .AddWithValue("@DateDueForFullPayment", txtDateOfFullPayment.Text.ToString) 
        .AddWithValue("@PurchasePrice", Val(txtPurchasePrice.Text)) 
        .AddWithValue("@ReAppraisedValue", Val(txtReAppraisedValue.Text)) 
        .AddWithValue("@AmountDue", Val(txtAmountDue.Text)) 
        .AddWithValue("@TotalAmountPaid", Val(txtTotalAmountPaid.Text)) 
        .AddWithValue("@AmountUnpaid", Val(txtAmountUnpaid.Text)) 
        .AddWithValue("@PropertyRemarks", txtRemarks.Text) 
       End With 
       .ExecuteNonQuery() 
      End With 
      msg = MsgBox("Record Updated.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Update Payor") 
     Catch myError As Exception 
      MsgBox("Error: " & myError.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Query Error") 
     End Try 
    Catch myError As Exception 
     MsgBox("Error: " & myError.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Connection Error") 
    End Try 
+1

可能是* this *代碼或*方法/函數*未被調用和執行。 – adatapost

+0

感謝您的回覆,但我確定我打電話給他。因爲這個,我真的很難過。 – Kosmo

+0

刪除所有try..catch語句,看看會發生什麼?請發佈*異常/錯誤*描述。請包括* event *處理程序定義(button_click或類似的東西)。 – adatapost

回答

1

你只是有一個錯字在你的代碼

更換

@PropertyPropertyID 

@PropertyPayorID 

然後安排您的參數順序與您的更新聲明相同。

而且試試這個:

Try 
      Dim conn As New OleDbConnection(gConnectionString) 
      If conn.State = ConnectionState.Closed Then 
       conn.Open() 
      End If 
      Try 
        Dim comm As New OleDbCommand("UPDATE PropertiesPayors SET [PayorName][email protected],[LotNumber][email protected],[LotArea][email protected],[DateOfAward][email protected],[DateDueForFullPayment][email protected],[PurchasePrice][email protected],[ReAppraisedValue][email protected],[AmountDue][email protected],[TotalAmountPaid][email protected],[AmountUnpaid][email protected],[PropertyRemarks][email protected] WHERE [PropertyID][email protected] ", conn) 
      With comm 
       With .Parameters 
        '.AddWithValue("@PropertyPayorID", Val(propertyPayorSessionID.ToString)) move this to the last part 
        .AddWithValue("@PayorName", txtPayorName.Text) 
        .AddWithValue("@LotNumber", txtLotNumber.Text) 
        .AddWithValue("@LotArea", Val(txtLotArea.Text)) 
        .AddWithValue("@DateOfAward", txtDateOfAward.Text.ToString) 
        .AddWithValue("@DateDueForFullPayment", txtDateOfFullPayment.Text.ToString) 
        .AddWithValue("@PurchasePrice", Val(txtPurchasePrice.Text)) 
        .AddWithValue("@ReAppraisedValue", Val(txtReAppraisedValue.Text)) 
        .AddWithValue("@AmountDue", Val(txtAmountDue.Text)) 
        .AddWithValue("@TotalAmountPaid", Val(txtTotalAmountPaid.Text)) 
        .AddWithValue("@AmountUnpaid", Val(txtAmountUnpaid.Text)) 
        .AddWithValue("@PropertyRemarks", txtRemarks.Text) 
        .AddWithValue("@PropertyPayorID", Val(propertyPayorSessionID.ToString)) 
       End With 
       .ExecuteNonQuery() 
      End With 
       msg = MsgBox("Record Updated.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Update Payor") 
      Catch myError As Exception 
       MsgBox("Error: " & myError.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Query Error") 
      End Try 
     Catch myError As Exception 
      MsgBox("Error: " & myError.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Connection Error") 
     End Try 

這將解決您的問題。

另請參閱:OleDbCommand parameters order and priority僅供參考。

最好的問候!

+0

感謝您的回答,但這真的是來自論壇文本編輯器的錯誤。我其實是新來的,所以放置代碼很困難。但是再一次,我仍然陷入困境。 – Kosmo

+0

再次檢查我的文章。 – BizApps

+0

是的,我在參數的第一部分看到了評論,我試過了,但仍然無法工作。 – Kosmo

0

如果我沒有記錯,默認情況下,查詢中佔位符的名稱和參數名稱之間沒有關係。正如BizApps所說,您應該按照查詢中定義的順序放置參數;這意味着PropertyPayorID在您將其添加到您的Parameters集合時應該最後一次。 Parameters集合的名稱只能在本地使用;比如改變單個參數的一些屬性。

此外,我不記得您是否可以在您的查詢字符串中使用命名參數作爲佔位符,或者您必須改用?;像Update PropertiesPayors SET [PayorName]=?, ...

+0

感謝您的回覆@SylvainL,我試過你的方法,但它沒有奏效。關於作爲佔位符的'?',我實際上並不熟悉它。我應該在參數名稱中加入什麼? – Kosmo

+0

對於參數名稱,您可以編寫任何內容;如果您稍後要更改參數元素的屬性,它只是一個本地提醒:它在本地很有用,但遠程無意義。我知道有一個選項可以讓ODBC(COM)改變這種行爲,但對於.NET OleDbCommand,我不知道,因爲我不熟悉它。 – SylvainL

0

命令語句.ExecuteNonQuery返回受影響的行數。

如果使用這意味着...

intRowsAffected = .ExecuteNonQuery() 

,值返回到變量intRowsAffected是零(0)

則意味着與你的領域屬性ID相同值的記錄(意思是你傳遞給參數集合的值...... PROPERTYPAYORSESSIONID)不存在!

這就是爲什麼你沒有收到任何錯誤,也沒有更新您的數據庫。

要仔細檢查這... 如果你的代碼聲明.EXECUTENONQUERY()是...

您可以用下面的更換...

intRowsAffected = .ExecuteNonQuery() 
Messagebox(intRowsAffected & " Data rows were updated.") 

如果消息框顯示零行被更新(沒有行被更新),那麼你的下一步是手動檢查數據庫,看看ACTUALLY行是否存在,以及它是否有你用來識別行的SAME鍵值 - 我認爲這是屬性-payor-會話ID。

另請注意,session-id隨每個會話而變化,並且始終不變。

相關問題