数据库
首页 > 数据库> > C# sqlite操作方法

C# sqlite操作方法

作者:互联网

//新建一个数据库文件

SQLiteConnection.CreateFile("MyDatabase.sqlite");


//创建一个连接到指定数据库
SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
//在指定数据库中创建一个table
string sqltable = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sqltable, m_dbConnection);
 command.ExecuteNonQuery();

//插入一些数据
for (int n = 1000; n < 1500; n = n + 100)
{
string sql = "insert into highscores (name, score) values ('hua',"+n+")";
SQLiteCommand commandsave = new SQLiteCommand(sql, m_dbConnection);
commandsave.ExecuteNonQuery();
}
//string sqla = "insert into highscores (name, score) values ('Myself', 6000)";
//SQLiteCommand command1 = new SQLiteCommand(sqla, m_dbConnection);
//command1.ExecuteNonQuery();

//添加新字段(列)
SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
string sqltable = "ALTER TABLE 'highscores' ADD '地址' text";//添加新字段(列)
SQLiteCommand command = new SQLiteCommand(sqltable, m_dbConnection);
command.ExecuteNonQuery();
MessageBox.Show("添加完成");

//查询结果按score的大小排序
SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
string sql = "select * from highscores order by score desc"; //查询结果按score的大小排序
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
textBox1.Text= textBox1.Text+"Name: " + reader["name"] + "\tScore: " + reader["score"]+"\r\n";

 

标签:sqlite,dbConnection,C#,操作方法,SQLiteCommand,command,score,SQLiteConnection,new
来源: https://www.cnblogs.com/NB571/p/14450958.html