• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

c#连sqlite数据库,实现数据更新、插入

武飞扬头像
jesondata
帮助3

  1. data.db 需自行导入至bin文件夹内
  2. 安装sqlite包

data.db 需自行导入至bin文件夹内

安装sqlite包

创建SqliteHelper类

using System.Data;
using System.Data.SQLite;
using Stem.Linq;
using System.Text;
using System.Threading.Tasks;
namespace one.Service
{
  public  class SQLiteHelper
    {
        public static string ConnectionString = "Data Source=data.db;Pooling=true;FailIfMissing=false";
        private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string sqlStr, params SQLiteParameter[] p)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
                cmd.Parameters.Clear();
                cmd.Connection = conn;
                cmd.CommandText = sqlStr;
                cmd.CommandType = CommandType.Text;
                cmd.CommandTimeout = 30;
                if (p != null)
                {
                    foreach (SQLiteParameter parm in p)
                    {
                        cmd.Parameters.AddWithValue(parm.ParameterName, parm.Value);
                    }
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }
        
        /// 查询dataSet
        
        public static DataSet ExecuteQuery(string sqlStr, params SQLiteParameter[] p)
        {
            using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand())
                {
                    DataSet ds = new DataSet();
                    try
                    {
                        PrepareCommand(command, conn, sqlStr, p);
                        SQLiteDataAdapter da = new SQLiteDataAdapter(command);
                        da.Fill(ds);
                        return ds;
                    }
                    catch (Exception ex)
                    {
                        return ds;
                    }
                }
            }
        }
        /// <summary>
        /// 事务处理
        /// </summary>
        
        public static bool ExecSQL(string sqlStr, params SQLiteParameter[] p)
        {
            bool result = true;
            using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand())
                {
                    PrepareCommand(command, conn, sqlStr, p);
                    SQLiteTransaction transaction = conn.BeginTransaction();
                    try
                    {
                        command.Transaction = transaction;
                        command.ExecuteNonQuery();
                        transaction.Commit();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        result = false;
                    }
                }
            }
            return result;
        }
        public static SQLiteDataReader ExecuteReader(string sqlStr, params SQLiteParameter[] p)
        {
            using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand())
                {
                    try
                    {
                        PrepareCommand(command, conn, sqlStr, p);
                        return command.ExecuteReader(CommandBehavior.CloseConnection);
                    }
                    catch (Exception ex)
                    {
                        return null;
                    }
                }
            }
        }
        public static int ExecuteNonQuery(string sqlStr, params SQLiteParameter[] p)
        {
            using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand())
                {
                    try
                    {
                        PrepareCommand(command, conn, sqlStr, p);
                        return command.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        return -99;
                    }
                }
            }
        }
       


         

外部调用


      3.更新和插入方法:

private void insert_sql(string str)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into test(");
            strSql.Append("id,name,ph,tds,yang,press)");
            strSql.Append(" values (");
            strSql.Append("@id,@name,@ph,@tds,@yang,@press)");
            SQLiteParameter[] parameters = {
            new SQLiteParameter("@id",2),
            new SQLiteParameter("@name","test1 "),
            new SQLiteParameter("@ph",3 ),
            new SQLiteParameter("@tds",4 ),
            new SQLiteParameter("@yang",5 ),
            new SQLiteParameter("@press",4)
              };
            Service.SQLiteHelper.ExecSQL(strSql.ToString(), parameters);
        }

//更新
        private void updata()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update test set ");
            strSql.Append(" name=@name,ph=@ph,tds=@tds,yang=@yang,press=@press");
            strSql.Append(" where id=1");
            SQLiteParameter[] parameters = {
            new SQLiteParameter("@name","test2 "),
            new SQLiteParameter("@ph",3 ),
            new SQLiteParameter("@tds",4 ),
            new SQLiteParameter("@yang",5 ),
            new SQLiteParameter("@press",4)
              };
            Service.SQLiteHelper.ExecSQL(strSql.ToString(), parameters);    
       }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfheibi
系列文章
更多 icon
同类精品
更多 icon
继续加载