Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013
This lesson describes how to delete data in a database
As well as reading, inserting, and updating data, you sometimes need to delete data from the data source.
To remove data, you use the DELETE statement that removes entire rows, so you need to determine the records you want to delete with WHERE condition of the query.
The Syntax of the Delete Statement is as follows:
DELETE [FROM] table-name [WHERE selection-criteria]
The following code shows you how to delete a single row from a database table:
Dim ConnectionString As String = _ "Data Source=SqlServerNameOrIP;Initial Catalog=DBName;Integrated Security=True" Dim connection As New SqlConnection(ConnectionString) Try connection.Open() ' declare and create a sql command object Dim command As SqlCommand = connection.CreateCommand ' set the sql statement to execute at the data source command.CommandText = "DELETE FROM Users WHERE Username = @Username" ' add the parameters command.Parameters.AddWithValue("@Username", Username.Text) ' execute the sql statement against the connection command.ExecuteNonQuery() ' release the resources command.Dispose() Catch ex As Exception Console.Write(ex.Message) Finally ' close the connection connection.Close() End Try