Check if the database exists to delete, yes or no.

manny cash

Member
Joined
Oct 19, 2024
Messages
12
Programming Experience
Beginner
This little program checks if the database exists to delete with the message box option, yes or no. When I debug it, the program has no errors, but when I run it, it doesn't do anything and has no inconsistent messages. I use VB 2017, Sql Database. I am asking for help "cause I am just learning visual basic without a teacher. I appreciate your help guys.
VB.NET:
Imports System.Data.SqlClient

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(340, 189)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(227, 93)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(800, 450)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents Button1 As Button

    'Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    'End Sub
    Public Shared Function CheckDatabaseExists(ByVal server As String,
                                           ByVal database As String) As Boolean

        Dim connString As String = ("Data Source=" _
                + (server + ";Initial Catalog=master;Integrated Security=True;"))

        Dim Appoinment As String = Nothing

        Dim cmdText As String =
   ("select * from master.dbo.sysdatabases where name=\’" + (Appoinment + "\’"))

        Dim bRet As Boolean = False

        Select Case MsgBox("Are you sure ?", MsgBoxStyle.YesNo, "Delete")
            Case MsgBoxResult.Yes
                ' Do something if yes

                Dim dropDatabaseQuery As String = "DROP DATABASE IF EXISTS Appoinment"

            Case MsgBoxResult.No
                ' Do something if no
                Try
                Catch ex As exception

                    Console.WriteLine("An error occurred: " & ex.Message)
                End Try
        End Select

        Using sqlConnection As SqlConnection = New SqlConnection(connString)
            sqlConnection.Open()

            Using sqlCmd As SqlCommand = New SqlCommand(cmdText, sqlConnection)
                Using reader As SqlDataReader = sqlCmd.ExecuteReader
                    bRet = reader.HasRows
                End Using
            End Using
        End Using

        Return bRet

    End Function

End Class
 
Last edited by a moderator:
I'm not sure you know what debugging is. Building the project is not debugging. You need to run the built project in the debugger. Given that you never actually call your CheckDatabaseExists method anywhere in that code, I'm not sure how you could think that you're debugging it. I would assume that you would want to call that method on the Click of a Button. Also, there's no good reason for that method to be Shared.
 
It's also worth noting that, even if you called that method, it checks whether the database exists but doesn't drop it. You have some SQL code to drop the database but you never execute it.

One thing I would recommend is that you don't write a bunch of code first and then try to test it all. Take things one step at a time. You should have confirmed that you were actually executing that method before you ever write any code in it. You should have set a breakpoint on the definition of the method and then run the project in the debugger to see if it was hit. You should have then made whatever changes were required with whatever help you needed to fix that before writing any code in the method. If you don't actually know how to debug with breakpoints, you should learn that first, but you could have still put a message box to see if it was displayed.
 
Back
Top