I have built an application that uses SQL Server Compact 4.0 (.sdf) for data. The user should then be able to save and load .sdf files into the application. I've been able to build an method that creates an empty .sdf file. but my App.Config has an connectionstring to a predefined .sdf (Entity Framework). If the user should be able to load an .sdf file I need to set the connectionstring of the app.config file to work against the newly created .sdf-file.
How do I dynamicly set the Entity Framework to work against my new .sdf file?
This is my code for the class that creates the sdf so far. after this the user should be able to choose what file to work against. How do I set this up?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlServerCe;
using System.Configuration;
using GLL.Properties;
namespace GLL
{
public partial class LoadDatabaseForm : Form
{
public LoadDatabaseForm()
{
InitializeComponent();
}
private void buttonNew_Click(object sender, EventArgs e)
{
createDatabase();
setConnectionString();
}
private void setConnectionString()
{
ConnectionStringSettings str = System.Configuration.ConfigurationManager.ConnectionStrings["GLLDBEntities"];
MessageBox.Show(str.ConnectionString);
}
private void createDatabase()
{
string connStr = "Data Source = FooDatabase.sdf; Password = SomePassword";
if (File.Exists("FooDatabase.sdf"))
File.Delete("FooDatabase.sdf");
SqlCeEngine engine = new SqlCeEngine(connStr);
engine.CreateDatabase();
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection(connStr);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "CREATE TABLE FooTable(col1 int, col2 ntext)";
cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
conn.Close();
}
}
}
}