Connecting To SQLExpress From a VB Console Application

I've been reading through the forums and I have seen that I am not the only person to have trouble simply trying to connect to a database from VB.

Firstly I have created a console application, and from within that application I have created a simple 1 table database and added two rows of data. So I now the database is there and working.

Problem is I keep getting the following error which trying to apply a connection string to an SQLConnection object:

Message: "ExecuteReader requires an open and available Connection. The connection's current state is closed."

The code I am using is as follows:

strSQLConnection = "Data Source=.\SQLEXPRESS;AttachDbFilename="
+ Chr(34) + _
   My.Computer.FileSystem.CurrentDirectory.ToString() + "\MusicManager.mdf"
+ Chr(34) + _
   ";Integrated Security=True;User Instance=True"

Try
   
conn.ConnectionString = strSQLConnection
   ...
   ...


I know the file exists and is in the right location. Why won't SQL Express open the database

I have successfully created Windows Applications that can connect to a database. What am I doing wrong

Cheers,

Roy



Answer this question

Connecting To SQLExpress From a VB Console Application

  • JohnsonZhang

    I can't count the number of times I've forgotten to Open the DB connection or tell the DataReader to Read.

    Uriel


  • CHowell

    Do I feel stupid or what...

    That was it. Thanks.

    How I missed that I do not know...

    Roy

  • victoriak68

    Did you call Open() on the connection object before trying to execute a command against it

    Pablo Castro
    Program Manager - ADO.NET Team
    Microsoft Corp.

  • Connecting To SQLExpress From a VB Console Application