0
    Function SQLSelect() As ActionResult

    Dim theconnection As New SqlConnection("Data Source=(localdb);Database=test_drive_database;")
    Dim queryString As String = "SELECT * FROM ColorTable"
    Dim command As New SqlCommand(queryString)
    command.CommandTimeout = 15
    command.CommandType = CommandType.Text

    'Printing Out the SQL Result

    Return ViewData("command")

End Function

I am having an empty SQL result, with no error messages.

The URL to invoke the above function is: http://localhost:1812/Home/SQLSelect

The question is, is there a way to specifically check if the SQL database has gotten opened?

The Tools Used: Visual Studio 2012, VB.NET MVC 4, Microsoft SQL Server Compact 4.0

Notice: The database name is test_drive_database, and there is no password set for the database.

3 Answers 3

2

It seems that there are some errors in your code

If you are connecting to a Sql Server Compact then connection string should be something like this

  Data Source=MyData.sdf;Persist Security Info=False;

see ConnectionStrings

Then the objects to work with are SqlCeConnection and SqlCeCommand from the namespace

  System.Data.SqlServerCe 

Two more problems are: you don't open the connection and don't associate the connection to the command

  theconnection.Open
  command.Connection = theconnection 
Sign up to request clarification or add additional context in comments.

Comments

1

As Steve said, you need to invoke the Open method of the SQLConnection object to open the connection.

Once you've done that, you can use the State property of the SQLConnection object to check the status of the connection at any time.

    Dim sConn As New SqlConnection(connectionString)
    If sConn.State = ConnectionState.Open Then
        Debug.Print("Your database connection is open")
    ElseIf sConn.State = ConnsectionState.Closed Then
        Debug.Print("Your database connection is closed")
    Else
        Debug.Print("Your database connection state: " & sConn.State.ToString)
    End If

Comments

0

You can try
If (theconnection.State = ConnectionState.Open) Then
//your connection is open

Consider not only the Open or Closed, but also the Connecting, Broken, Executing and Fetching options.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.