博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#之WPF连接sqllite数据库
阅读量:4885 次
发布时间:2019-06-11

本文共 3275 字,大约阅读时间需要 10 分钟。

using System;using System.Collections.Generic;using System.Data;using System.Data.SQLite;namespace EasyEnergy.AppLogicService{    public class AlarmSqliteDbHelp    {        public static string ConnSqlLiteDbPath = @"D:\data\test.data";        public static string ConnString        {            get            {                return string.Format("Data Source={0}", ConnSqlLiteDbPath);            }        }        public static int ExecuteRefId(string sql)        {            SQLiteConnection connection;            SQLiteTransaction transaction = null;            try            {                connection = new SQLiteConnection(ConnString);                connection.Open();                var command = new SQLiteCommand(connection)                {                    CommandText = sql + ";SELECT last_insert_rowid()"                };                return Convert.ToInt32(command.ExecuteScalar());            }            catch (Exception)            {                transaction.Rollback();                throw;            }        }        public static int Execute(string sSQL, bool bUseTransaction = false)        {            int num;            SQLiteConnection connection;            //if (!bUseTransaction)            //{            //    connection = new SQLiteConnection(ConnString);            //    connection.Open();            //    var command = new SQLiteCommand(connection) {            //        CommandText = sSQL            //    };            //    return command.ExecuteNonQuery();            //}            SQLiteTransaction transaction = null;            try            {                connection = new SQLiteConnection(ConnString);                connection.Open();                transaction = connection.BeginTransaction();                num = new SQLiteCommand(connection) { CommandText = sSQL }.ExecuteNonQuery();                transaction.Commit();            }            catch (Exception)            {                transaction.Rollback();                throw;            }            return num;        }        public static DataTable GetDataTable(string sSQL)        {            var connection = new SQLiteConnection(ConnString);            connection.Open();            var command = new SQLiteCommand            {                CommandText = sSQL,                Connection = connection            };            var adapter = new SQLiteDataAdapter(command);            var dataTable = new DataTable("table");            adapter.Fill(dataTable);            return dataTable;        }        public static int GetMaxId(string sKeyField, string sTableName)        {            DataTable dataTable = GetDataTable("select ifnull(max([" + sKeyField + "]),0) as MaxID from [" + sTableName + "]");            if ((dataTable != null) && (dataTable.Rows.Count > 0))            {                return Convert.ToInt32(dataTable.Rows[0][0].ToString());            }            return 0;        }        public static object GetSingle(string sSQL)        {            DataTable dataTable = GetDataTable(sSQL);            if ((dataTable != null) && (dataTable.Rows.Count > 0))            {                return dataTable.Rows[0][0];            }            return null;        }    }}

 

转载于:https://www.cnblogs.com/zdfbk/p/6993179.html

你可能感兴趣的文章
[HNOI/AHOI2018]道路
查看>>
NIO编程---通道(Channel)
查看>>
Android——BroadcastReceiver
查看>>
mysql查询处理的步骤
查看>>
This is your path and you will pursue it with excellence.
查看>>
2019春第八周作业Compile Summarize
查看>>
cocos2d-x游戏开发系列教程-前言
查看>>
如何实现类的成员函数作为回调函数
查看>>
spring中的各种注解解析
查看>>
BZOJ2244: [SDOI2011]拦截导弹(CDQ分治,二维LIS,计数)
查看>>
ubuntu因“不完整的语言支持”更新后字体变难看的解决
查看>>
C#与.NET程序员面试宝典 Day1
查看>>
Java线程实现的第三种方式Callable方式与结合Future获取返回值
查看>>
基于Apache+php+mysql的许愿墙网站的搭建create database xyq; //创建xyq数...
查看>>
securecrt中vim行号下划线问题及SecureCRT里root没有高亮的设置,修改linux终端命令行颜色...
查看>>
NFC技术:读写非NDEF格式的数据
查看>>
2792. Grammar Lessons
查看>>
Inno Setup Compiler打包需要管理员权限的程序
查看>>
qemu-img 快照的一些总结
查看>>
Do you master on array in C ?
查看>>