您的位置:首页 > 博客中心 > 数据库 >

vs2010 net4.0 c# 操作 sqlite

时间:2022-03-14 04:33

1、百科介绍

SQLite,是一款轻型的数据库,是遵守的关系型,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是的,而且目前已经在很多中使用了它,它占用资源非常的低,在中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名来讲,它的处理速度比他们都快。SQLite第一个诞生于2000年5月。 至今已经有14个年头,SQLite也迎来了一个版本 SQLite 3已经发布。

2、下载安装

  1.   在Precompiled Binaries for Windows  下载一个shell版本,可以解压、并将解压后的目录添加到系统的 PATH 变量中,这样在cmd中可以直接使用,当然用的不多也可以每次都cd到目录执行

技术分享

 

2. 按照net版本选择的下载

如果是vs2010 请下载 http://system.data.sqlite.org/downloads/1.0.94.0/sqlite-netFx40-setup-bundle-x86-2010-1.0.94.0.exe

3、基本SQl语句

1.建库 sqlite3 test.db 

(问题:一般会出现near "sqlite3":syntax error ,但是搜索还找不到,有知道怎么回事的请告知一下;解决方法:

sqlite3 d:/test.db;)
2.建表create table testtable(id integer primary key, testname varchar(100));

技术分享

  
3、插入数据

技术分享

4、查询数据

技术分享

5.  .quit 退出, 其他命令请.help查看

4、开始c#操作sqlite
1、先去下载system.data.sqlite,安装一下,建立一个Console程序把System.Data.SQLite.dll 和 System.Data.SQLite.Linq.dll拷贝出来引用
2、第一步创建库和连接数据库
 1  string FilePath =@"D:\test.db";
 2             if (!File.Exists(FilePath))
 3             {
 4                 System.Data.SQLite.SQLiteConnection.CreateFile(FilePath);
 5             }
 6             SQLiteConnection Conn = new SQLiteConnection();
 7             SQLiteConnectionStringBuilder ConnStr = new SQLiteConnectionStringBuilder();
 8             ConnStr.DataSource = FilePath;
 9             ConnStr.Password = "pguser";
10             ConnStr.Pooling = true;
11             Conn.ConnectionString = ConnStr.ToString();
12             Conn.Open();

3、创建表

  //创建表
            SQLiteCommand cmd = new SQLiteCommand();
            string sql = "CREATE TABLE  Xlog(logtype varchar(20),content varchar(400))";
            cmd.CommandText = sql;
            cmd.Connection = Conn;
            cmd.ExecuteNonQuery();
            Conn.Dispose();

  4、插入数据

 string sql1 = "insert into Xlog(logtype,content) VALUES (‘test1‘ ,‘test2‘)";
            SQLiteCommand cmd1 = new SQLiteCommand();
            cmd1.CommandText = sql1;
            cmd1.Connection = Conn;
            cmd1.ExecuteNonQuery();
            Conn.Dispose();

  5、查询

string sql3 = "select * from Xlog";
            SQLiteCommand cmd2 = new SQLiteCommand();
            cmd2.Connection = Conn;
            cmd2.CommandText = sql3;         
            SQLiteDataReader reader =cmd2.ExecuteReader();
            StringBuilder sb = new StringBuilder();
            while (reader.Read())
            { sb.Append("logtype:"+reader.GetString(0)); }
            //Conn.Dispose();
            Conn.Close();
            Console.WriteLine(sb.ToString());
            Console.Read();

  基础的操作已经完成,其他扩展就需要大家自己baidu和阅读

 



热门排行

今日推荐

热门手游