Linux 部署ASP.NET SQLite 應用 的坎坷之旅 附demo及源碼

來源:互聯網
上載者:User

標籤:

Linux 部署ASP.NET SQLite 應用 的坎坷之旅。文章底部 附範例程式碼。

有一台閑置的Linux VPS,嘗試著部署一下.NET 程式,結果就踏上了坑之路,不過最後算是完美解決問題,遂記錄如下。

環境:Linux系統為centos 6  mono 版本 mono-3.2.1  jexus版本 jexus-5.6.4

 

一.安裝環境

 

1.安裝mono

首先安裝系統內容:

yum -y install gcc gcc-c++ bison pkgconfig glib2-devel gettext make libpng-devel libjpeg-devel libtiff-devel libexif-devel giflib-devel libX11-devel freetype-devel fontconfig-devel  cairo-devel

下載mono 安裝:

wget http://download.mono-project.com/sources/mono/mono-3.2.1.tar.bz2tar -jxvf  mono-3.2.1.tar.bz2cd mono-3.2.1./configure –prefix=/usrmake    此時出現以下錯誤../src/.libs/libeglib.a(libeglib_la-gunicode.o): In function `monoeg_g_get_charset’: /root/mono-3.2.1/eglib/src/gunicode.c:223: undefined reference to `locale_charset’ collect2: ld returned 1 exit status make[4]: *** [test-eglib] Error 1 make[4]: Leaving directory `/root/mono-3.2.1/eglib/test’ make[3]: *** [all-recursive] Error 1 make[3]: Leaving directory `/root/mono-3.2.1/eglib’ make[2]: *** [all] Error 2 make[2]: Leaving directory `/root/mono-3.2.1/eglib’ make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory `/root/mono-3.2.1′ make: *** [all] Error 2修改 eglib/config.hvi eglib/config.h 將#define HAVE_LOCALCHARSET_H 1 替換為 #define HAVE_LOCALCHARSET_H 0makemake installmono –V  這一步顯示資訊代表安裝成功

 

2.安裝jexus  

cd /tmpwget linuxdot.net/down/jexus-5.6.4.tar.gz tar -zxvf jexus-5.6.4.tar.gz cd jexus-5.6.4 sudo ./install 

jexus 相關配置說明:http://www.linuxdot.net/bbsfile-3084

 

二.代碼部分

 

有兩種方式可以在Linux上使asp.net 操作sqlite。

由於Linux內建sqlite 環境,所以無需安裝環境。而Windows沒有需要安裝sqlite,或者下載sqlite3.dll。

本文樣本採用WebForm 開發。

 

1.使用Mono內建的 Mono.Data.Sqlite

 使用方法和Ado.Net 相同。這裡就不做過多講解。

主要在連結字串部分 "Version=3;Data Source={檔案路徑}"

 

2.使用NuGet中的sqlite-net (最終採用的方法,比較完美)

Install-Package sqlite-net

github:  https://github.com/praeclarum/sqlite-net

wiki:  https://github.com/praeclarum/sqlite-net/wiki

安裝好後就可以開發。wiki 裡面講解了一些詳細的用法。

 

我只是實現一些簡單的增刪查改操作。

使用方法:

[PrimaryKey, AutoIncrement] //主鍵加自增public int ID { get; set; }

更多方法參見wiki

 

基本的一些代碼

public partial class Demo : System.Web.UI.Page    {        private SQLiteConnection con = null;        protected void Page_Load(object sender, EventArgs e)        {            var path = Server.MapPath("~/App_Data/Demo.db");            con = new SQLiteConnection(path);            //con.CreateTable<User>();//建立表 只需執行一次            if (!string.IsNullOrEmpty(Request["id"]))            {                con.Delete<User>(Request["id"]);                Response.Redirect("Demo.aspx");            }            var cmd = con.CreateCommand("select * from User");            var list = cmd.ExecuteQuery<User>();            foreach (var item in list)            {                Response.Write(string.Format("{0}-{1}-{2}   <a href=‘Demo.aspx?id={3}‘>刪除</a><br>", item.Name,item.PassWord,item.CreateTime,item.ID));            }        }        protected void BtnAdd_Click(object sender, EventArgs e)        {            var rand = new Random().Next(100, 999);            User user = new User();            user.Name = "ceshi"+rand;            user.PassWord = "123456"+rand;            user.CreateTime = DateTime.Now;            con.Insert(user);//添加資料            Response.Redirect("Demo.aspx");        }    }

 

最終發布的程式採用winscp上傳到vps中的。完美在linux上運行。

運行:

 上面的系統標識為linux。

 

三.總結

 

asp.net mvc 5.0版本及以上在linux mono 環境下支援不是特別好。

jexus 對於大小寫敏感,必須保持大小寫一致。

最後感謝網路上的各位前輩的無私分享。特別感謝 園子@張善友的分享。

 

源碼地址:https://github.com/linezero/sqlitedemo

 

參考連結: http://www.cnblogs.com/shanyou/p/3322811.html

      http://www.jexus.org/

      http://www.linuxdot.net/

 

如果你覺得本文對你有協助,請點擊“推薦”,謝謝。

Linux 部署ASP.NET SQLite 應用 的坎坷之旅 附demo及源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.