-
Feb 13th, 2025, 11:55 AM
#1
Thread Starter
Hyperactive Member
I am not able to fix this error VB 2017 Net SQLEXpress.
This little Module Creates a new database, but before creating it, it checks if the database exists. There are two options (YesNo): delete or leave.
I have a problem in the connection My string is good because it works in another program it is a local database SQLExpress.
The Error message is this: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
I have tried for more than a week now, and I do not understand this error. All I know is the connection, but I do not know how to fix it. Thank you in advance if someone wants to help me.
Here is the code:
Module Module1
Sub Main()
Dim connectionString As String = "Data Source=Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;"
Dim databaseName As String = "Appoinment"
Dim dbExists As Boolean = False
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim cmd As New SqlCommand("SELECT database_id FROM sys.databases WHERE name = @dbName", connection)
cmd.Parameters.AddWithValue("@dbName", databaseName)
Dim result = cmd.ExecuteScalar()
If result IsNot Nothing Then
dbExists = True
End If
End Using
If dbExists Then
Dim result As DialogResult = MessageBox.Show("Database exists. Do you want to delete it?", "Confirm Delete", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim cmd As New SqlCommand("DROP DATABASE " & databaseName, connection)
cmd.ExecuteNonQuery()
MessageBox.Show("Database deleted successfully.")
End Using
End If
Else
MessageBox.Show("Database does not exist.")
End If
End Sub
End Module
-
Feb 13th, 2025, 02:02 PM
#2
Re: I am not able to fix this error VB 2017 Net SQLEXpress.
Which line throws the exception?
-
Feb 14th, 2025, 01:49 AM
#3
Re: I am not able to fix this error VB 2017 Net SQLEXpress.
Seriously?
Dim connectionString As String = " Data Source=Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;"
https://www.connectionstrings.com/sql-server/
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Feb 14th, 2025, 06:47 AM
#4
Thread Starter
Hyperactive Member
Re: I am not able to fix this error VB 2017 Net SQLEXpress.
-
Feb 14th, 2025, 07:47 AM
#5
Re: I am not able to fix this error VB 2017 Net SQLEXpress.
I seriously doubt this:
My string is good because it works in another program
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|