I’m new to the SQL query language and I’m trying to figure out how to delete a row (in a SQL DB table) with a non key ID column value using vb.net. For example in the table TEST below:
ID Name
1 x
2 y
3 z
I have a windows form that the user selects the Name value, however when I run my code it says
Cannot convert varchar “y” to int.
Here is my code attempt:
Dim sConnectionString As String
sConnectionString = "Data Source=" + serverName + ";Initial Catalog=" + dbName + ";Integrated Security=SSPI;"
Dim objConn As New SqlConnection(sConnectionString)
objConn.Open()
Dim cmd As New SqlCommand
cmd.Connection = objConn
cmd.CommandText = "DELETE FROM TEST WHERE Name = y"
cmd.ExecuteNonQuery()
Thank you!
If your Name column is of type varchar you need to enclose the value in single quotes
A complete answer to your question will be:
I’m supposing that your user insert the name to delete in a textbox and you pass this value to a parametrized query. The use of parametrized query will save you from the hassle to handle quoting problems and from Sql Injection Attacks