0

I have a project set up with 3 separate tables. I used the built-in designer from Visual Studio.

The tables contain

  1. Students
  2. Courses
  3. Tests in each course

All the tables share a studentnumber.

I have no problem getting and accessing the data true different tableadapters and corresponding bindingsources automatically generated by Visual Studio when I "drag and drop" them on the forms. I also am able to use the query builder to get the data I need from each table.

My problem occurs when I try to access the database manually via connection string.

I get this error:

Cannot attach file local path\HovedDatabase.mdf as database Outcomes because the file is already in use for database local path\HovedDatabase.mdf

This is the code I have:

Private Sub FormPrøver3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO:   This line of code loads data into the 'StudentDataSet.StudentTabell' table. You can move, or remove it, as needed.
    Me.StudentTabellTableAdapter.Fill(Me.StudentDataSet.StudentTabell)
    Me.StudentTabellTableAdapter.Connection.Close()

    Get_Data_Manualy_Fag()

End Sub

Private Sub Get_Data_Manualy_Fag()
    Try
        'Create variabals for inputcontainer
         Dim Studnr As String = Me.StudentnrLabel1.Text

        'Create a connection to the DataBase
        Dim myConn As SqlConnection
        myConn = New SqlConnection("Initial Catalog=OutComes;Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\HovedDatabase.mdf;Integrated Security=True")

        'Create a Command object.
        Dim myCmd As SqlCommand
        myCmd = myConn.CreateCommand
        myCmd.CommandText = "SELECT Fag FROM Fag3Tabell WHERE Fag = @Studnr"
        myCmd.Parameters.AddWithValue("@Studnr", Studnr)

        'Open the connection.
        myConn.Open()

        'Process the answer
        Dim myreader As SqlDataReader
        myreader = myCmd.ExecuteReader

        'Traverse the DataSet and Display :
        Dim i As Integer = 0
        Do While myreader.Read()
            Me.ComboBox1.Items.Add(myreader.GetString(i))
            i = i + 1
        Loop

        'Close the connection
        myConn.Close()

        'Handle errors
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

I set up a similar project with a similar LocalSQL database. I have done nothing in Visual Studio other than creating the database with the same tables.

Here I have no problem connecting to the database via manual connection string.

My question is: why can't I access the local DB with a connection string in the first project?

1 Answer 1

1

You haven't posted your other connection string, but it looks like AttachDbFilename may be the problem. The DB will already be attached, this option is only meant to be used in single-user mode (one connection only). Best bet is to permanently attach the DB to the LocalDB instance if that is what you are using the whole time.

Sign up to request clarification or add additional context in comments.

1 Comment

The second connectionstring was exactly the same as the first with a differen filenam. Just created a replica. Thank you for clarifying this to me. I thought it might be something like that.

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.