WinRT/Metro SQLite 数据库连接方法(DBHelper)

SQLite 数据库连接方法

第一步:

第二步

第三步

namespace DBHelper.Base

 

{

    public class DBHelperSQLite : DBHelperBase

 

    {

        const string FilePath = @"App_Data\Data.dat";

 

        private static async Task<string> getFile()

 

        {

            var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(FilePath);

 

            return file.Path;

 

        }

        string Path { get; set; }

 

        public string ConnectionString

 

        {

            get

 

            {

                if (Path == null || Path == string.Empty)

 

                {

                    Path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path + "\\" + FilePath;

 

                }

                return Path;

 

            }

        }

 

        #region Linq SQL

 

        public override int Insert<T>(T Data)

 

        {

            int result = -1;

 

            using (var db = new SQLite.SQLiteConnection(ConnectionString))

 

            {

                result = db.Insert(Data);

                db.Close();

            }

            return result;

 

        }

        public override int Update<T>(T Data)

 

        {

            int result = -1;

 

            using (var db = new SQLite.SQLiteConnection(ConnectionString))

 

            {

                db.Update(Data);

                db.Close();

            }

            return result;

 

        }

        public override int Delete<T>(object primaryKey)

 

        {

            int result = -1;

 

            using (var db = new SQLite.SQLiteConnection(ConnectionString))

 

            {

                db.Delete<T>(primaryKey);

                db.Close();

            }

            return result;

 

        }

        public override List<T> Query<T>(string query, params object[] args)

 

        {

            List<T> result = null;

 

            using (var db = new SQLite.SQLiteConnection(ConnectionString))

 

            {

                result = db.Query<T>(query, args);

                db.Close();

            }

            return result;

 

        }

 

        #endregion

 

        #region Linq Table

 

        public int CreateTable<T>()

 

        {

            int result = -1;

 

            using (var db = new SQLite.SQLiteConnection(ConnectionString))

 

            {

                result = db.CreateTable<T>();

                db.Close();

            }

            return result;

 

        }

        public bool ExistTable<T>() where T : new()

 

        {

            bool result = false;

 

            using (var db = new SQLite.SQLiteConnection(ConnectionString))

 

            {

                //result = db.Table<T>().;

 

                db.Close();

            }

            return result;

 

        }

        public int DropTable<T>()

 

        {

            int result = -1;

 

            using (var db = new SQLite.SQLiteConnection(ConnectionString))

 

            {

                result = db.DropTable<T>();

                db.Close();

            }

            return result;

 

        }

 

        #endregion

 

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值