SQLite.Net使用入门(一),sqlite.net使用入门
逆境是成长必经的过程,能勇于接受逆境的人,生命就会日渐的茁壮。
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();
}
//创建一个两列的名为“highscores” 表:name(最多20个字符的字符串) score(一个int)
void createTable()
{
string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
//插入一些值到highscores表中
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();
}
}
运行结果如图:
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。