源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#中嵌入SQLite数据库的简单方法

  • 时间:2020-04-12 20:26 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#中嵌入SQLite数据库的简单方法
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快。 SQLite第一个Alpha版本诞生于2000年5月。 至2015年已经有15个年头,SQLite也迎来了一个版本 SQLite 3已经发布。 具体下载地址:[url=http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki]http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki[/url] 编写SQLite测试方法
引用命名空间:
using System.Data.SQLite;
using System.Data.SQLite.Generic;
using System.Data.Common;
 
    /// <summary>
    ///【测试方法】 简答的测试SQLite数据库及表的创建过程
    /// </summary>
    [TestMethod()]
    public void Test()
    {
      string strConnectionString = string.Empty,/*SQLite连接字符串,刚开始没有,暂时留空*/
          strDataSource = @"D:\test.db";//SQLite数据库文件存放物理地址
      //用SQLiteConnectionStringBuilder构建SQLite连接字符串
      System.Data.SQLite.SQLiteConnectionStringBuilder scBuilder = new SQLiteConnectionStringBuilder();
      scBuilder.DataSource = strDataSource;//SQLite数据库地址
      scBuilder.Password = "123456";//密码
      strConnectionString = scBuilder.ToString();
      using (SQLiteConnection connection = new SQLiteConnection(strConnectionString))
      {
        //验证数据库文件是否存在
        if (System.IO.File.Exists(strDataSource) == false)
        {
          //创建数据库文件
          SQLiteConnection.CreateFile(strDataSource);
        }
        //打开数据连接
        connection.Open();
        //Command
        SQLiteCommand command = new SQLiteCommand(connection);
        command.CommandText = "CREATE TABLE tb_User(ID int,UserName varchar(60));INSERT INTO [tb_User](ID,UserName) VALUES(1,'A')";// "CREATE TABLE tb_User(ID int,UserName varchar(60));";
        command.CommandType = System.Data.CommandType.Text;
        //执行SQL
        int iResult = command.ExecuteNonQuery();
        //可省略步骤=======关闭连接
        connection.Close();
      }
    }
示意图: [img]http://files.jb51.net/file_images/article/201506/201506041006562.png[/img] 以上所述就是本文的全部内容了,希望大家能够喜欢。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部