• C#学习十之windows应用和windows phone应用中的SQLite操作


    Windows应用和windows phone应用可以用博客:http://www.cnblogs.com/clownice/p/4518639.html中WPF的方法使用SQLite数据库

    但是,还有另一种方法是使用SQLite:

    首先,在VS中的工具栏中有一项拓展和更新,进入后联机搜索拓展包

    根据开发的应用选用不同的拓展包,安装即可。

    然后,添加引用来引用这个拓展的应用,之后,需要调整运行架构为X64或X86否则会报错(在配置管理器中调整)。

    之后,右击项目名称进入管理Gu-net包,在里面加载两个类SQLite.cs和SQLiteAsync.cs

    到这里,配置就完成了,然后是这两个类中使用数据库的一些操作:

     class DatabaseHelperClass
        {
            SQLiteConnection dbConn;
    
            //create Table
            public async Task<bool> OnCreate(string DB_PATH)
            {
                try
                {
                    if (!CheckFileExists(DB_PATH).Result)
                    {
                        using (dbConn = new SQLiteConnection(DB_PATH))
                        {
                            dbConn.CreateTable<Information>();
                        }
                    }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            private async Task<bool> CheckFileExists(string fileName)
            {
                try
                {
                    var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            //Retrieve the specific information from the database
            public Information readRecord(int RecordId)
            {
                using (var dbConn = new SQLiteConnection(App.DB_PATH))
                {
                    var existingconact = dbConn.Query<Information>("select * from Information where Id =" + RecordId).FirstOrDefault();
                    return existingconact;
                }
            }
    
            // Retrieve the all contact list from the database. 
            public ObservableCollection<Information> ReadRecord()
            {
                using (var dbConn = new SQLiteConnection(App.DB_PATH))
                {
                    List<Information> myCollection = dbConn.Table<Information>().ToList<Information>();
                    ObservableCollection<Information> RecordList = new ObservableCollection<Information>(myCollection);
                    return RecordList;
                }
            }
    
            //Update existing conatct 
            public void UpdateRecord(Information record)
            {
                using (var dbConn = new SQLiteConnection(App.DB_PATH))
                {
                    var existingconact = dbConn.Query<Information>("select * from Information where Id =" + record.Id).FirstOrDefault();
                    if (existingconact != null)
                    {
                        existingconact.Website = record.Website;
                        existingconact.userName = record.userName;
                        existingconact.password = record.password;
                        existingconact.CreationDate = record.CreationDate;
                        dbConn.RunInTransaction(() =>
                        {
                            dbConn.Update(existingconact);
                        });
                    }
                }
            }
    
            // Insert the new contact in the Contacts table. 
            public void Insert(Information newrecord)
            {
                using (var dbConn = new SQLiteConnection(App.DB_PATH))
                {
                    dbConn.RunInTransaction(() =>
                    {
                        dbConn.Insert(newrecord);
                    });
                }
            }
    
            //Delete specific contact 
            public void DeleteRecord(int Id)
            {
                using (var dbConn = new SQLiteConnection(App.DB_PATH))
                {
                    var existingconact = dbConn.Query<Information>("select * from Information where Id =" + Id).FirstOrDefault();
                    if (existingconact != null)
                    {
                        dbConn.RunInTransaction(() =>
                        {
                            dbConn.Delete(existingconact);
                        });
                    }
                }
            }
            //Delete all contactlist or delete Contacts table 
            public void DeleteAllRecord()
            {
                using (var dbConn = new SQLiteConnection(App.DB_PATH))
                {
                    //dbConn.RunInTransaction(() => 
                    //   { 
                    dbConn.DropTable<Information>();
                    dbConn.CreateTable<Information>();
                    dbConn.Dispose();
                    dbConn.Close();
                    //}); 
                }
            }
    

      

  • 相关阅读:
    【ASP.NET 进阶】根据IP地址返回对应位置信息
    【网络文摘】编程的智慧
    【ASP.NET 类库】当你懒得用 Json+Ajax 时,可以试试 AjaxPro
    【iOS 初见】第一个简单的 iOS 应用
    【C#】C# 实现发送手机短信
    【网络文摘】一家公司要了你后,凭什么给你开高工资?
    深入理解Java虚拟机01--概述
    Java虚拟机(五)Java的四种引用级别
    OkHttp3源码详解(六) Okhttp任务队列工作原理
    OkHttp3源码详解(五) okhttp连接池复用机制
  • 原文地址:https://www.cnblogs.com/clownice/p/4521164.html
Copyright © 2020-2023  润新知