get data from sqlite in csharp

Here's an example of how to retrieve data from an SQLite database using C#:

main.cs
using System.Data.SQLite;

// ...

// Connection string for SQLite database
string connectionString = "Data Source=/path/to/database.db;Version=3;";

// Open a connection to the SQLite database
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
    connection.Open();

    // Query to retrieve data from a table
    string query = "SELECT * FROM my_table;";

    // Create a command object
    using (SQLiteCommand command = new SQLiteCommand(query, connection))
    {
        // Execute the query and get a data reader
        using (SQLiteDataReader reader = command.ExecuteReader())
        {
            // Iterate over the data and display it
            while (reader.Read())
            {
                // Access data using the column names or indexes
                int id = reader.GetInt32(reader.GetOrdinal("id"));
                string name = reader.GetString(reader.GetOrdinal("name"));

                // ... do something with the data ...
            }
        }
    }
}
1018 chars
34 lines

In this example, we use the System.Data.SQLite namespace to open a connection to an SQLite database using a connection string. We then create a query string and a command object for the query, execute it using an ExecuteReader() method, and iterate over the returned data using a data reader. We can access the individual columns of each row using their column names or indexes, depending on our needs.

gistlibby LogSnag