在Insert SQL command 的後面加上:
; SELECT @New_ID = SCOPE_IDENTITY() /* 前面有一個「;」分號 */
在SqlDataSource Inserting事件內:
Private Sub SqlDataSource1_Inserting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Inserting Dim InsertedKey As Data.SqlClient.SqlParameter InsertedKey = New Data.SqlClient.SqlParameter("@NewID", Data.SqlDbType.Int) '@NewID就是取得最新值 InsertedKey.Direction = Data.ParameterDirection.Output e.Command.Parameters.Add(InsertedKey) End Sub
在SqlDataSource Inserted事件內:Private Sub SqlDataSource1_Inserted(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Inserted '取出自動編號欄位新增後的值 e.Command.Parameters("@New_ID").Value End Sub
沒有留言:
張貼留言