DevExpress ASP. NET experience (1)-XPO model creation, devexpress-xpo
This series uses some simple examples to introduce how to use the DevExpress ASP. NET control. First introduce the use of XPO, install DevExpress version for DXperienceUniversal-12.2.4, use Visual Studio 2012 + SQL Server2005.
What is XPO?
XPO is short for eXpress Persistent Objects. It is an ORM tool launched by DevExpress on the. NETFramwork platform. Persistent Objects means "Persistent Object". Persistence means storing data, such as databases and files. XPO Is An ORM tool that plays an intermediate layer role between application code and databases and serves as an intermediate bridge. In short, it is to map the objects created by Object-Oriented Programming in the database so that the tables in the database are mapped one by one. In object-oriented programming, we only need to care about the "object" in the program. XPO will automatically reflect our operations on the object to the database.
The installation process is skipped here...
After the installation is successful, VS2012 creates a new project and the DevExpress category option is installed, as shown in:
(Figure 1) create a solution
(Figure 2) Add a class library project
(Figure 3) create an xpo orm Model Wizard
(Figure 4) Select ing to existing database
(Figure 5) Select a database, enter UserName and Password, and select export and stored procedure here. The stored procedure selection page will appear in the subsequent steps.
(Figure 6) confirm the selected table
(Figure 7) confirm the selected Stored Procedure
(Figure 8) database table, field prefix, suffix, and other settings
(Figure 9) completed
(Figure 10) New model not saved
(Figure 11) after the new model is saved, the XXXCode directory is generated.
(Figure 12) generate the code directory structure
Automatically generated code:
ConnectionHelper. cs
using System;using DevExpress.Xpo;using DevExpress.Data.Filtering;namespace XPOModel.DemoDB{ public static class ConnectionHelper { public const string ConnectionString = @"XpoProvider=MSSqlServer;data source=.;user id=demo;password=demo;initial catalog=DemoDB;Persist Security Info=true"; public static void Connect(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption) { XpoDefault.DataLayer = XpoDefault.GetDataLayer(ConnectionString, autoCreateOption); XpoDefault.Session = null; } public static DevExpress.Xpo.DB.IDataStore GetConnectionProvider(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption) { return XpoDefault.GetConnectionProvider(ConnectionString, autoCreateOption); } public static DevExpress.Xpo.DB.IDataStore GetConnectionProvider(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption, out IDisposable[] objectsToDisposeOnDisconnect) { return XpoDefault.GetConnectionProvider(ConnectionString, autoCreateOption, out objectsToDisposeOnDisconnect); } public static IDataLayer GetDataLayer(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption) { return XpoDefault.GetDataLayer(ConnectionString, autoCreateOption); } }}
StoredProcHelper. cs
using System;using DevExpress.Xpo;using DevExpress.Data.Filtering;namespace XPOModel.DemoDB{ public static class SprocHelper { public static DevExpress.Xpo.DB.SelectedData ExecAspNetPager(Session session, int pageSize, int curPage, string viewName, string fieldName, string orderField, string orderType, string where1) { return session.ExecuteSproc("", new OperandValue(pageSize), new OperandValue(curPage), new OperandValue(viewName), new OperandValue(fieldName), new OperandValue(orderField), new OperandValue(orderType), new OperandValue(where1)); } public static DevExpress.Xpo.DB.SelectedData ExecSelectjqGridUsers(Session session, int PageIndex, string SortColumnName, string SortOrderBy, int NumberOfRows) { return session.ExecuteSproc("SelectjqGridUsers", new OperandValue(PageIndex), new OperandValue(SortColumnName), new OperandValue(SortOrderBy), new OperandValue(NumberOfRows)); } public static System.Collections.Generic.ICollection<SelectjqGridUsersResult> ExecSelectjqGridUsersIntoObjects(Session session, int PageIndex, string SortColumnName, string SortOrderBy, int NumberOfRows) { return session.GetObjectsFromSproc<SelectjqGridUsersResult>("SelectjqGridUsers", new OperandValue(PageIndex), new OperandValue(SortColumnName), new OperandValue(SortOrderBy), new OperandValue(NumberOfRows)); } public static XPDataView ExecSelectjqGridUsersIntoDataView(Session session, int PageIndex, string SortColumnName, string SortOrderBy, int NumberOfRows) { DevExpress.Xpo.DB.SelectedData sprocData = session.ExecuteSproc("SelectjqGridUsers", new OperandValue(PageIndex), new OperandValue(SortColumnName), new OperandValue(SortOrderBy), new OperandValue(NumberOfRows)); return new XPDataView(session.Dictionary, session.GetClassInfo(typeof(SelectjqGridUsersResult)), sprocData); } public static void ExecSelectjqGridUsersIntoDataView(XPDataView dataView, Session session, int PageIndex, string SortColumnName, string SortOrderBy, int NumberOfRows) { DevExpress.Xpo.DB.SelectedData sprocData = session.ExecuteSproc("SelectjqGridUsers", new OperandValue(PageIndex), new OperandValue(SortColumnName), new OperandValue(SortOrderBy), new OperandValue(NumberOfRows)); dataView.PopulateProperties(session.GetClassInfo(typeof(SelectjqGridUsersResult))); dataView.LoadData(sprocData); } }}
Users. cs
using System;using DevExpress.Xpo;using DevExpress.Data.Filtering;namespace XPOModel.DemoDB{ public partial class Users { public Users(Session session) : base(session) { } public override void AfterConstruction() { base.AfterConstruction(); } }}
Users. Designer. cs
using System;using DevExpress.Xpo;using DevExpress.Data.Filtering;namespace XPOModel.DemoDB{ public partial class Users : XPLiteObject { int fUserID; [Key(true)] public int UserID { get { return fUserID; } set { SetPropertyValue<int>("UserID", ref fUserID, value); } } string fUserName; [Size(50)] public string UserName { get { return fUserName; } set { SetPropertyValue<string>("UserName", ref fUserName, value); } } string fFirstName; [Size(50)] public string FirstName { get { return fFirstName; } set { SetPropertyValue<string>("FirstName", ref fFirstName, value); } } string fLastName; [Size(50)] public string LastName { get { return fLastName; } set { SetPropertyValue<string>("LastName", ref fLastName, value); } } string fMiddleName; [Size(50)] public string MiddleName { get { return fMiddleName; } set { SetPropertyValue<string>("MiddleName", ref fMiddleName, value); } } string fEmailID; [Size(50)] public string EmailID { get { return fEmailID; } set { SetPropertyValue<string>("EmailID", ref fEmailID, value); } } }}
So far, the XPO model has been created from the database to the XPO model. Next, we will use the XPO object to see what it brings to us?
In the next section, we continue to use XPO, combined with XpoDataSource and ASPxGridView, to complete the single-Table CRUD operation with very little code.
Bytes -------------------------------------------------------------------------------------------------------
The database creation script for the example database is attached and the SQL Server2005 database is used. The SQL statement is as follows:
CREATE DATABASE [DemoDB] ON PRIMARY ( NAME = N'DemoDB', FILENAME = N'C:\DemoDB.mdf' , SIZE = 3072KB , FILEGROWTH = 1024KB ) LOG ON ( NAME = N'DemoDB_log', FILENAME = N'C:\DemoDB_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)GOEXEC dbo.sp_dbcmptlevel @dbname=N'DemoDB', @new_cmptlevel=90GOIF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))beginEXEC [DemoDB].[dbo].[sp_fulltext_database] @action = 'disable'endGOALTER DATABASE [DemoDB] SET ANSI_NULL_DEFAULT OFF GOALTER DATABASE [DemoDB] SET ANSI_NULLS OFF GOALTER DATABASE [DemoDB] SET ANSI_PADDING OFF GOALTER DATABASE [DemoDB] SET ANSI_WARNINGS OFF GOALTER DATABASE [DemoDB] SET ARITHABORT OFF GOALTER DATABASE [DemoDB] SET AUTO_CLOSE OFF GOALTER DATABASE [DemoDB] SET AUTO_CREATE_STATISTICS ON GOALTER DATABASE [DemoDB] SET AUTO_SHRINK OFF GOALTER DATABASE [DemoDB] SET AUTO_UPDATE_STATISTICS ON GOALTER DATABASE [DemoDB] SET CURSOR_CLOSE_ON_COMMIT OFF GOALTER DATABASE [DemoDB] SET CURSOR_DEFAULT GLOBAL GOALTER DATABASE [DemoDB] SET CONCAT_NULL_YIELDS_NULL OFF GOALTER DATABASE [DemoDB] SET NUMERIC_ROUNDABORT OFF GOALTER DATABASE [DemoDB] SET QUOTED_IDENTIFIER OFF GOALTER DATABASE [DemoDB] SET RECURSIVE_TRIGGERS OFF GOALTER DATABASE [DemoDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF GOALTER DATABASE [DemoDB] SET DATE_CORRELATION_OPTIMIZATION OFF GOALTER DATABASE [DemoDB] SET PARAMETERIZATION SIMPLE GOALTER DATABASE [DemoDB] SET READ_WRITE GOALTER DATABASE [DemoDB] SET RECOVERY FULL GOALTER DATABASE [DemoDB] SET MULTI_USER GOALTER DATABASE [DemoDB] SET PAGE_VERIFY CHECKSUM GOUSE [DemoDB]GOIF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY') ALTER DATABASE [DemoDB] MODIFY FILEGROUP [PRIMARY] DEFAULTGOUSE [DemoDB]GO/****** Object: Table [dbo].[Users] Script Date: 2014/11/27 13:36:56 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[Users]([UserID] [int] IDENTITY(1,1) NOT NULL,[UserName] [nvarchar](50) NULL,[FirstName] [nvarchar](50) NULL,[LastName] [nvarchar](50) NULL,[MiddleName] [nvarchar](50) NULL,[EmailID] [nvarchar](50) NULL, CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ([UserID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO