在asp.net web 程式中使用Sqlite資料庫

來源:互聯網
上載者:User

本文介紹了如何在asp.net web 程式中使用Sqlite嵌入式資料庫,sqlite資料庫可以作為檔案放在網站的APP_DATA目錄下,適合小網站使用,使用它不需要買sql server空間,而且據說它的效能很不錯。

Sqlite是嵌入資料庫,類似與MS Sql Server Compact,之所以不用Sql server compact是因為它既不支援SELECT TOP也不支援ROW_NUMBER()還不支援LIMIT,也就是我沒有辦法用它分頁了,取資料的時候必鬚根據條件取,不能在給定條件下取n條。

1. 安裝Sqlite資料庫,sqlite資料庫非常方便,他的安裝只有一個exe檔案,可以下載。

下載可執行檔之後使用命令“sqlite3 dbname”執行就可以建立資料庫。

或者為了開發方便下載Sqlite Developer軟體,使用軟體建立也有方便。下載連結

2. 安裝dot net下的Sqlite資料庫驅動,其實就是一個dll,System.Data.SQLite,他是一個開源項目,可以到SourceForge上下載

3. 在VS中建立一個Web Application,並引用2中的dll

4. 在default.aspx的cs檔案中實現訪問Sqlite的代碼,代碼和注釋如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SQLite;

namespace SqliteWebApp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //這個檔案是預先產生的資料庫檔案
            string sqliteFilePath = Server.MapPath("~/App_Data/firstsqlite.db");
            DataSet ds = new DataSet();
            //聲明一個Sqlite資料庫的連結
            using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + sqliteFilePath))
            {
                //建立sqlite命令
                using (SQLiteCommand comm = conn.CreateCommand())
                {
                    //開啟資料庫連結
                    conn.Open();
                    //插入資料
                    comm.CommandText = "INSERT INTO [t] VALUES(10,'Hello 9')";
                    comm.ExecuteNonQuery();

                    //更新資料
                    comm.CommandText = "UPDATE [t] SET name = 'Hello 10' WHERE id = 10";
                    comm.ExecuteNonQuery();

                    //使用參數插入資料
                    comm.CommandText = "INSERT INTO [t] VALUES(@id,@name)";
                    comm.Parameters.AddRange(
                        new SQLiteParameter[]{
                        CreateSqliteParameter("@id",DbType.Int32,4,11),
                        CreateSqliteParameter("@name",DbType.String,10,"Hello 11")
                        });
                    comm.ExecuteNonQuery();

                    comm.Parameters.Clear();
                    //select資料分頁用limit就行,很方便
                    comm.CommandText = "Select * From MAIN.[t]";
                    using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(comm))
                    {
                        adapter.Fill(ds);
                    }
                }
            }

            gv1.DataSource = ds;
            gv1.DataBind();
        }

        /**//// <summary>
        /// 放回一個SQLiteParameter
        /// </summary>
        /// <param name="name">參數名字</param>
        /// <param name="type">參數類型</param>
        /// <param name="size">參數大小</param>
        /// <param name="value">參數值</param>
        /// <returns>SQLiteParameter的值</returns>
        static private SQLiteParameter CreateSqliteParameter(string name,DbType type,int size,object value)
        {
            SQLiteParameter parm = new SQLiteParameter(name,type, size);
            parm.Value = value;
            return parm;
        }
    }
}

最後希望的MS Sql Server Compact可以做一些改進,讓我們可以舒服的使用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.