2011-07-19 19 views
0

我正在將我的應用程序從MSSQL切換到MYSQL。當我使用MSSQL,我通過使用asp.net和SqlDataSource控件的MySQL輸出參數

Private Sub dsImpoundInformation_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles dsImpoundInformation.Inserted 
    _impoundId = e.Command.Parameters("impoundId").Value 
End Sub 

Private Sub dsImpoundInformation_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles dsImpoundInformation.Inserting 
    Dim impoundIdparam As New SqlClient.SqlParameter() 
    impoundIdparam.ParameterName = "impoundId" 
    impoundIdparam.Direction = System.Data.ParameterDirection.Output 
    impoundIdparam.DbType = DbType.Int32 
    impoundIdparam.Value = 0 
    e.Command.Parameters.Add(impoundIdparam) 
End Sub 

InsertCommand="INSERT INTO LotManager_impounds (accountId, truckId, createdBy, driver, locationId, dateArrived, towedFrom, reasonForImpound, reasonForImpoundOther, impoundCity, impoundCounty, timeOfImpound, dateDeemedAbandoned, ticketNumber) VALUES (@accountId,@truckId,@createdBy,@driver,@locationId,@dateArrived,@towedFrom,@reasonForImpound,@reasonForImpoundOther,@impoundCity,@impoundCounty,@timeOfImpound,@dateDeemedAbandoned,@ticketNumber); SET @impoundId = SCOPE_IDENTITY();" 

檢索到的最後一個自動遞增值現在,當我嘗試

InsertCommand="INSERT INTO LotManager_impounds (accountId, truckId, createdBy, driver, locationId, dateArrived, towedFrom, reasonForImpound, reasonForImpoundOther, impoundCity, impoundCounty, timeOfImpound, dateDeemedAbandoned, ticketNumber) VALUES (@accountId,@truckId,@createdBy,@driver,@locationId,@dateArrived,@towedFrom,@reasonForImpound,@reasonForImpoundOther,@impoundCity,@impoundCounty,@timeOfImpound,@dateDeemedAbandoned,@ticketNumber); SET @impoundId = LAST_INSERT_ID();" 

我得到的錯誤:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0 = LAST_INSERT_ID()' at line 1

當我嘗試:
InsertCommand="INSERT INTO LotManager_impounds (accountId, truckId, createdBy, driver, locationId, dateArrived, towedFrom, reasonForImpound, reasonForImpoundOther, impoundCity, impoundCounty, timeOfImpound, dateDeemedAbandoned, ticketNumber) VALUES (@accountId,@truckId,@createdBy,@driver,@locationId,@dateArrived,@towedFrom,@reasonForImpound,@reasonForImpoundOther,@impoundCity,@impoundCounty,@timeOfImpound,@dateDeemedAbandoned,@ticketNumber); SET impoundId = LAST_INSERT_ID();" 

我得到的錯誤:

Unknown system variable 'impoundId'

最終,我只是想獲得最後的自動增加值,但也有我計劃在切換到我的其他應用程序代碼的其它部分MYSQL取決於輸出參數。我還沒有探索過使用存儲過程,但是現在我想以類似於MSSQL的方式工作。

在此先感謝。

回答

0

最後我打破了決定使用存儲過程。這是以任何方式完成它的最好方法,並使代碼更加清潔。對於任何遇到同樣問題的人,我建議你不要浪費你時間去嘗試使它工作,而只是使用存儲過程。

相關問題