前端之家收集整理的这篇文章主要介绍了
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();
}
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,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();
}
}
运行结果如图:
原文链接:https://www.f2er.com/sqlite/198716.html