An attempt to attach an auto-named database for file failed in Vb.Net

Posted by user2454135 on Stack Overflow See other posts from Stack Overflow or by user2454135
Published on 2013-10-24T06:21:15Z Indexed on 2013/10/25 9:55 UTC
Read the original article Hit count: 124

I am Trying to connect database for first time , and I am getting this error :

An attempt to attach an auto-named database for file VBTestDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

and getting error on

myconnect.Open()

Heres my code :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myconnect As New SqlClient.SqlConnection
    myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=VBTestDB.mdf;Integrated Security=True;User Instance=True;"


    Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
    mycommand.Connection = myconnect
    mycommand.CommandText = "INSERT INTO Card (CardNo,Name) VALUES (@cardno,@name)"
    myconnect.Open()

    Try
        mycommand.Parameters.Add("@cardno", SqlDbType.Int).Value = TextBox1.Text
        mycommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = TextBox2.Text

        mycommand.ExecuteNonQuery()
        MsgBox("Success")
    Catch ex As System.Data.SqlClient.SqlException
        MsgBox(ex.Message)
    End Try
    myconnect.Close()
End Sub

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about vb.net