user1605859 user1605859 - 7 months ago 17
Vb.net Question

How to delete top n rows of a sqlserver table in asp.net?

I want to delete (for example) top 5 rows in my asp.net sql server database. I used the below code but it does not work. How may I get this?

Dim con As New SqlConnection(connectionString)
Using cmd = New SqlCommand()
cmd.CommandText = "DELETE From PubTb TOP 5"
cmd.Connection = con
con.Open()
Dim numberDeleted As Integer = cmd.ExecuteNonQuery()
con.Close()
End Using

Answer

See this link

Change it as per your table and Database name. Also order it if you want to

  cmd.CommandText = ";WITH CTE AS ( SELECT TOP 5 * FROM PubTb )  DELETE FROM CTE"
Comments