逆境是成长必经的过程,能勇于接受逆境的人,生命就会日渐的茁壮。
Program.cs主程序:
class Program
{
SQLiteConnection m_dbConnection;
static void Main(string [] args)
{
Program p = new Program();
}
public Program ()
{
createNewDatabase();
connectToDatabase();
createTable();
fillTable();
printHighscores();
}
void createNewDatabase()
{
SQLiteConnection.CreateFile("MyDatabase.sqlite" );
}
void connectToDatabase()
{
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;" );
m_dbConnection.Open();
}
void createTable()
{
string sql = "create table highscores (name varchar(20), score int)" ;
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
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();
}
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();
}
}
运行结果如图: