C# Sample: ScimoreDB Embedded in Windows Application - Get Started in 3 minutes in
Using ScimoreDB is easy. It takes 3 minutes to have a fully featured powerfull SQL server inside your application. Below is a sample showing how to use ScimoreDB Embedded:
In this "quick start" the database will be started as a child process of our sample program.
Download ScimoreDB
Download from ScimoreDB.3.0.msi and install.
Add references
Inside your application, add a reference to:
- Scimore.Data.ScimoreClient
- Scimore.Data.ScimoreClientNative
- Scimore.Data.ScimoreEmbeddedDBNative
Add using
For simplicity, add the following "using" statement.
using Scimore.Data.ScimoreClient;
The ScimoreDB Embedded is now ready to be used in your program!
Working with the database
You now need to create your database. Usually one start up the manager, and create an embedded database. But for the sake of demonstration it's here done using C# code. Note that at any point, while your program us running and is using your database - you can connect to the database with the manager, and do the things you use to do with a database manager.
using System;
using Scimore.Data.ScimoreClient;
namespace GetStartedIn3Minutes {
class Program {
///
/// Scimore DB Introduction sample.
///
/// Do the following:
/// 1) Create database instance using Scimore.Data.ScimoreClient.ScimoreEmbedded class
/// 2) Connect to the database instance
/// 3) Create a database
/// 4) Create a tabel
/// 5) And finally insert some data
/// 6) Shut down database
///
static void Main()
{
// 1) Create handle to out of process embedded database instance
string dbInstanceName =
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
"\\GetStartedIn3Minutes";
// If using ScimoreDB Embedded in a web application use:
// string dbInstanceName = AppDomain.CurrentDomain.BaseDirectory + "App_Data\\GetStartedIn3Minutes";
ScimoreEmbedded em = new ScimoreEmbedded();
em.Create(dbInstanceName);
em.Open(dbInstanceName);
// 2) Connect to our embedded database using standard .NET provider
using( ScimoreConnection cn = em.CreateConnection())
{
// Connect to the database instance
cn.Open();
// 3) Create a database on our database instance
ScimoreCommand cmd = new ScimoreCommand("create database sampledatabase", cn);
cmd.ExecuteNonQuery();
// 4) Create sample table
cmd = new ScimoreCommand(
@"use sampledatabase;
create table sampletable ([id] int not null, [text] nvarchar)", cn);
cmd.ExecuteNonQuery();
// 5) Insert sample row
cmd = new ScimoreCommand(@"insert into sampletable ([id], [text]) values (@id, @text);", cn);
cmd.Parameters.Add("id", ScimoreDbType.Int).Value = 4;
cmd.Parameters.Add("text", ScimoreDbType.NVarChar).Value = "some sample text data";
cmd.ExecuteNonQuery();
}
// 6) Shutdown embedded database process. If we don't, the db will automatically shut down 1 minute
// after last connection to it is lost
em.Shutdown();
}
}
}
The file structure
After running the above application, a database with the name
GetStartedIn3Minutes. The resulting database is a folder containing the files needed for the database. So everytime you refer to a database, you actually need to refer to the "database folder".
You can move the folder around, the database will still be valid. If you shut down the database, you can make a backup of the database, by simply copying the folder.
Accessing the database with ScimoreDB Manager.
It is possible to access the database using the ScimoreDB manager, simultaniously as you program is accessing the database.
First open the ScimoreDB manager. By right clicking on the embedded folder, and choose open you can open "WinForms1".
It is located as a directory under our binary. In our case the "bin/debug" folder.
Your can now work with the database from the manager.