I found the code to migrate the tables and I tried just testing on one table. I also put in some msgboxes to see thru each step. I then changed the records in my Company info in my application. I then made some change in table so that there would be pre folder made. I then ran the application and it updated it. It didn't pick up that the file prefile exists and didn't continue with the migration and it overwrote my existing records that I had added in my application. I had to change things a little from his code because he used a .sdf type file and I'm using a .mdf type file? Also he uses a 'SqlServerCe' and I used '
SqlClient.SqlConnection'. But I don't think that the code is getting to the part where the new connections are made. Any Ideas what might be wrong here? The Build Action property is set to Content and the copy to output directory is set to copy if newer in the properties for BuilderEstimatorDatabase.mdf. Below is the code that I used.
Thanks
Imports
System.Windows.Forms
Imports
System.Deployment.Application
Imports
System.IO
Imports
BuildersEstimator_2.BuildersEstimatorDatabaseDataSetTableAdapters
Imports
System.Data.SqlClient.SqlConnection
Public
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
If ApplicationDeployment.IsNetworkDeployed Then
If ApplicationDeployment.CurrentDeployment.IsFirstRun Then
Migratedata()
End If
End If
End Sub
Private Sub Migratedata()
MsgBox(
"Checking if Data file to migrate")
Dim preFile As String = Path.Combine(ApplicationDeployment.CurrentDeployment.DataDirectory, ".\.pre\BuidlersEstimatorDatabase.mdf")
If Not File.Exists(preFile) Then
MsgBox(
"Nothing to migrate")
Return
' nothing to migrate
End If
' Get a connection to the old data in the \.pre folder
MsgBox(
"Migrating Data Files")
Dim oldDataConnectionString As String = "Data Source=|DataDirectory|\.pre\BuidlersEstimatorDatabase.mdf"
Dim oldConnection As New SqlClient.SqlConnection(oldDataConnectionString)
' Get a connection to the new data
Dim newDataConnectionString As String = "Data Source=|DataDirectory|\BuidlersEstimatorDatabase.mdf"
Dim newConnection As New SqlClient.SqlConnection(newDataConnectionString)
' Fill a dataset with the migration data
Dim oldData As New BuildersEstimatorDatabaseDataSet()
Dim oldAdapter As New CompanyInfoTableAdapter()
oldAdapter.Connection = oldConnection
oldAdapter.Fill(oldData.CompanyInfo)
' Create a table adapter for the new database and a compatible data set
Dim newAdapter As New CompanyInfoTableAdapter()
newAdapter.Connection = newConnection
Dim newData As New BuildersEstimatorDatabaseDataSet()
' Fill the new data set with default data
newAdapter.Fill(newData.CompanyInfo)
' Merge the old data into the new schema
' Assumes compatible schemas - this is the hard part for
' Real apps with significant schema changes
For Each row As DataRow In newData.CompanyInfo
row.Delete()
Next
newData.Merge(oldData)
newAdapter.Update(newData)
End Sub