sqlserver 2008,vs2010,Devperience 12.1
1.创建一个Devperience winform application
2.新加一个类文件class1.cs
并且添加引用
DevExpress.Xpo v12.1.dll
修改类文件Class1.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.Xpo; namespace DXWindowsApplication1 { public class tbUsers5 : XPObject { #region Fields private int nID; private string sUserName; private string sPassword; private string sCreatedBy; private DateTime dCreatedDate; private string sModifiedBy; private DateTime dModifiedDate; private bool bVisible; private bool bEnable; #endregion #region properties public int ID { get { return nID; } set { nID = value; } } public String UserName { get { return sUserName; } set { sUserName = value; } } public String Password { get { return sPassword; } set { sPassword = value; } } public String CreatedBy { get { return sCreatedBy; } set { sCreatedBy = value; } } public DateTime CreatedDate { get { return dCreatedDate; } set { dCreatedDate = value; } } public String ModifiedBy { get { return sModifiedBy; } set { sModifiedBy = value; } } public DateTime ModifiedDate { get { return dModifiedDate; } set { dModifiedDate = value; } } public bool Visible { get { return bVisible; } set { bVisible = value; } } public bool Enable { get { return bEnable; } set { bEnable = value; } } #endregion #region structure and method public tbUsers5() { } #endregion } }
对项目--按“生成”。
在program.cs里指定使用MSSQLServer
using System; using System.Collections.Generic; using System.Windows.Forms; using DevExpress.LookAndFeel; using DevExpress.Xpo; using DevExpress.Xpo.DB; namespace DXWindowsApplication1 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); string forcedConnectionString = @"XpoProvider=MSSqlServer;Data Source=SYFOLINK-PC\MSSQLSERVER2008;User ID=viper;Password=123456;Initial Catalog=test1;Persist Security Info=true"; XpoDefault.DataLayer = XpoDefault.GetDataLayer(forcedConnectionString, AutoCreateOption.DatabaseAndSchema); Application.Run(new Form1()); } } }
XpoDefault.DataLayer指定使用哪个数据库。
在Form1.cs里创建tbUser5的实例,然后使用实例保存方法将tbUser5类里的公共字段创建表(表名就是tbUser5这个类名)到指定数据库下.
保存实例同时可以给实例赋值,每一个实例都会对应数据库里的对应的表的一行。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.Skins; using DevExpress.LookAndFeel; using DevExpress.UserSkins; using DevExpress.XtraBars; using DevExpress.XtraBars.Ribbon; using DevExpress.XtraBars.Helpers; namespace DXWindowsApplication1 { public partial class Form1 : RibbonForm { public Form1() { InitializeComponent(); tbUsers5 tbusers5 = new tbUsers5(); tbusers5.Oid = 45; tbusers5.ID = 2; tbusers5.UserName = "viper2"; tbusers5.Password = "1234562"; tbusers5.CreatedDate = DateTime.Now; tbusers5.Visible = true; tbusers5.Enable = true; tbusers5.CreatedBy = "admin2"; tbusers5.Save(); } } }
使用了tbusers5.Save(),运行不调试,打开数据库就有数据。这个方法会创建表(如果DB里还没有这个表名=类名),并且添加一行数据(如果赋值)
下面是数据库里保存的值
可以看到,OID是自动创建自动赋值,OptimisticLockField以及GcRecord都不是我们在类里定义的字段。