Click here to Skip to main content
15,860,844 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
The following code snippets demonstrates the common practice of reading data from SqlCe file-based Database stored on HDD/SSD (i.e. permanent PC data storage) into DataTable object (.NET, WPF/C# : for simplicity purpose it's using just plain SQL string, no parameters):

Listing 1.

C#
#region private static method: Read DB, return DataTable
/// <summary>
/// DB Read returning DataTable on SQL command text
/// </summary>
/// <param name="SQL">string</param>
/// <returns>DataTable</returns>
private static DataTable SqlCeReadTable(string cnString, string SQL)
{
    DataTable _dt;
    try
    {
        using (SqlCeConnection _cn = new SqlCeConnection(cnString))
        {
            using (SqlCeCommand _cmd = new SqlCeCommand(SQL, _cn))
            {
                _cmd.CommandType = CommandType.Text;
                _cn.Open();
                using (SqlCeDataReader _dr = _cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    _dt = new DataTable();
                    _dt.Load(_dr);
                    _dr.Close();
                }
            }
            _cn.Close();
            return _dt;
        }
    }
    catch { return null; }
}
#endregion


Question: Is it possible, and if so - How To to read (i.e. to return a populated DataTable object pertinent to the same SQL string) the in-memory SQLite ReadOnly Database (containing some reference data, non-modifiable) existing as EmbeddedResource in WPF/C# application (.NET 4.0 and up)? More specific: how to modify a connection string in provided code snippet to access (in Read-Only mode) the said Embedded SQLite database existing only in-memory?
Thanks and regards,
Posted

1 solution

"Data Source=:memory:;Version=3;New=True;"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900