清风缘 發表於 2020-6-9 15:25:00

C#操作SQLite数据库

<h2>一、SQLite介绍</h2>
<h3>1、SQLite 简介</h3>
<p>SQLite是一个开源、免费的小型RDBMS(关系型数据库),能独立运行、无服务器、零配置、支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准。</p>
<p>这意味着与其他数据库一样,您不需要在系统中配置。SQLite 引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite 直接访问其存储文件。</p>
<p>SQLite 源代码不受版权限制。SQLite数据库官方主页:http://www.sqlite.org/index.html</p>
<p><img src="https://img2020.cnblogs.com/blog/24244/202006/24244-20200610112535416-631099428.png" alt="image" width="640" height="311" title="image" border="0" style="border: 0 currentColor; display: inline; background-image: none; border-image: none"></p>
<h3>2、为什么要用 SQLite?</h3>
<ul>
<li>不需要一个单独的服务器进程或操作的系统(无服务器的)。</li>
<li>SQLite 不需要配置,这意味着不需要安装或管理。</li>
<li>一个完整的 SQLite 数据库是存储在一个单一的跨平台的磁盘文件。</li>
<li>SQLite 是非常小的,是轻量级的,完全配置时小于 400KiB,省略可选功能配置时小于250KiB。</li>
<li>SQLite 是自给自足的,这意味着不需要任何外部的依赖。</li>
<li>SQLite 事务是完全兼容 ACID 的,允许从多个进程或线程安全访问。</li>
<li>SQLite 支持 SQL92(SQL2)标准的大多数查询语言的功能。</li>
<li>SQLite 使用 ANSI-C 编写的,并提供了简单和易于使用的 API。</li>
<li>SQLite 可在 UNIX(Linux, Mac OS-X, Android, iOS)和 Windows(Win32, WinCE, WinRT)中运行。</li>
</ul>
<h3>3、SQLite 局限性</h3>
<p>在 SQLite 中,SQL92 不支持的特性如下所示:</p>
<p><img src="https://img2020.cnblogs.com/blog/24244/202006/24244-20200610112536554-660157953.png" alt="image" width="1024" height="295" title="image" border="0" style="border: 0 currentColor; display: inline; background-image: none; border-image: none"></p>
<h3>4、SQLite GUI客户端列表:</h3>
<ul>
<li>SQLite Expert :SQLite administration | SQLite Expert</li>
<li>SQLite Administrator:http://download.orbmu2k.de/files/sqliteadmin.zip</li>
<li>SQLiteStudio:</li>
<li>SQLite Database Browser:</li>
<li>Navicat Premium :Navicat | 下载 Navicat for SQLite 14 天免费 Windows、macOS 和 Linux 的试用版</li>
</ul>
<p><img src="https://img2020.cnblogs.com/blog/24244/202011/24244-20201130161403774-74201191.png" alt="image" width="1024" height="423" title="image" border="0" style="border: 0 currentColor; display: inline; background-image: none"></p>
<p>&nbsp;</p>
<h2>二、C#操作数据库</h2>
<h3>1、关于SQLite的connection string:</h3>
<p>http://www.connectionstrings.com/sqlite/</p>
<p>基本方式:Data Source=c:\mydb.db;Version=3;</p>
<h3>2、C#下SQLite操作</h3>
<p>驱动dll下载:System.Data.SQLite</p>
<p>也Nuget&nbsp;直接搜索安装:System.Data.SQLite.Core</p>
<p>NuGet Gallery | SQLite</p>
<p><img src="https://img2020.cnblogs.com/blog/24244/202112/24244-20211207162706698-965855406.png" alt="" width="609" height="453" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>C#使用SQLite步骤:</p>
<p>(1)新建一个project</p>
<p>(2)添加SQLite操作驱动dll引用:System.Data.SQLite.dll</p>
<p>(3)使用API操作SQLite DataBase</p>
<div class="cnblogs_code" style="padding: 5px; border: 1px solid rgba(204, 204, 204, 1); background-color: rgba(245, 245, 245, 1); border-image: none">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Data.SQLite;

</span><span style="color: rgba(0, 0, 255, 1)">namespace</span><span style="color: rgba(0, 0, 0, 1)"> SQLiteSamples
{
    </span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Program
    {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">数据库连接</span>
<span style="color: rgba(0, 0, 0, 1)">      SQLiteConnection m_dbConnection;

      </span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> Main(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">[] args)
      {
            Program p </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Program();
      }

      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Program()
      {
            createNewDatabase();
            connectToDatabase();
            createTable();
            fillTable();
            printHighscores();
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个空的数据库</span>
      <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> createNewDatabase()
      {
            SQLiteConnection.CreateFile(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyDatabase.db</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);//可以不要此句
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个连接到指定数据库</span>
      <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> connectToDatabase()
      {
            m_dbConnection </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> SQLiteConnection(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Data Source=MyDatabase.db;Version=3;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);//没有数据库则自动创建
            m_dbConnection.Open();
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在指定数据库中创建一个table</span>
      <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> createTable()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">string</span> sql = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">create table <span style="background-color: rgba(255, 255, 0, 1)"> if not exists</span> highscores (name varchar(20), score int)</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            SQLiteCommand command </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">插入一些数据</span>
      <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> fillTable()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">string</span> sql = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">insert into highscores (name, score) values ('Me', 3000)</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            SQLiteCommand command </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">insert into highscores (name, score) values ('Myself', 6000)</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            command </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">insert into highscores (name, score) values ('And I', 9001)</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            command </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用sql查询语句,并显示结果</span>
      <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> printHighscores()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">string</span> sql = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">select * from highscores order by score desc</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            SQLiteCommand command </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SQLiteCommand(sql, m_dbConnection);
            SQLiteDataReader reader </span>=<span style="color: rgba(0, 0, 0, 1)"> command.ExecuteReader();
            </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (reader.Read())
                Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Name: </span><span style="color: rgba(128, 0, 0, 1)">"</span> + reader[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">"</span>] + <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">\tScore: </span><span style="color: rgba(128, 0, 0, 1)">"</span> + reader[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">score</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">]);
            Console.ReadLine();
      }
    }
}</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/24244/202011/24244-20201130161405172-1742791026.png" alt="image" width="1024" height="618" title="image" border="0" style="border: 0 currentColor; display: inline; background-image: none"></p>
<h2>三、下载地址:</h2>
<p>链接:https://pan.baidu.com/s/1iryCFW05hu-U_ZDZkMFA6A <br>提取码:q5xy

</p><br><br>
来源:https://www.cnblogs.com/springsnow/p/13072915.html
頁: [1]
查看完整版本: C#操作SQLite数据库