SQLite C # local data storage scheme of

Even doing network applications, in the case of disconnection, also need to be considered locally stored data. Before SQLite appear under a large amount of data, we have been using ACCESS, a small amount of data, the file is stored. ACCESS does not support atomic transactions, (which always happens) in case of power failure causes data is difficult to recover.

 

A: install

SQLITE, is a lightweight database, a relational database management system to comply with the ACID. I directly using http://sqlite.phxsoftware.com/ (An Open Source at The ADO.NET Provider for SQLite Database Engine). The download is complete is an EXE, after the installation root directory as follows:

clip_image002

There is a test tool under Bin, you can view the performance indicators of local SQLITE run.

 

II: New Database

After installation, open visual studio, a new data connection, the data source can be seen that a plurality SQLite.

clip_image004

New connection, as shown below. SQLITE database, after saving a file.

clip_image006

 

Three: Database Maintenance

SQLITE data may be maintained in terms of VS, as shown below:

clip_image008

SQL Query Analyzer can be used similar function in VS, as shown below:

clip_image010

 

Four: Mixed Mode

Installation, can be directly referenced in the project set, more

System.Data.SQLite

System.Data.SQLite.Linq

Two assemblies, due http://sqlite.phxsoftware.com/ the mixed mode System.Data.SQLite assembly, is generated for "v2.0.50727" runtime version, without additional information in the configuration , the assembly can not be loaded in the 4.0 runtime. Therefore, the following parameters need to be configured in App.config.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

 

  

五:SQLiteHelper

最后,提供一个自己写的SQLiteHelper:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;
using System.Data.Common;

namespace Com.Luminji.DataService.SQLHelpers
{
    public class SQLiteHelper
    {
        /// <summary>
        /// ConnectionString样例:Data Source=Test.db3;Pooling=true;FailIfMissing=false
        /// </summary>
        public static string ConnectionString { get; set; }

        private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params object[] p)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Parameters.Clear();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            cmd.CommandType = CommandType.Text;
            cmd.CommandTimeout = 30;
            if (p != null)
            {
                foreach (object parm in p)
                    cmd.Parameters.AddWithValue(string.Empty, parm);
            }
        }

        public static DataSet ExecuteQuery(string cmdText, params object[] p)
        {
            using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand())
                {
                    DataSet ds = new DataSet();
                    PrepareCommand(command, conn, cmdText, p);
                    SQLiteDataAdapter da = new SQLiteDataAdapter(command);
                    da.Fill(ds);
                    return ds;
                }
            }
        }

        public static int ExecuteNonQuery(string cmdText, params object[] p)
        {
            using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand())
                {
                    PrepareCommand(command, conn, cmdText, p);
                    return command.ExecuteNonQuery();
                }
            }
        }

        public static SQLiteDataReader ExecuteReader(string cmdText, params object[] p)
        {
            using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand())
                {
                    PrepareCommand(command, conn, cmdText, p);
                    return command.ExecuteReader(CommandBehavior.CloseConnection);
                }
            }
        }

        public static object ExecuteScalar(string cmdText, params object[] p)
        {
            using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand())
                {
                    PrepareCommand(command, conn, cmdText, p);
                    return command.ExecuteScalar();
                }
            }
        }

    }
}
 
六:附注
SQLite官方网站: http://www.sqlite. org/ 时第一眼看到关于SQLite的特性。
  1. ACID事务
  2. 零配置 – 无需安装和管理配置
  3. 储存在单一磁盘文件中的一个完整的数据库
  4. 数据库文件可以在不同字节顺序的机器间自由的共享
  5. 支持数据库大小至2TB
  6. 足够小, 大致3万行C代码, 250K
  7. 比一些流行的数据库在大部分普通数据库操作要快
  8. Simple, easy API
  9. include TCL bindings, through binding Wrapper support for other languages
  10. Good annotated source, and has more than 90% coverage of test
  11. Independent: no additional dependent
  12. Source complete Open, you can be used for any purpose, including selling it
  13. supports a variety of development languages, C, PHP, Perl, Java, ASP .NET, Python

This article Source: http://www.cnblogs.com/luminji/ 

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/04/12/2443994.html

Guess you like

Origin blog.csdn.net/weixin_33830216/article/details/93495118
Recommended