4

I am trying to connect to SQL Server Express locally using VB.NET 2005. I pulled my connection string directly from the app.config file. When I run, I get NO errors and the connection states returns open, however the commands are not being processed.

Imports System.Data
Imports System.Data.SqlClient

Public Class frmAddMovie
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click    
    Dim conString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True;User Instance=True;"
    Dim con As New SqlConnection(conString)
    Dim cmd As New SqlCommand("Insert Into tblMovies(fldTitle, fldDirector, fldRating)Values('Solar Babies', 'PG', 'Rick Flair')", con)

    Using con
       con.Open()
       cmd.ExecuteNonQuery()
    End Using

    If MessageBox.Show("Movie Added") = Windows.Forms.DialogResult.OK Then
        Me.Close()
    End If

End Sub
End Class
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
  • 2
    Are you **100% sure** the commands don't get executed?? With an attached MDF, you just might be looking at the wrong database.... try this: set a breakpoint after the `Me.Close()` in your code; once you hit that breakpoint, inspect the database - I would almost bet the changes are there - if you're looking at the correct database file! – marc_s Oct 17 '11 at 04:44
  • It's probably not that important, but your insert will write "PG" to the "fldDirector" column and "Rick Flair" into the "fldRating" column. There's a small possibility that this is causing the problem. – Ortund Nov 23 '11 at 11:34
  • What is WRONG with your table names and column names? Its a table NO NEED TO HAVE `tbl` in its name. – Jeremy Dec 30 '11 at 13:03

2 Answers2

1

Are you running this inside of a virtual machine?

I run on Snow Leopard and have to access VS2010 from time to time from my mac. I have had similar issues on a VM but when using the same code otherwise it runs perfectly.

I am not exactly sure if that is the issue, but the solution is to try it on a windows-based PC. If it works, then at least you know it is Virtual Machine-related.

Pandagrrl
  • 106
  • 2
  • 6
  • yeah when I copied it into a windows-based computer it ran fine. I was running Parallels on my Mac OS X but I didn't think that was relevant since I couldn't find anything on google. Thanks! – Carrie Kendall Feb 21 '12 at 20:03
0

AttachDbFilename=|DataDirectory is in the bin/debug folder and by default your is recopyed each time you run your project, so what you a have inserted will be overwritten.

The solution is simply: 1 - Connect directly to main data

or

2 - In your project select the Movies.mdf, hit F4 Set Copy to output directory to 'Do not copy'

Amen Ayach
  • 4,288
  • 1
  • 23
  • 23