如何读取现成的sqlLite数据库

1、添加右键引用NUGet包 安装VICI CoolStorage

2、此时可以看到引用中多两个文件Vici.CoolStorage.WP8 和Vici.Core.WP8

3、

  public class SQLite
    {

        private static SQLite _instance;

        public static SQLite getInstance()
        {
            if (_instance == null)
                _instance = new SQLite();
            return _instance;
        }

        public SQLite()
        {
        }


        / <summary>
        / 通过!iStorage.FileExists(fn)保证 程序第一次
        / 运行的时候才会将数据库复制
        / 到隔离存储空间中,
        / fn为复制的数据库路径及名称
        / </summary>
        / <param name="fn"></param>
        //private void move(string fn)
        //{
        //    //StreamResourceInfo sr = Application.GetResourceStream(new Uri(fn, UriKind.Relative));
        //    IsolatedStorageFile iStorage = IsolatedStorageFile.GetUserStoreForApplication();
        //    if (!iStorage.FileExists(fn))
        //    {
        //        using (var outputStream = iStorage.OpenFile(fn, System.IO.FileMode.CreateNew))
        //        {
        //            byte[] buffer = new byte[10000];

        //            for (; ; )
        //            {
        //                int read = sr.Stream.Read(buffer, 0, buffer.Length);

        //                if (read <= 0)
        //                    break;

        //                outputStream.Write(buffer, 0, read);
        //            }
        //        }
        //    }
        //}

        /// <summary>
        /// 将文件存储到独立存储空间
        /// </summary>
        /// <param name="assemblyName">项目名</param>
        /// <param name="dbName">文件名</param>
        private void CopyFromContentToStorage(string assemblyName, String dbName)
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            System.IO.Stream src = Application.GetResourceStream(new Uri("/" + assemblyName + ";component/DataBase/" + dbName, UriKind.Relative)).Stream;
            IsolatedStorageFileStream dest = new IsolatedStorageFileStream(dbName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, store);
            src.Position = 0;
            CopyStream(src, dest);
            dest.Flush();
            dest.Close();
            src.Close();
            dest.Dispose();
        }

        /// <summary>
        /// 复制流
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        private static void CopyStream(System.IO.Stream input, IsolatedStorageFileStream output)
        {
            byte[] buffer = new byte[32768];
            long TempPos = input.Position;
            int readCount;
            do
            {
                readCount = input.Read(buffer, 0, buffer.Length);
                if (readCount > 0)
                {
                    output.Write(buffer, 0, readCount);
                }
            } while (readCount > 0);
            input.Position = TempPos;
        }

 

        /// <summary>
        /// 查询区域信息表中的所有数据信息
        /// </summary>
        /// <param name="sTableName">表名称</param>
        /// <param name="sWhere">查询条件</param>
        /// <param name="sOrder"></param>
        /// <returns></returns>
        public CSGenericRecordList queryAllData(String sSelect, String sTableName, String sWhere, String sOrder)
        {
             string strSql = string.Empty;

            strSql += "SELECT " +sSelect+" FROM " + sTableName;

            if (!sWhere.Equals(""))
            {
                strSql = strSql + " WHERE " + sWhere;
            }
            if (!sOrder.Equals(""))
            {
                strSql=strSql+" ORDER BY "+sOrder;
            }

            //move(fn);

            CopyFromContentToStorage("PingAn_WP", "FinancialTel.db");

            //设置使用的数据库
            CSConfig.SetDB("FinancialTel.db");

            CSGenericRecordList arealist = CSDatabase.RunQuery(strSql);

            return arealist;

        }
    }  

4、 调用

 protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            SQLite sqlLite = new SQLite();
            CSGenericRecordList cslis = sqlLite.queryAllData("distinct level2,tel", "pingan", "level3=''", "sort");
            lb_Main.ItemsSource = from cs in cslis select new PingAn_Model(cs["level2"].ToString(), cs["tel"].ToString());

            //PingAn_Model是一个对象
            base.OnNavigatedTo(e);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值