SqlCeHelper - Native C++ SQL Helper class for SQL Server Compact

Project Description
The SqlCeHelper provides an easy, efficient and succinct way to manipulate SQL Server Compact using SQL clause from Native C++ applications.

Dependencies


Included Features

  • Create SqlCe database.
  • Delete SqlCe database.
  • DDL support
  • DML support
    • Insert, Update, Delete
    • Select, RecordSet

Supported SqlCe version

  1. SQL Server Compact 3.5

TO-DO-LIST

  • Support to open the database with password
  • Support to specify the Open Mode
  • Support to load Ole Db without COM registry.
  • Support Transactions
  • Support Parameters

How-to-Use

Interfaces

SqlCeHelper3.png

Insert, Update, Delete

    SqlCeHelper db;
    HRESULT hr = db.Open(DB_FILE_NAME);
    if(hr < 0)
    {
        wprintf(L"ERR:%S", db.GetErrorsMessage());
    }

    CString sqlStr;
    for(int i=0; i<max; ++i)
    {
        SYSTEMTIME currentTime;    
        GetLocalTime(&currentTime);
        sqlStr.Format(L"INSERT INTO T1 (F1, F2, F3) VALUES(%d, 'STR%d', '%d-%d-%d %d:%d:%d')", 
            i, i, currentTime.wYear, currentTime.wMonth, currentTime.wDay, currentTime.wHour, currentTime.wMinute, currentTime.wSecond);
        hr = db.ExecuteNonQuery(sqlStr);
        if(hr < 0)
        {
            wprintf(L"ERR:%S", db.GetErrorsMessage());
        }
    }

    db.Close();

Select

    SqlCeHelper db;
    HRESULT hr = db.Open(DB_FILE_NAME);
    if(hr < 0)
    {
        wprintf(L"ERR:%S", db.GetErrorsMessage());
    }
    
    CString sqlStr;
    sqlStr.Format(L"SELECT * FROM T1");
    hr = db.ExecuteReader(sqlStr);
    if(hr < 0)
    {
        wprintf(L"ERR:%S", db.GetErrorsMessage());
    }
    
    while(!db.IsEndOfRecordSet())
    {
        wprintf(L"F1=[%d], F2=[%s], F3=[%s]\n", db.GetRowInt(1), db.GetRowStr(2), db.GetRowStr(3));
        db.MoveNext();
    }
    db.CloseReader();
    db.Close();

Last edited Sep 15, 2009 at 11:45 AM by JakeLin, version 7