Is anyone using System.Data.SQLite within SharpDevelop?

老子叫甜甜 提交于 2019-12-06 09:29:16

After googling a lot and mixing a variety of sources and approaches I've found a way to acomplish this. Here's a snippet of most significant code:

/// <remarks>
/// Creating a DataSet to feed the DataGridView
/// </remarks>          
// 
DataSet results = new DataSet();
try
{
    /// <remarks>
    /// Setting the path where the database file is located
    /// </remarks>
    string database = "X:\\path\\to\\database\\file\\books.db";
    /// <remarks>
    /// Creating a ConnectionString pointing to the database file
    /// </remarks>
    SQLiteConnectionStringBuilder datasource = new SQLiteConnectionStringBuilder();
    datasource.Add("Data Source", database);
    datasource.Add("Version", "3");
    datasource.Add("New", "False");
    datasource.Add("Compress", "True");             
    /// <remarks>
    /// Starting the connection and sending the query
    /// </remarks>              
    using (SQLiteConnection connection = new SQLiteConnection(datasource.ConnectionString))
    {
        using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(queryTextBox.Text, connection))
        {
            /// <remarks>
            /// Populating the DataGridView
            /// </remarks>
            adapter.Fill(results);
            resultsDataGridView.DataSource = results.Tables[0].DefaultView;
        }
    }
}
catch (Exception error)
{
    MessageBox.Show("Exception caught: " + error.Message);
}

Where resultsDataGridView has been created with the IDE and queryTextBox is a TextBox element containing the SQL statement.

Don't forget to add a reference to System.Data.SQLite.dll and its corresponding using directive.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!