`

Using sqlite with .NET

    博客分类:
  • C#
阅读更多
The other day I found that there is a .NET wrapper for sqlite. sqlite is a very cool embeddable SQL-92 database engine. It's a single library that gives you a very fast, very scalable (2TB), single file, multi-user database. I thought the .NET wrapper is exceptionally handy because ADO is slow, and is hard to use (compared to this) and a HUGE overkill for smaller apps. It lets smaller apps have a real relational database without huge numbers of dependencies, complicated installs, or the complexity of ADO. Here is a really quick example program I wrote to try it out:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using SQLite.NET;

namespace WindowsApplication2
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.ListBox listBox1;
        private System.ComponentModel.Container components = null;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private SQLiteClient db;

        public Form1()
        {
            db = new SQLiteClient("test.db");
            InitializeComponent();
            UpdateList();
        }

        public void UpdateList()
        {
            SQLiteResultSet results;

            results = db.Execute("select name, phone from people order by name;");
            listBox1.Items.Clear();
            foreach(ArrayList row in results.Rows)
            {
                listBox1.Items.Add(row[0]);
            }
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // listBox1
            // 
            this.listBox1.Location = new System.Drawing.Point(8, 8);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(136, 199);
            this.listBox1.TabIndex = 0;
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(192, 24);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(120, 20);
            this.textBox1.TabIndex = 1;
            this.textBox1.Text = "textBox1";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(320, 24);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(72, 16);
            this.button1.TabIndex = 2;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(408, 349);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.listBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            db.Execute("insert into people values ('" + textBox1.Text.Replace("'","''") + "','111');");
            UpdateList();
        }
    }
}
分享到:
评论

相关推荐

    VB.NET永久保存文件

    对于大量或结构化数据,可以考虑使用数据库,如SQLite、SQL Server等。VB.NET提供了ADO.NET库来与数据库交互,包括`SqlConnection`, `SqlCommand`, `SqlDataReader`等类。 9. **文件流与缓冲** `FileStream`可以...

    C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition

    Chapter 4, Using .NET Standard Types, is about commonly used .NET Core types that are part of .NET Standard and how they are related to C#. You will learn about the .NET Standard class library ...

    DbLinq 0.20.1 released(2010.04.16) -- LINQ provider for Oracle, PostgreSQL, MySQL, Ingres, SQLite, Firebird and ... SQL Server (C# 3.5-4.0)¶

    DbLinq is THE LINQ provider that allows to use ...DbLinq.Sqlite.dll: SQLite support, using either the System.Data.SQLite or Mono.Data.Sqlite drivers. DbLinq.SqlServer.dll: Microsoft SQL Server support.

    Programming Windows Store Apps with C#

    Store data and system settings locally with SQLite Use app bars for commands and the settings charm for Help options Present notifications as tile updates, badges, or toast popups Help users visualize...

    UniDAC 7.1.4

    Bug with using TVirtualQuery with TVirtualTable is fixed Bug with processing NULL values in the Loader component is fixed Bug with executing a query after executing a batch command is fixed Bug ...

    Mastering.Xamarin.UI.Development.1786462001.epub

    Build hybrid apps using the Razor Template Engine and create Razor Models that communicate with a SQLite database Use location based features within your app to display the user's current location ...

    Mastering Xamarin UI Development

    Build hybrid apps using the Razor Template Engine and create Razor Models that communicate with a SQLite database Use location based features within your app to display the user's current location ...

    windows applications using c#

    通过ADO.NET,我们可以连接到各种数据库,如SQL Server、Oracle或SQLite,并执行SQL语句进行数据操作。此外,Entity Framework是一个ORM(对象关系映射)框架,能够简化数据库操作,让开发者可以使用C#对象直接操作...

    SimpleSQL 2.11.0.unitypackage

    - Attach SQLite databases to managers in your scene - No need to move files around for mobile devices! - Simple and efficient access to data using customizable classes or write your own SQL statements...

    Unidac 4.15 For D6-Xe2 2012年3月最新源码版

    Using UniDAC with its server-independent interface is the best way to create cross-database applications. UniDAC gives outstanding flexibility and independence to your projects. 本版本支持以下 IDE...

    php.ini-development

    compatibility with older or less security conscience applications. We ; recommending using the production ini in production and testing environments. ; ...

    Universal Data Access Components (UniDAC) v3.0 for D6-D2010 (完整源码)

    Using UniDAC with its server-independent interface is the best way to create cross-database applications. UniDAC gives outstanding flexibility and independence to your projects. 本版本支持以下 IDE:...

    unidac+3.0.010 各种数据库连接

    Universal Data Access Components (UniDAC) is a powerful library of nonvisual cross-database data access components for Delphi, Delphi for .NET, C++Builder, and Free Pascal. The UniDAC library is ...

    Core PYTHON Programming, 2nd Edition (epub 格式, Python 2.5)

    Learn about Python’s database API and how to use a variety of database systems with Python, including MySQL, Postgres, and SQLite Core Python Programming delivers Systematic, expert coverage of ...

    vim数据库插件dbext_420

    *asa* *ase* *ingres* *interbase* *sqlite* *sqlsrv* *ora* *db2* 1. Overview |dbext-overview| 2. Installation |dbext-install| 3. Configuration |dbext-configure| 3.1 Displaying Results |dbext...

    一本android的好书beginning android 2 和 源码

    A Quick SQLite Primer Start at the Beginning Setting the Table Makin’ Data What Goes Around Comes Around Raw Queries Regular Queries Building with Builders Using Cursors Data, Data, Everywhere ■...

    NBEARLISY使用

    A: NBearLite is a .NET 2.0 data access component which supports MsAccess, SqlServer, Oracle, Sqlite, MySql, PostgreSql databases. Q: Is NBearLite an O/R Mapping component? A: No. Q: Then what are ...

    c# 擦操作数据库基础 实例

    通过学习这些知识,开发者可以高效地与各种数据库系统进行交互,如SQL Server、MySQL、SQLite或Oracle等。 首先,让我们深入理解C#连接数据库的基本过程。在C#中,我们通常使用ADO.NET库来实现这一目标。ADO.NET...

    PHP官方手册中文版

    ... Mehdi Achour ...74. Using PHP Manual CHM Edition 75. The Full Text Search 76. Specialities of this Edition 77. Integrating the PHP Manual 78. Skin development 79. CHM Edition Credits

    PHP手册2007整合中文版

    PHP,即...74. Using PHP Manual CHM Edition 75. The Full Text Search 76. Specialities of this Edition 77. Integrating the PHP Manual 78. Skin development 79. CHM Edition Credits

Global site tag (gtag.js) - Google Analytics