Link to home
Start Free TrialLog in
Avatar of searchei
searchei

asked on

How to persist inserted records in an .MDF file? Inserted records dissappear.

How do you persist data in an .MDF file?  I created an app that has a data source to an sql .MDF file, but I am not using the server (kind of like using an access database file).  I execute this code with no error, but when I close the app and open it again, the data is not in the table.  Why would this be?

My test code is like this

            SqlConnection myConn = new SqlConnection(s.ConnectionString);
            myConn.Open();

            for (int i = 0; i < 1000; i++)
            {
                string SQL = "INSERT INTO TEST_TABLE (FIELD1, FIELD2, FIELD3)" +
                             " VALUES ('TestData1'," +
                             "'TestData3'," +
                             "'TestData4')";

                SqlCommand ins = new SqlCommand(SQL, myConn);
                ins.ExecuteNonQuery();
            }
            myConn.Close();
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

Reading / Writing the .MDF file directly is not supported.

I don't know why no error was raised and I don't know how you're reading the file to verify what's in it.  But if you're going to use SQL Server, you need to talk to the server!

With Access, you're not doing direct file reading/writing either.  But to get the database stuff (Jet, I think it still is) to know what database you talk to, you tell it the file.  You're still using the installation of the DBMS.
ASKER CERTIFIED SOLUTION
Avatar of davecorun
davecorun

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of davecorun
davecorun

Oh, to check this, right-click the MDF file in Visual Studio, and select "Properties".  This option is in the Properties window.