Objective Coder
Objective Coder

Reputation: 557

Set connectionstring Entity Framework

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();
        }
        }
    }
}

Upvotes: 0

Views: 1955

Answers (2)

dotnetstep
dotnetstep

Reputation: 17485

You have to use entityconnectionbuilder.

http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder.aspx

Note : You new database must have same schema as old one

Opening an SQL CE file at runtime with Entity Framework 4 ( This will help you)

Upvotes: 0

Algorhythm
Algorhythm

Reputation: 570

Ok, so what I am hearing is you need to be able to set a connection string dyamicly in the App.config file. Good thing is that it is a key value pair. Here is an example of how to change a value in the App.Config:

// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);    
// Add an Application Setting.
config.AppSettings.Settings.Remove("ConnectionString");
config.AppSettings.Settings.Add("ConnectionString", "Data Source = FooDatabase.sdf; Password = SomePassword" );
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");

This is just an example. Here is more information on how to do this: http://chiragrdarji.wordpress.com/2008/09/25/how-to-change-appconfig-file-run-time-using-c/

Upvotes: 1

Related Questions