sqlite本地数据库存储实现的流程

来源:互联网 发布:电脑动漫设计软件 编辑:程序博客网 时间:2024/04/27 21:48
输入数据,点击添加:

1.本地是否存在数据库

2.存在的话,直接添加数据,显示出来。

         D层调sqlitehelper,显示出来结果就行,如果更换了数据库那就把sqlcommand,sqldatareader,sqlconneciton等操作数据库,读取数据库的机制性性的东西改成sqlitecommand,sqlitedatareader,sqliteconnection,然后前提需要你下载一个引用,system.data.sqlite.dll文件。

3.不存在的话(也要进行第2步下面的操作)——>先执行创建数据库的过程(找它的相对路径)——>然后执行创建数据表的过程——>插入数据——>显示数据


(下面这样就写到了U层的debug文件夹下面了)

string dbPath="ItcastCater.db";            if (!System.IO.File.Exists(dbPath))            {                sqlitehelper.CreateDB("ItcastCater.db");                           }
   public static void CreateDB(string dbPath)        {            using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + dbPath))            {                connection.Open();                using (SQLiteCommand command = new SQLiteCommand(connection))                {                    command.CommandText = "CREATE TABLE Demo(id integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE)";                    command.ExecuteNonQuery();                    command.CommandText = "DROP TABLE Demo";                    command.ExecuteNonQuery();                }            }        }

然后创建表,插入数据,显示数据就行。(ps:创建数据库表,可以引用查询的sqlite方法实现)