关于last_insert_id无法正常工作,请帮助我

时间:2012-02-05 03:02:40

标签: mysql vb.net

这是我的......

问题在于last_insert_id not working

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
    CMD.CommandText = "INSERT product_tbl (stockno, product_desc, price, Unit) VALUES (@stockno, @catid, @compid, @desc, @price, @unit);SELECT LAST_INSERT_ID()"
    CMD.Parameters.AddWithValue("@stockno", txtstockno.Text)
    CMD.Parameters.AddWithValue("@desc", txtdesc.Text)
    CMD.Parameters.AddWithValue("@price", txtprice.Text)
    CMD.Parameters.AddWithValue("@Unit", txtUnit.Text) 
    Dim ProductID As Int32
    Try
        ProductID = (System.Convert.ToInt32(CMD.ExecuteScalar))
    Catch ex As Exception
        TR.Rollback()
        CN.Close()
        Throw ex
    End Try 
     'the code above are working just fine but when it comes the second insert to        
     'inventory_tbl it doesn't work at all. am i missing something or did wrong? 

    CMD.CommandText = "INSERT invetory_tbl (productid, qty) VALUES(@productID, @qty)"
    CMD.Parameters.Clear()
    CMD.Parameters.AddWithValue("@orderID", ProductID)
    ' CMD.Parameters.AddWithValue("@qty", txtqty.Text)
    Try
        CMD.ExecuteNonQuery()
    Catch ex As Exception
        TR.Rollback()
        Throw ex
    End Try
    TR.Commit()
    CN.Close()

1 个答案:

答案 0 :(得分:0)

last_insert_id仅在您的表中有auto-increment变量时才有效...

你有什么桌面结构?通过执行命令DESCRIBE myTable

可以看到相同的内容

Also see if this link helps you