Some time ago, I saw the green version of Linux. NET-"Jws. Mono" published by xiaodie ". Because of my. net program is very interested in running on Linux, and I have read some mono information, but I have never spare time to study this, butterfly's blog aroused my interest. After four days, I finally ran my own application on Liunx, where the database used the PostgreSQL database. The database is used in the Linux. NET learning note (4) on the log of xiaodie Jing Hong.
Today, I just want to explain how to use the ASP. NET MVC + micro orm framework Petapoco to connect to the PostgreSQL database. C # many people should be familiar with operating PostgreSQL databases. They only need to use the NpgSql driver. For how to use NpgSql, refer to Npgsql, the. NET driver of PostgreSQL, published by Dr. Zhang Shanyou. For the introduction and usage of PetaPoco, you can refer to the official website of PetaPoco ,. NET object link CER PetaPoco, OoC's Blog, PetaPoco (2), PetaPoco (1) petaPoco, a small and convenient ORM class library (this is my reference on the Internet for a long time). They all have a clear and detailed introduction of how to use PetaPoco.
Because it was the first time I used the PostgreSQL database, I encountered many problems in the use process. Some problems did not exist. I only posted one problem to you, then I will explain my code to you.
<System. data> <DbProviderFactories> <add name = "Npgsql Data Provider" invariant = "Npgsql" support = "FF" description = ". net Framework Data Provider for Postgresql Server "type =" Npgsql. npgsqlFactory, Npgsql "/> </DbProviderFactories> </system. data>
In this way, the problem is easily solved. See :( example address: https://icdoe-haoxilu.rhcloud.com/) Install-Package Npgsql
Install-Package PetaPoco
2. Let's take a look at the important code in Web. config.
1> database connection string
<connectionStrings> <add name ="Postgresql" connectionString="Server=127.0.0.1;User id=postgres;password=123;Database=mono_test;" providerName="Npgsql"/> </connectionStrings>
2> NpgSql driver configuration file
<! -- Provider-driven configuration file --> <system. data> <DbProviderFactories> <add name = "Npgsql Data Provider" invariant = "Npgsql" support = "FF" description = ". net Framework Data Provider for Postgresql Server "type =" Npgsql. npgsqlFactory, Npgsql "/> </DbProviderFactories> </system. data>
3. Check the overall project structure.
Namespace PetaPoco {[TableName ("userinfo")] [PrimaryKey ("id")] [ExplicitColumns] public class UserInfo {[Column ("id")] public int Id {get; set;} [Column ("name")] public string Name {get; set;} [Column ("age")] public int Age {get; set ;} [Column ("qq")] public int Qq {get; set ;}}}
5. Code in Controllers
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using PetaPoco;namespace PostgreSqlDemo.Controllers{ public class HomeController : Controller { // // GET: /Home/ Database db = new PetaPoco.Database("Postgresql"); public ActionResult Index() { ViewData.Model = db.Query<UserInfo>("select * from userinfo"); return View(); } // // GET: /Home/Details/5 public ActionResult Details(int id) { ViewData.Model = db.SingleOrDefault<UserInfo>("select * from userinfo where id=@0", id); return View(); } // // GET: /Home/Create public ActionResult Create() { return View(); } // // POST: /Home/Create [HttpPost] public ActionResult Create(UserInfo user) { try { db.Insert(user); return RedirectToAction("Index"); } catch { return View(); } } // // GET: /Home/Edit/5 public ActionResult Edit(int id) { ViewData.Model = db.SingleOrDefault<UserInfo>("where id=@0", id); return View(); } // // POST: /Home/Edit/5 [HttpPost] public ActionResult Edit(UserInfo user) { try { db.Update(user); return RedirectToAction("Index"); } catch { return View(); } } // // GET: /Home/Delete/5 public ActionResult Delete(int id) { ViewData.Model = db.SingleOrDefault<UserInfo>("where id=@0",id); return View(); } // // POST: /Home/Delete/5 [HttpPost] public ActionResult Delete(UserInfo user) { try { db.Delete(user); return RedirectToAction("Index"); } catch { return View(); } } }}
6. The code in the view will be written using asp.net mvc, and the code will not be pasted here.
ASP. net mvc uses the Petapoco micro ORM framework + NpgSql driver to connect to the PostgreSQL database. I will record a video tutorial corresponding to this blog post later, the source code and video tutorials will be published in a later blog.
Finally, I would like to thank Mr. Zhang Shanyou and Mr. xiaodie Jing Hong for their blogs, and especially for their help on QQ.
Author: Hao xilu May 15, 2014 16:47:44
Blog: http://www.cnblogs.com/haoxilu/