2

SQLite的帮助类

 8 months ago
source link: https://blog.51cto.com/u_16371710/9193540
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

SQLite的帮助类

精选 原创

聪聪大神 2024-01-11 10:09:57 ©著作权

文章标签 SQL sql 数据 文章分类 .Net 后端开发 阅读数217

由于sqlserver安装复杂,维护困难,对于存储一些简单的数据,使用SQLite无疑是个好的选择

经过我的了解,我发现无论是SQLSERVER,还是MySQL,还是SQLite,他们的关键词都是大差不差的

比如SQLSERVER的connection关键词就是SqlConnection,SQLite的就是SQLiteConnection

今天是第一次使用SQLite,写了个帮助类,使用的时候直接调用就可以,非常方便

现在配置文件中写入以下代码

  <connectionStrings>
    <add name="SQLiteConnectionString"
         connectionString="Data Source=database.sqlite;"
         providerName="System.Data.SQLite" />
  </connectionStrings>

然后新建一个类,名为SQLiteHelper

以下是帮助类的代码

string connstr = ConfigurationManager.ConnectionStrings["SQLiteConnectionString"].ConnectionString.ToString();
        //执行增、删、改的方法:ExecuteNonQuery,返回受影响的行数
        public int ExecuteNonQuery(string sql, params SQLiteParameter[] pms)
        {
            try
            {
                using (SQLiteConnection conn = new SQLiteConnection(connstr))
                {
                    using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
                    {
                        if (pms != null)
                        {
                            cmd.Parameters.AddRange(pms);
                        }
                        conn.Open();
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                // 处理异常,例如记录日志或抛出异常等操作  
                throw new Exception("An error occurred while executing the SQLite command: " + ex.Message);
            }

        }
        //将查出的数据装到table里,返回一个DataTable
        public DataTable GetDatatableData(string sql, params SQLiteParameter[] pms)
        {
            DataTable dt = new DataTable();
            try
            {
                using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, connstr))
                {
                    if (pms != null)
                    {
                        adapter.SelectCommand.Parameters.AddRange(pms);
                    }
                    adapter.Fill(dt);
                }
            }
            catch (Exception ex)
            {
                // 记录或处理异常,例如输出到日志或向用户显示错误信息  
                Console.WriteLine(ex.Message);
            }
            return dt;
        }

        //将查出的数据装到DataSet里,返回一个DataSet
        public DataSet GetDataSet(string sql, params SQLiteParameter[] pms)
        {
            DataSet dataSet = new DataSet();
            try
            {
                using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, connstr))
                {
                    if (pms != null)
                    {
                        adapter.SelectCommand.Parameters.AddRange(pms);
                    }
                    adapter.Fill(dataSet);
                }
            }
            catch (Exception ex)
            {
                // 记录或处理异常,例如输出到日志或向用户显示错误信息  
                Console.WriteLine(ex.Message);
            }
            return dataSet;
        }

然后给出一个调用的方法示例,只需传入Sql语句和参数即可

/// <summary>
        /// 调用方法示例
        /// </summary>
        public void SomeMethod()
        {
            // 调用 GetDataSet 方法,传递 SQL 查询字符串和参数数组  
            string sql = "SELECT * FROM YourTable WHERE SomeColumn = @SomeValue";
            SQLiteParameter[] pms = {
                new SQLiteParameter("@SomeValue", "SomeValueToSearch")
            };
            SQLiteHelper helper = new SQLiteHelper();
            // 调用 GetDataSet 方法,并获取返回的 DataSet  
            DataSet dataSet = helper.GetDataSet(sql, pms);

        }

然后就可以操作得到的dataset了

  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK