1

I am just keeping up with WPF and C#. I have written a complete desktop application and it was Ok. I am now working on a database driven application (The database is a temporary storage, the application will sync with a live server). I have chosen MsSql Compact Edition as my application database but I seem not to find any tutorial that breaks down connecting, accessing and administering the database in a WPF program. I have a good background knowledge of database, RDBMS and ORM from the likes of PHP and MySql. Moving to Desktop/MS applications, I have the following questions.

  1. Creating the database (I can try this with VS 2012 but hints are welcome)
  2. Adding (bundling) it to the application.
  3. Performing a CRUD on it (A simple class or example)
  4. Possibility of using one database in two projects of the same solution
  5. Other things I should know, not leaving out available ORM (if they are available) and their respective documentations.

I will be glad if answers are related using a Database engine like MySql with PHP. I just need a breakdown.

Replies will be much appreciated.

Thanks in advance.

1 Answer 1

2

SqlCompact just creates a file (.sdf) on your local file system. You can create it on the fly and then delete it when your done.. or keep it around. Here's some code snippits I grabbed from a project I once did. It should help with the creation/CRUD questions.

using System.Data.SqlServerCe;

private SqlCeConnection InitializeDatabase()
{
    string connectionString = CreateDatabase();
    SqlCeConnection conn = new SqlCeConnection(connectionString);
    conn.Open();

    CreateTable(conn);

    return conn;
}

private string CreateDatabase()
{
    string dbPath = String.Format("{0}scanner.sdf", _rootPath);
    if (File.Exists(dbPath))
        File.Delete(dbPath);

    string connectionString = String.Format("DataSource=\"{0}\";Max Database Size=3000;", dbPath);
    SqlCeEngine en = new SqlCeEngine(connectionString);
    en.CreateDatabase();
    en.Dispose();

    return connectionString;
}

private void CreateTable(SqlCeConnection conn)
{
    using (SqlCeCommand comm = new SqlCeCommand())
    {
        comm.Connection = conn;
        comm.CommandType = CommandType.Text;
        comm.CommandText = "CREATE TABLE gnis ([Id] [int] IDENTITY(1,1) PRIMARY KEY, [Name] [nvarchar](110) NOT NULL, [Geometry] [varbinary](429) NOT NULL)";
        comm.ExecuteNonQuery();
    }
}

private void CreateTableIndex(SqlCeConnection conn)
{
    using (SqlCeCommand comm = new SqlCeCommand())
    {
        comm.Connection = conn;
        comm.CommandType = CommandType.Text;
        comm.CommandText = "CREATE INDEX IXgnis ON gnis ([Name]);";
        comm.ExecuteNonQuery();
    }
}

private void WriteFeature(SqlCeConnection conn, string name, MultiPoint multiPoint)
{
    byte[] wkb = WkbWriter.WriteWkb(multiPoint);
    using (SqlCeCommand comm = new SqlCeCommand())
    {
        comm.Connection = conn;
        comm.CommandType = CommandType.Text;
        comm.CommandText = "INSERT INTO gnis ([Name], [Geometry])  VALUES (@a, @b)";
        comm.Parameters.AddWithValue("@a", name);
        comm.Parameters.AddWithValue("@b", wkb);
        comm.ExecuteNonQuery();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Big thumbs up, Hers a good head start, I got another tutorial though, with this and that one, I am ready to go.
There are some variables that are not declared and there uses are not clear, I will appreciate if you post the list of the dependencies used. Also, please what is the use of the WkbWriter.WriteWkb(multiPoint); method and the MultiPoint namespace is unknown. Thanks
It's just code snippets.. not a full class. Just ignore the geometry stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.