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
Re: I am not able to fix this error VB 2017 Net SQLEXpress.
Which line throws the exception?
Re: I am not able to fix this error VB 2017 Net SQLEXpress.
Seriously?
Quote:
Dim connectionString As String = "Data Source=Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;"
https://www.connectionstrings.com/sql-server/
Re: I am not able to fix this error VB 2017 Net SQLEXpress.
Re: I am not able to fix this error VB 2017 Net SQLEXpress.
I seriously doubt this:
Quote:
My string is good because it works in another program