//创建一个数据库文件
//string datasource = "d:/test.db";
//System.Data.SQLite.SQLiteConnection.CreateFile(datasource);
//连接数据库
SQLiteConnection conn = new SQLiteConnection();
SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder();
connstr.DataSource = "E:/test/test.db";
//connstr.Password = "admin";//设置密码,SQLite ADO.NET实现了数据库密码保护
conn.ConnectionString = connstr.ToString();
conn.Open();
//创建表
//SQLiteCommand cmd = new SQLiteCommand();
//string sql = "CREATE TABLE test(username varchar(20),password varchar(20))";
//cmd.CommandText = sql;
//cmd.Connection = conn;
//cmd.ExecuteNonQuery();
//插入数据
SQLiteCommand cmd = new SQLiteCommand();
string sql = "INSERT INTO userinfo VALUES(10, 'lcd', '123456')";
cmd.CommandText = sql;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
//取出数据
sql = "SELECT * FROM userinfo";
//select c.rowid from userinfo可以获取到数据库行id
cmd.CommandText = sql;
SQLiteDataReader reader = cmd.ExecuteReader();
String username = "";
String password = "";
while (reader.Read())
{
username = reader.GetString(1);
password = reader.GetString(2);
}
MessageBox.Show(username + ";" + password);