SQL Express not updating database

I recently converted a program I made in C# from Access to Sql Server Express and I noticed something odd.

Anytime I update or insert data into the database, it only sticks around until the program closes. I can insert new records but as soon as the program quits, the inserts are gone.

Is this a bug or is there something else I need to do


Answer this question

SQL Express not updating database

  • Niran

    A couple other questions:

    • Are you "running" your program using F5
    • Is your database (mdf) file embedded in your application
    • What are the Copy File settings for the database if it's embedded

    A common mistake that people make when debugging applications in VS is setting the Copy File for the database to Always and then running their program by hitting F5. Doing this causes VS to put a new copy of the database into the Debug directory from the Project directory everytime you hit F5, which makes it look like your data is not being saved.

    The "design time" database that you see in your project is not the actual database that is being used when you Debug (F5) your application, that database is actually located in the Debug directory. (And the retail version of your applications will have a similar relationship, only in the Retail directory.) I typically suggest only copying the database file when it's newer.

    If this issue is in a deployed app that you're running from a .exe, then Jens is likely correct that you're not closing a transaction. (Or possible you're not writting data from your DataSet back into the database, if you're using DataSets that is.)

    Regards,

    Mike Wachal
    SQL Express team

    ----
    Check out my tips for getting your answer faster and how to ask a good question: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=307712&SiteID=1



  • xabrooklyn

    I am designing my first database (Visual Basic & SQL Server Express 2005) and it appears as if the database isn't updating. Refering to Mike's response above, I am running it by hitting F5, closing by hitting the "X" and then hitting F5 again to check if the changes have stuck. They didn't. These are the instructions given in the tutorial.

    Any ideas Thanks.

    In particular I have tried two step-by-step tutorials distributed by Microsoft: 1. Absolute Beginner's Video Series, Lesson09; and 2. the Help tutorial: Managing Your Records (ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_vbcnexpress/html/1ccdb8f1-4162-4a54-af17-231007eb529b.htm)

    The code for the form is:

    Public Class Form1

    Private Sub AddressesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddressesBindingNavigatorSaveItem.Click

    Me.Validate()

    Me.AddressesBindingSource.EndEdit()

    Me.AddressesTableAdapter.Update(Me.FirstDatabaseDataSet.Addresses)

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Me.AddressesBindingSource.EndEdit()

    Me.AddressesTableAdapter.Update(Me.FirstDatabaseDataSet.Addresses)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'TODO: This line of code loads data into the 'FirstDatabaseDataSet.Addresses' table. You can move, or remove it, as needed.

    Me.AddressesTableAdapter.Fill(Me.FirstDatabaseDataSet.Addresses)

    End Sub

    End Class


  • Howard555

    Hi,

    seems that you open a transaction and didn’t commit it. Did you try to check the following: Make chages to your data within your application, meanwhile query the database if the data changes have been reflected on the server. If not you are sticking in an open transaction and you have to close / commit it first.

    HTH, jens Suessmeyer.

    ---
    http://www.sqlserver2005.de
    ---


  • Joshua_10002

    I am glad you clarified this as I am up all night trying to figure this out. Is there a simple way of just having both instances point to one database Furthermore, if the database gets large, having two instances of your db, one in the projects folder and one in the bin folder takes up too much space. Any suggestions

  • chawacho

    One solution (but not perfect)

    in the "Getting the Information You Need: Connecting to an Existing Database " part of the help tutorial, ( ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_vbcnexpress/html/c8148fa5-f671-4f8f-b32e-e9016b76223d.htm) where it says:

    "10. Click OK to close the dialog box, and then in the Data Source Configuration Wizard, click Next. If prompted to copy the data file to your project, click Yes."

    instead of clicking yes, click No and it will use the database you created in the FirstDatabase project, and it WILL update it.

    This may help (or not): ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_vsnav/html/3ffa1aa9-17e4-422c-a02f-09224828cdfc.htm


  • SQL Express not updating database