暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

逸鹏说道 2016-02-22
595

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.bz2


tar -jxvf  mono-3.2.1.tar.bz2


cd mono-3.2.1


./configure –prefix=/usr


make    此时出现以下错误


../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.h


vi eglib/config.h 将#define HAVE_LOCALCHARSET_H 1 替换为 #define HAVE_LOCALCHARSET_H 0


make


make install


mono –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 环境下支持不是特别好。

感谢@张善友 提醒: Windows不区分大小写,Linux严格区分大小写。所以路径须保持大小写一致。

最后感谢网络上的各位前辈的无私分享。特别感谢 园子@张善友的分享。

 

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

 

参考链接: http://www.cnblogs.com/shanyou/p/3322811.html

      http://www.jexus.org/

      http://www.linuxdot.net/

 


文章转载自逸鹏说道,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论