我有一個小應用程序我正在使用SQL CE和EF,我在安裝應用程序(clickonce)時部署模板數據庫。然後,我在應用程序啓動時使用spash屏幕來加載以前創建的數據庫或讓用戶創建一個新的數據庫。
當他們創建一個新的數據庫時,我只需提示他們輸入一個位置並將模板數據庫複製到他們想要的位置並添加他們想要的名稱。我還將其設置爲使用已部署的數據庫,而不給他們提供具有多個數據庫文件的機會。
我們在這裏處理了幾件如下:
- SQL CE數據庫文件
我的股票/模板.sdf文件坐在我的項目文件夾,幷包含在範圍內的項目Visual Studio(我正在使用2015)。右擊在解決方案資源管理器,選擇屬性的文件要進行以下設置:
生成操作 - 內容
複製到輸出目錄 - 始終複製
- 創建全局可變
既可以使用現有的模塊文件或創建一個新的,看起來像這樣:
Public Module Globals
Friend g_recipeData As RecipeEntities
End Module
- 創建在Solution Explorer您的項目名稱設置爲最後一個文件路徑
右擊並選擇屬性。點擊設置標籤和加入新的設定如下:
名稱:lastpath
類型:String
範圍:用戶
值:
- 創建初始屏幕形式(frmSplash)
礦看起來像這樣:
是在表格上
控制如下:
txtFile
cmdSelectDatabase
cmdNew
cmdOpen
cmdExit
- 閃屏(frmSplash)代碼
Region「Form Methods」
Private Sub OnFormLoad() Handles Me.Load
txtFile.Text = My.Settings.lastpath
If txtFile.Text <> "" Then
cmdOpen.Enabled = True
cmdOpen.Select()
Else
cmdNew.Select()
End If
End Sub
Private Sub FileSelect()
Try
Dim openFileDialog As New OpenFileDialog()
openFileDialog.Filter = "sdf files (*.sdf)|*.sdf|All files (*.*)|*.*"
openFileDialog.FilterIndex = 1
openFileDialog.RestoreDirectory = True
Dim result As DialogResult = openFileDialog.ShowDialog(Me)
If result = DialogResult.Cancel Then
cmdSelectDatabase.Select()
Exit Sub
End If
txtFile.Text = openFileDialog.FileName
If txtFile.Text <> "" Then
cmdOpen.Enabled = True
cmdOpen.Select()
My.Settings.lastpath = openFileDialog.FileName
My.Settings.Save()
Else
cmdOpen.Enabled = False
cmdSelectDatabase.Select()
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "Application Error")
Application.Exit()
Finally
End Try
End Sub
Private Sub SetConnectionString()
Try
Dim providerName As String = "System.Data.SqlServerCe.4.0"
Dim datasource As String = txtFile.Text
Dim sqlCeBuilder As New SqlCeConnectionStringBuilder
sqlCeBuilder.DataSource = datasource
sqlCeBuilder.PersistSecurityInfo = True
g_SQLCeConnectionString = sqlCeBuilder.ConnectionString
Dim providerString As String = sqlCeBuilder.ToString()
Dim entityBuilder As New EntityConnectionStringBuilder()
entityBuilder.Provider = providerName
entityBuilder.ProviderConnectionString = providerString
entityBuilder.Metadata = "res://*/RecipeModel.csdl|res://*/RecipeModel.ssdl|res://*/RecipeModel.msl"
Dim c As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location)
Dim section As ConnectionStringsSection = DirectCast(c.GetSection("connectionStrings"), ConnectionStringsSection)
g_EntityConnectionString = entityBuilder.ConnectionString
section.ConnectionStrings("RecipeEntities").ConnectionString = g_EntityConnectionString
c.Save(ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("connectionStrings")
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "Application Error")
Application.Exit()
End Try
End Sub
Private Sub CreateDatabase()
Try
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Filter = "sdf files (*.sdf)|*.sdf"
saveFileDialog.Title = "Create Database"
saveFileDialog.FilterIndex = 1
If saveFileDialog.ShowDialog() = DialogResult.OK Then
File.Copy(Path.Combine(ApplicationDeployment.CurrentDeployment.DataDirectory, "rw.sdf"), saveFileDialog.FileName, True)
Dim strPathandFile As String = saveFileDialog.FileName
txtFile.Text = strPathandFile
My.Settings.lastpath = strPathandFile
My.Settings.Save()
cmdOpen.Enabled = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "Application Error")
Application.Exit()
End Try
End Sub
Private Sub LoadMainApplication()
Try
Dim objNewForm As New FrmMain
objNewForm.Show()
Me.Close()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "Application Error")
Application.Exit()
End Try
End Sub
端部區域
區域 「的事件處理程序」
Private Sub cmdSelectDatabase_Click(sender As Object,
e As EventArgs) Handles cmdSelectDatabase.Click
FileSelect()
cmdOpen.Select()
End Sub
Private Sub cmdCancel_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
Me.Close()
End Sub
Private Sub cmdOk_Click(sender As Object, e As EventArgs) Handles cmdOpen.Click
Me.Cursor = Cursors.WaitCursor
SetConnectionString()
LoadMainApplication()
Me.Cursor = Cursors.Default
End Sub
Private Sub txtFile_Validated(sender As Object, e As EventArgs) Handles txtFile.Validated
If txtFile.Text.Length = 0 Then
cmdOpen.Enabled = False
Else
cmdOpen.Enabled = True
End If
End Sub
Private Sub cmdNew_Click(sender As Object,
e As EventArgs) Handles cmdNew.Click
CreateDatabase()
SetConnectionString()
LoadMainApplication()
End Sub
Private Sub CatchEnterKey(ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txtFile.KeyPress, txtPassword.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
cmdOk_Click(sender, e)
e.Handled = True
Exit Sub
End If
End Sub
- 設置全局變量值
在主應用程序的形式(frmMain以上)將以下內容添加到構造函數中:
Public Sub New()
InitializeComponent()
g_recipeData = New RecipeEntities
End Sub
如果在模塊文件中創建變量時執行上述操作,則會設置實體的連接字符串,並且無法對其進行更改。您必須首先在app.config中設置連接字符串(使用上面的代碼),然後在實例化時實體將使用所需的連接字符串。
我認爲這是現在的基礎。我強烈建議您在完成後再讀一遍。我做了一些更正,甚至爲最後一個路徑設置添加了一個步驟。如果有什麼不工作或混亂,請打我,我會盡我所能提供幫助。祝你好運!
您可以使用SqlCeEngine.CreateDatabase創建一個空數據庫,並運行一個sql腳本來創建所需的對象。 – ErikEJ
嗨,erik!感謝您的所有偉大工作!我假設你推薦這個路徑,因爲EF不提供任何運行時數據庫生成?我的意思是說:有沒有辦法在運行時獲得用於數據庫創建的原始SQL,並將其用作所引用的「sql腳本」? –