简体   繁体   中英

Using SQL DELETE query with VB.NET

I've written a program in VB.NET in conjunction with a SQL server database table to record the assessment marks of students. The SQL table (Marks) details columns, which show Student_ID, Assessment (eg Maths, Science, English) and Mark. The subroutine shown below is designed to allow users to delete specific assessments and, by using the DELETE statement nested within a conditional if/elseif clause, I have managed to achieve this, the program runs fine. However, as a relative newcomer to programming I am conscious about adopting good practice and, if possible, avoiding repetitive code which, in my mind, is clearly evident within the if/elseif clause. This problem becomes increasingly relevant when considering the possibility of having to deal with 15 or more student assessments. My question is, can the SQL query statement be modified to incorporate an 'assessment' variable rather than explicitly typing in each condition within an if/elseif clause? I tried to do this in the same way as the variable 'id' is used to identify the 'Student_ID' within the SQL query statement, but this returned an error stating that, for example, Maths is not a column. Hope I have explained this sufficiently and everything makes sense. Any ideas, as always, most appreciated.

Thanks, Mike

   Console.WriteLine("")
    Console.Write("Enter assessment to be deleted : ")
    Dim assessment_name As String = Console.ReadLine

    If assessment_name = "Maths" Then
        query = ("DELETE FROM Marks WHERE Assessment='Maths' AND Student_ID=" & id)
    ElseIf assessment_name = "English" Then
        query = ("DELETE FROM Marks WHERE Assessment='English' AND Student_ID=" & id)
    ElseIf assessment_name = "Science" Then
        query = ("DELETE FROM Marks WHERE Assessment='Science' AND Student_ID=" & id)
    ElseIf assessment_name = "Art" Then
        query = ("DELETE FROM Marks WHERE Assessment='Art' AND Student_ID=" & id)
    End If

    command = New SqlCommand(query, connection)
    command.ExecuteNonQuery()

I think you could do something like below.

You can use parameters to easily create your query.

You can use a USING block for easy dispose

Public Sub DeleteNote(ByVal Assessement As String, ByVal Studentid As Integer)
    'Using statement for easy dispose
    Using connection As New SqlConnection(connectionString), _
          command As New SqlCommand("DELETE FROM Marks WHERE Assessment=@Assessement AND Student_ID=@Studentid", connection)

        'Add parameters
        command.Parameters.AddWithValue("@Assessement", Assessement)
        command.Parameters.AddWithValue("@Studentid", Studentid)

        'Open the connection
        connection.Open()
        'Execute query
        Using reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
                'Read result of query
                Console.WriteLine(String.Format("{0}, {1}",
                     reader(0), reader(1)))
            End While
        End Using
    End Using
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM