string datasource = "e:/tmp/test.db"
System.Data.SQLite.SQLiteConnection.CreateFile(datasource)
//连接数据库
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection()
System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder()
connstr.DataSource = datasource
connstr.Password = "admin"//设置密码,SQLite ADO.NET实现了数据库密码保护
conn.ConnectionString = connstr.ToString()
conn.Open()
//创建表
System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand()
string sql = "CREATE TABLE test(username varchar(20),password varchar(20))"
cmd.CommandText = sql
cmd.Connection = conn
cmd.ExecuteNonQuery()
//插入数据
sql = "INSERT INTO test VALUES('a','b')"
cmd.CommandText = sql
cmd.ExecuteNonQuery()
//取出数据
sql = "SELECT * FROM test"
cmd.CommandText = sql
System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader()
StringBuilder sb = new StringBuilder()
while (reader.Read())
{
sb.Append("username:").Append(reader.GetString(0)).Append("\n")
.Append("password:").Append(reader.GetString(1))
}
MessageBox.Show(sb.ToString())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)