0% found this document useful (0 votes)
28 views2 pages

Using Using Namespace Class: Program Sqliteconnection

This document contains C# code that demonstrates how to work with a SQLite database. It shows how to create a database connection, define a table with columns for name and score, insert sample data into the table, and print the table contents sorted by score. The code connects to an SQLite database file, creates a "highscores" table, inserts three name and score records, then retrieves and prints the sorted data to the console.

Uploaded by

Ion Taban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Using Using Namespace Class: Program Sqliteconnection

This document contains C# code that demonstrates how to work with a SQLite database. It shows how to create a database connection, define a table with columns for name and score, insert sample data into the table, and print the table contents sorted by score. The code connects to an SQLite database file, creates a "highscores" table, inserts three name and score records, then retrieves and prints the sorted data to the console.

Uploaded by

Ion Taban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Author: Tigran Gasparian

// This sample is part Part One of the 'Getting Started with SQLite in C#' tutorial at
http://www.blog.tigrangasparian.com/

using System;
using System.Data.SQLite;

namespace SQLiteSamples
{
class Program
{
// Holds our connection with the database
SQLiteConnection m_dbConnection;

static void Main(string[] args)


{
Program p = new Program();
}

public Program()
{
createNewDatabase();
connectToDatabase();
createTable();
fillTable();
printHighscores();
}

// Creates an empty database file


void createNewDatabase()
{
SQLiteConnection.CreateFile("MyDatabase.sqlite");
}

// Creates a connection with our database file.


void connectToDatabase()
{
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
}

// Creates a table named 'highscores' with two columns: name (a string of max 20 characters)
and score (an int)
void createTable()
{
string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}

// Inserts some values in the highscores table.


// As you can see, there is quite some duplicate code here, we'll solve this in part two.
void fillTable()
{
string sql = "insert into highscores (name, score) values ('Me', 3000)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into highscores (name, score) values ('Myself', 6000)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into highscores (name, score) values ('And I', 9001)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}

// Writes the highscores to the console sorted on score in descending order.


void printHighscores()
{
string sql = "select * from highscores order by score desc";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);
Console.ReadLine();
}
}
}

You might also like