关于SQLite的使用

首先引用System.Data.SQLite.dll

//连接

 1 System.Data.SQLite.SQLiteConnection conn=new System.Data.SQLite.SQLiteConnection();
 2 
 3 string datasource="e:/test.db";//System.Web.HttpContext.Current.Server.MapPath(@"ClientBin\test.db");
 4 
 5 System.Data.SQLite.SQLiteConnection.CreateFile(datasource);
 6 
 7 System.Data.SQLite.SQLiteConnectionStringBuilder ssb=new System.Data.SQLiteConnectionStringBuilder();
 8 
 9 ssb.DataSource=datasource;
10 
11 ssb.Password="admin";//在SQLite的编辑器里面打开此数据库的时候 需要命令对数据库的保护
12 
13 ssb.ConnectionString=ssb.ToString();
14 
15 conn.open();
System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();

//创建表

1   sql = "CREATE TABLE TEST(USERNAME VARCHAR(20),PASSWORD VARCHAR(20))";                
2 
3   cmd.CommandText = sql;                
4 
5   cmd.Connection = conn;
6 
7   cmd.ExecuteNonQuery();

//新增数据

sql = "INSERT INTO TEST VALUES('A','A111111')";                

 cmd.CommandText = sql;

 cmd.ExecuteNonQuery();

 

//查询

 1 sql="select * from TEST";
 2 
 3  cmd.Connection = conn;                
 4 
 5  cmd.CommandText = sql;                
 6 
 7  //System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();                
 8 
 9  System.Data.SQLite.SQLiteDataAdapter dataAdapter = new System.Data.SQLite.SQLiteDataAdapter(cmd);                
10 
11  DataSet ds = new DataSet();                
12 
13  dataAdapter.Fill(ds);

 

 可以返回DataSet 或者是 遍历reader

posted @ 2012-03-28 14:02  GeneralKING  阅读(481)  评论(0编辑  收藏  举报