MySQL输出参数与asp.net和SqlDataSource控件

时间:2011-07-19 20:40:33

标签: asp.net mysql sql-server vb.net

我正在将我的应用程序从MSSQL切换到MYSQL。当我使用MSSQL时,我通过

检索了最后一个自动增量值
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();"

我收到错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   在第1行'0 = LAST_INSERT_ID()'附近

当我尝试时:

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();"

我收到错误:

  

未知的系统变量'impoundId'

最终,我只是想获取最后一个自动增量值,但我在其他应用程序中还有其他部分代码,我计划切换到依赖于输出参数的MYSQL。我还没有探索过使用存储过程,但是现在我想让它以类似于MSSQL的方式工作。

提前致谢。

1 个答案:

答案 0 :(得分:0)

最后我崩溃了,决定使用存储过程。这是以任何方式执行此操作的最佳方式,并使代码更清晰。对于那些遇到同样问题的人,我给你的建议是不要浪费你的时间来使它工作并只使用存储过程。