Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I run application error not display but stop on this line Sqlcon.Open()
Because app.Config not run properly therefore database not created. Generate script
File is "C:\Create\SQLScriptDetail.sql"


What I have tried:

Config file

<configuration>
  <appSettings>
    <add key="SQLSqriptRead" value="C:\Create\SQLScriptDetail.sql"/>
  </appSettings >
  <connectionStrings>
    <add name="RMS.My.MySettings.StupConnection" connectionString="Data Source=Computech-PC\SQLEXPRESS;Initial Catalog=SQLSqript;Integrated Security=true;User ID=sa;Password=" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Imports System.Data.SqlClient
Imports System.Configuration

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim SQLConnectionString As String = ConfigurationManager.ConnectionStrings("RMS.My.MySettings.StupConnection").ConnectionString
        System.Configuration.ConfigurationManager.AppSettings.Get("SQLSqriptRead")

        Using Sqlcon As New SqlConnection(SQLConnectionString)
            Sqlcon.Open()

            Dim Check_User_Name_Found As New SqlClient.SqlCommand("Select Max(Name) from SQLSqript.dbo.Script", Sqlcon)
            Dim Check_UserNameFound = Check_User_Name_Found.ExecuteScalar().ToString()

       End Using

    End Sub
End Class
Posted
Updated 19-Sep-18 1:02am
Comments
CHill60 19-Sep-18 6:29am    
Where is the code that runs the script to create the database?
Herman<T>.Instance 19-Sep-18 6:37am    
Seems he is trying to connect to a database before it is being created?

1 solution

At a guess - and I haven't got any access to your database so I can't check - you are trying to create a database in code that tries to connect to it. That won't work, not unless the DB already exists, and the error message would indicate it doesn't.

What you need to do is connect to the database engine without specifying a DB to use.
At the moment your connection string specifies a DB:
Data Source=Computech-PC\SQLEXPRESS;Initial Catalog=SQLSqript;Integrated Security=true;User ID=sa;Password='' providerName=System.Data.SqlClient"
                                    \_______________________/
                                              DB
Use String.Replace to remove that section and it should start to work, but you probably need to sort out the authorization - Integrated Security and a username and password is unusual, and you probably don't want the ProviderName section at all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900