English 中文(简体)
Writing a C# desktop application that needs to embed an encrypted database. What type of database should I use?
原标题:

Hey SO ers, I m making a C#/WPF app that needs to access a number of tables to generate some of the xaml code I will use in the app. The tables will also contain some floating point numerical data as well.

The program is single-user, so the rdbms doesn t have to be very fancy, but the file does need to be encrypted as it will be "shrink-wrapped" and sent. I also can t force a user to install a separate program for this.

So far I ve been leaning towards using an encrypted .accdb database, but am open to better options. Does using accdb require the user to have access installed?

(NOTE: I have also been looking at Database engine for a desktop application in C# and )

问题回答

SQL Server Compact Edition and SQL Lite appear to be your main two options. SQL Server Compact has better .NET integration and Date/Time handling so I would go with that one. Also, the redistributable files are fairly low-weight (2-3MB IIRC).

SQL Server Compact will allow you to encrypt the database:

http://technet.microsoft.com/en-us/library/ms172901.aspx

...But you will have to be careful with your connection string because it is trivial to obtain an unencrypted password embedded in the client.

One last note: Selecting SQL Server Compact will enable you to use the built-in LINQ provider, which has the potential to make your life a lot easier.

Have you looked into SQL Server Compact?

SQL Server Compact 3.5 is a free, easy-to-use embedded database engine that lets developers build robust Windows Desktop and mobile applications that run on all Windows platforms including Windows XP, Vista, Pocket PC, and Smartphone.

"I also can t force a user to install a separate program for this." - you can distribute the required files with your application.

Another solution is to use an encrypted xml file. The Dataset can very easily be saved and loaded using an xml file:

This answer TOTALLY depends on your having a relatively small dataset, so consider it accordingly...

Have you considered keeping your objects in a collection and then serializing them to disk?

You can use Linq to Objects to query them in much the same way you would a database including joins, etc.

I have used this technique with a few personal projects and it works great, IF you have a small dataset.

An example is a custom membership provider I wrote for ASP.Net that uses this method if you don t have a database available (think really cheap hosted environment).

I ve tested it with loads as great as 10,000 users with basically instantaneous responses (it is coming from memory after all). When I get up to 100,000 users it begins to degrade a bit and by 500,000 it is a total dog that is unusable.

You could implement your own encrypted serialize/deserialize methods.

There is a cryptographic app walkthrough at http://msdn.microsoft.com/en-us/library/bb397867.aspx

Hope this helps.

From your brief synopsys of how you will be using this and the single user nature of it maybe an RDBMS will be overkill, you could possibly just store your small amount of data in a text/xml file and embed it as a resource in your exe (encrypted if you need to)

jeffa00 pointed out that this can be done by simple serialization if your data set isn t very large. Here is a complete working implementation to show how easy it is:

public class DataSerializer<T>
{
  static XmlSerializer _serializer = new XmlSerializer(typeof(T));
  static SymmetricAlgorithm _encryptionAlgorithm;

  public static void SetEncryptionKey(byte[] key)
  {
    _encryptionAlgorithm = Aes.Create();
    _encryptionAlgorithm.Key = key;
  }

  public static void WriteData(string filePath, T data)
  {
    using(var fileStream = File.OpenWrite(filePath))
    {
      var cryptoStream = new CryptoStream(fileStream, _encryptionAlgorithm, CryptoStreamMode.Write);
      _serializer.Serialize(cryptoStream, data);
      cryptoStream.Flush();
    }
  }

  public static T ReadData(string filePath)
  {
    using(var fileStream = File.OpenRead(filePath))
    {
      var cryptoStream = new CryptoStream(fileStream, _encryptionAlgorithm, CryptoStreamMode.Read);
      return (T)_serializer.Deserialize(Stream);
    }
  }
}

With this you can save an entire tree of objects to disk in encrypted form by simply doing:

DataSerializer<MyObjectType>.WriteData(@"c:somewheresomething.data", myObject);

and read it back with:

myObject = DataSerializer<MyObjectType>.ReadData(@"c:somewheresomething.data");

This is much nicer than working with SQL Server Compact or even full SQL Server, since LINQ to Objects is so much more powerful than LINQ to SQL. Faster too in many cases.

But this only works if your dataset easily fits in RAM.

sqlite

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is in the public domain.

sqlite.phxsoftware.com System.Data.SQLite is the original SQLite database engine and a complete ADO.NET 2.0/3.5 provider all rolled into a single mixed mode assembly. It is a complete drop-in replacement for the original sqlite3.dll (you can even rename it to sqlite3.dll if you re using it natively).

the list of avilable wrappers for c# here: http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers under C# wrappers section

I m using the one here: Simple C# Wrapper for SQLite which is basic but enough for my needs

Answer can depends on more details; yet C#SQLite (http://code.google.com/p/csharp-sqlite/) could be an option. If your database will have less that 4GB data then SQL Server Compact is a perfect candidate; no installation, no configuration, xcopy deploy.

VistaDB supports xcopy (easy deployment) with full encryption, on a database or table level.

Also check out Db4o, which is an open source object database. They support encryption of the object database as well as linq which makes querying the data a breeze.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签