Avalonia learning practice (4)--realize the simplest SQLite operation auxiliary class

In the development of desktop applications, if there is a need for data storage, and the performance requirements are not extremely high, and the application can be run anytime and anywhere, the lightweight and compact SQlite is naturally the best choice. In fact, this article has nothing to do with Avalonia. There are also a lot of SQLite operation aids on the Internet, so here is the right to learn from the past.


1. Install Microsoft.Data.Sqlite

insert image description here
Why use Microsoft.Data.Sqlite instead of System.Data.SQLite?
In fact, there is no special reason, that is, System.Data.SQLite is used, and I want to try Microsoft.Data.Sqlite. It can be roughly distinguished from the name. Microsoft.Data.Sqlite is officially produced by Microsoft, and System.Data.SQLite is produced by the SQLite team. There is not much difference in function.

Overview of Microsoft.Data.Sqlite

2. Implement common methods

    public static class SqliteHelper
    {
    
    
        public const string DbConnectString = "Data Source=data//database.db";

        #region ExecuteReader
        public static DbDataReader ExecuteReader(string commandText, IList<SqliteParameter> parameters = null)
        {
    
    
            using (var connection = new SqliteConnection(DbConnectString))
            {
    
    
                connection.Open();
                var command = connection.CreateCommand();
                command.CommandText = commandText;
                if (parameters != null)
                {
    
    
                    foreach (var p in parameters)
                    {
    
    
                        command.Parameters.Add(p);
                    }
                }
                return command.ExecuteReader();
            }
        } 
        #endregion

        #region ExecuteDatatable
        public static DataTable ExecuteDatatable(string commandText, IList<SqliteParameter> parameters = null)
        {
    
    
            using (var connection = new SqliteConnection(DbConnectString))
            {
    
    
                connection.Open();
                var command = connection.CreateCommand();
                command.CommandText = commandText;
                if (parameters != null)
                {
    
    
                    foreach (var p in parameters)
                    {
    
    
                        command.Parameters.Add(p);
                    }
                }
                DataTable dt = new DataTable();
                dt.Load(command.ExecuteReader());
                return dt;
            }
        } 
        #endregion

        #region ExecuteList<T>
        public static List<T> ExecuteList<T>(string commandText, IList<SqliteParameter> parameters = null) where T : class, new()
        {
    
    
            using (var connection = new SqliteConnection(DbConnectString))
            {
    
    
                connection.Open();
                var command = connection.CreateCommand();
                command.CommandText = commandText;
                if (parameters != null)
                {
    
    
                    foreach (var p in parameters)
                    {
    
    
                        command.Parameters.Add(p);
                    }
                }
                var dt=new DataTable();
                var result = new List<T>();
                var reader = command.ExecuteReader();
                //var properties = typeof(T).GetProperties().ToList();
                //while (reader.Read())
                //{
    
    
                //    var obj = new T();

                //    foreach (var property in properties)
                //    {
    
    
                //        try
                //        {
    
    
                //            var id = reader.GetOrdinal(property.Name.ToString());
                //            if (!reader.IsDBNull(id))
                //            {
    
    
                //                if (reader.GetValue(id) != DBNull.Value)
                //                {
    
    
                //                    property.SetValue(obj, Convert.ChangeType(reader.GetValue(id),reader.GetFieldType(id)),null);
                //                }
                //            }
                //        }
                //        catch(Exception e)
                //        {
    
    
                //            continue;
                //        }
                //    }

                //    result.Add(obj);
                //}
                dt.Load(reader);
                string strJson=Newtonsoft.Json.JsonConvert.SerializeObject(dt);
                result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(strJson);
                return result;
            }
        }

        #endregion

        #region ExecuteScalar
        public static object ExecuteScalar(string commandText, IList<SqliteParameter> parameters, int timeout)
        {
    
    
            using (var connection = new SqliteConnection(DbConnectString))
            {
    
    
                connection.Open();
                var command = connection.CreateCommand();
                command.CommandText = commandText;
                command.CommandTimeout = timeout;
                if (parameters != null)
                {
    
    
                    foreach (var p in parameters)
                    {
    
    
                        command.Parameters.Add(p);
                    }
                }
                return command.ExecuteScalar();
            }
        } 
        #endregion

        #region ExecuteScalar<T>
        public static T ExecuteScalar<T>(string commandText, IList<SqliteParameter> parameters = null, int timeout = 30)
        {
    
    
            using (var connection = new SqliteConnection(DbConnectString))
            {
    
    
                connection.Open();
                var command = connection.CreateCommand();
                command.CommandText = commandText;
                command.CommandTimeout = timeout;
                if (parameters != null)
                {
    
    
                    foreach (var p in parameters)
                    {
    
    
                        command.Parameters.Add(p);
                    }
                }
                return (T)command.ExecuteScalar();
            }
        } 
        #endregion

        #region ExecuteNonQuery
        public static int ExecuteNonQuery(string commandText, IList<SqliteParameter> parameters, int timeout = 30)
        {
    
    
            using (var connection = new SqliteConnection(DbConnectString))
            {
    
    
                connection.Open();
                var command = connection.CreateCommand();
                command.CommandText = commandText;
                if (parameters != null)
                {
    
    
                    foreach (var p in parameters)
                    {
    
    
                        command.Parameters.Add(p);
                    }
                }
                return command.ExecuteNonQuery();
            }
        } 
        #endregion
    }

Several basic methods are implemented here, all of which are the execution of SQL statements and the return of results. On this basis, it can be further encapsulated for use by business classes. It is more convenient to find an ORM honestly for more complicated businesses.

Guess you like

Origin blog.csdn.net/lordwish/article/details/126236520