SqlCommand.ExecuteScalar的應用
Dim objReturn As Object
Dim strConn As String = "Server= myServer ;database=_TestDB;User ID=abc;Password=abc;"
Dim MyConn As SqlConnection
Dim MyCmd As SqlCommand
Dim SQLStr As String = "Insert into TBXX (Data1) Values ( @Data1) ; Select SCOPE_IDENTITY() "
MyConn = New SqlConnection(strConn)
MyCmd = New SqlCommand(SQLStr, MyConn)
MyCmd.Parameters.AddWithValue("@Data1", TextBox1.Text)
MyConn.Open()
objReturn = MyCmd.ExecuteScalar()
MyConn.Close()
MyConn.Dispose()
MessageBox.Show(CType(objReturn, Integer))
上面這個VB.NET的程式碼就是使用Select SCOPE_IDENTITY()的SQL敘述取回Insert進去那一列資料的自動編號欄位值.