首先產生一個 Web Site,方法和以前的版本其實大同小異,最後產出的結果會因版本差異,目錄結構不一樣而已。


 
SQLite是嵌入式資料庫,被定位成單機用的,所以在ASP.NET Web Site 這種多人連線環境並不支援。所以在開發上並不是很便利,你無法透過編輯器提供的功能來新增DataSource,一切都要自己寫程式碼來進行SQLite的操作。(應該可以透過Regedit去修改Visual Web Developer 2010 Express的機碼,針對Add New Item這部份來新增這項目,方便以後新增DataSource,只是修改機碼有其風險性,這邊就沒去仔細研究了)
 
要讓 SQLite 能正常運作,有幾個重要步驟如下:
1. 在方案中使用[加入參考],將 System.Data.SQLite 加入方案中,此時 web.config 檔案中會多出一段設定。


<assemblies>
<add assembly="System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=DB937BC2D44FF139"/>
</assemblies>


2. 在方案中的 App_Data 目錄選擇 [加入現有項目],將要使用 SQLite 檔案加進來。
 
3. 修改 web.config,加入 connectionString 設定


<connectionStrings>
    <add name="SQLiteService" connectionString="Data Source=|DataDirectory|Your.db;Version=3;" providerName="System.Data.SQLite"/>
</connectionStrings>


4. 在網頁(*.cs)新增操作 SQLite 的相關語法。


    protected void Page_Load(object sender, EventArgs e)
    {
        string sqliteFilePath = Server.MapPath("~/App_Data/Your.db");
        using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + sqliteFilePath))
        {
            using (SQLiteCommand comm = conn.CreateCommand())
            {
                conn.Open();

                comm.CommandText = "select * from [YourData]";
                SQLiteDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    Response.Write("Id " + System.Convert.ToString(dr["Id"]) + "<br />");
                }
            }
        }
    }


從方案中的 [在瀏覽器中檢視]就可以看到實際結果了。
arrow
arrow

    mark528 發表在 痞客邦 留言(0) 人氣()