XPO 学习笔记(五)

1.绑定一般的DropDownList
      
        M_ReferringAgentAndGP是一个对应数据库表的实体类,
        M0201是要显示的文本,M0200要显示的文本对应的值域

        Dim Agent As XPCollection = New XPCollection(GetType(M_ReferringAgentAndGP))
        ddlF1609.DataSource = Agent
        ddlF1609.DataTextField = "M0201"
        ddlF1609.DataValueField = "M0200"
        ddlF1609.DataBind()


2.绑定有组合字段的DropDownList

        M_StaffDetails类中有M0000,M0002,M0003字段域,分别对应主鍵,FirstName,LastName
        Dim sName As XPCollection = New XPCollection(GetType(M_StaffDetails))
        ddlF1617.DataSource = sName
        ddlF1617.DataTextField = "FullName"
        ddlF1617.DataValueField = "M0000"
        ddlF1617.DataBind()

在M_StaffDetails类中加入如下属性:
<Persistent()> _
    Public ReadOnly Property FullName() As String
        Get
            Return String.Format("{0} {1}", M0002, M0003)
        End Get
    End Property

在生成数据库时会把FullName当作一个字段创建出来


3.通过主鍵获得唯一一条记录

   F_DomesticViolence是对应数据库中的一个表的类,ID是这个表的主


        Dim bizObject As New F_DomesticViolence
        Dim newSession As New DevExpress.Xpo.Session
        newSession.ConnectionString = DevExpress.Xpo.Session.DefaultSession.ConnectionString
        bizObject = newSession.GetObjectByKey(GetType(F_DomesticViolence), CStr(ID))


4.新增和保存一条记录


加重显示的地方是关键,开始没这样写,导致每次操作都是新增数据,后来想到由于是持久性对象,所以
只要不释放,它一直都存在,如果每次都 Dim bizObject As New ... ,那么每点保存按钮时,重新建立一个对象

所以导致每次
都是新增数据,要想保存必须找到以前的持久性对象,所以用了如下方法!

 Dim bizObject As New F_DomesticViolence
        If Request.QueryString("mode") = "edit" Then
            bizObject = New XPCollection(GetType(F_DomesticViolence), New BinaryOperator("F1600", editID, BinaryOperatorType.Equal)).Object(0)
        End If

        With bizObject

            .F1601 = chkF1601.Checked
            .F1602 = chkF1602.Checked
            .F1603 = chkF1603.Checked
            .F1604 = chkF1604.Checked
            .F1605 = ddlF1605.SelectedValue
            .F1606 = chkF1606.Checked
            .F1607 = chkF1607.Checked
            .F1608 = chkF1608.Checked
            .F1609 = ddlF1609.SelectedValue
            .F1610 = txtF1610.Text
            .F1611 = txtF1611.Text

            .F1613 = chkF1613.Checked
            .F1614 = chkF1614.Checked
            .F1615 = chkF1615.Checked
            .F1616 = txtF1616.Text
            .F1617 = ddlF1617.SelectedValue
            .F1618 = saveDateNull(txtF1618)
            .F1619 = txtF1619.Text


            'If Request.QueryString("mode") = "edit" Then
            '.F1600 = 9
            'End If

            .Save()
        End With


5.多条件查询,返回对象集合!
        
       加重显示的语句可以增加多个条件

       Dim cond As New GroupOperator
        Dim oper1 As New BinaryOperator(New OperandProperty("F1400"), New OperandValue(2), BinaryOperatorType.Greater)
        Dim oper2 As New BinaryOperator(New OperandProperty("F1401"), New OperandValue(1), BinaryOperatorType.Equal)
        cond = New GroupOperator(GroupOperatorType.And, oper1, oper2)
        Dim Collection1 As New XPCollection(GetType(F_ExitInterview), cond)
        DataGrid1.DataSource = Collection1
        DataGrid1.DataBind()

6.表关系为一对多的实现方法.

[Aggregated]   没有参数      

              实现两个持久类的级联删除的功能,简单地说就是如果加上Aggregated参数,那么删除主表的时记录时一同连子表的记录也都删除了,如果不加那么必须把子表的数据全部删除才可以删除主表的记录.

例子:

有一个主表 V_CommonReview  和一个子表V_Exit
主表V_CommonReview  主键为V0900 
子表V_Exit  主键为V0600 外鍵为V0627

一条V_CommonReview表中记录对应多条子表V_Exit的记录

那么为了实现级联关系那么先在子表V_Exit定义一个外鍵为V0627
类型为V_CommonReview对象类型.

<Association("V_ExitCommonReview")> Public V0627 As V_CommonReview


然后在V_CommonReview表中定义


<Association("V_ExitCommonReview", GetType(V_Exit))> _
    Public ReadOnly Property V_exits() As XPCollection
        Get
            Return GetCollection("V_exits")
        End Get
    End Property


注:V_ExitCommonReview为关系名称,起什么都可以


做完这步了就可以把子表当作一个数据库中的视图显示两个表中的记录了

例如:

(1)  下面是实现关联表数据集合的显示功能.

 Dim x As New XPCollection(GetType(V_Exit))
        x.DisplayableProperties = "V0627.V0900;V0627.V0901;V0627.V0902"
        DataGrid1.DataSource = x
        DataGrid1.DataBind()

默认情况下如果设置DataGrid的自动生成列属性为真,那么把子表的内容全都显示出来,但你可以通过设置XPCollection的DisplayableProperties属性来设置想要显示的主表中的字段.


(2) 下面是实现关联表单条数据的增加和保存功能.


  Dim bizObject As V_Exit
        If CheckIsEdit() Then
            bizObject = New XPCollection(GetType(V_Exit), New BinaryOperator("V0602", CInt(Session("VOLUNTEERID")), BinaryOperatorType.Equal)).Object(0)

        Else
            bizObject = New V_Exit
        End If
        With bizObject
            '.V0601 = ddlV0601.SelectedValue
            .V0602 = CInt(Session("VOLUNTEERID"))
            .V0603 = txtV0603.Text
              .......

            If .V0627 Is Nothing Then  '----这里.V0627是一个类对象
                Dim CR As New V_CommonReview

                With CR
                    .V0901 = ddlV0901.SelectedValue
                    .V0919 = txtV0919.Text
                    .V_exits.Add(bizObject)
                    .Save()
                End With
            Else
                .V0627 = New XPCollection(GetType(V_CommonReview), New BinaryOperator("V0900", .V0627.V0900, BinaryOperatorType.Equal)).Object(0)
                .V0627.V0901 = ddlV0901.SelectedValue
                       
                .V0627.V0919 = txtV0919.Text
                .V0627.V_exits.Add(bizObject)
                .Save()
            End If
         
       End With

      
只需保存主表的内容,把子表的域都赋值,那么子表的内容自动保存

 

(3) 下面是实现关联表单条数据的显示功能.

在Load数据时也要先判断一下外键是否为空,然后在Load主表信息

  Dim bizObject As V_Exit
        If CheckIsEdit() Then
            bizObject = New XPCollection(GetType(V_Exit), New BinaryOperator("V0602", CInt(Session("VOLUNTEERID")), BinaryOperatorType.Equal)).Object(0)

        Else
            Return
        End If

        With bizObject
           txtV0603.Text = .V0603
         If Not .V0627 Is Nothing Then
                ddlV0901.SelectedValue = .V0627.V0901
                ddlV0902.SelectedValue = .V0627.V0902
         End If
        End With



7.直接通过传SQL语句获得对象集合的方法!


  Dim command As IDbCommand
        Dim reader As IDataReader
        command = DevExpress.Xpo.Session.DefaultSession.ConnectionProvider.CreateCommand()
        command.CommandText = "Select * from VS_Service where F0302=2" 
        reader = command.ExecuteReader()

        DataGrid1.DataSource = reader
        DataGrid1.DataBind()
        reader.Close()



8.删除一条记录的写法

Dim bizobject As New F_FamilyService
        bizobject = New XPCollection(GetType(F_FamilyService), New

BinaryOperator("F0300", editID,

BinaryOperatorType.Equal)).Object(0)
        bizobject.Delete()

注:F0300为F_FamilyService表的主键

posted @ 2009-03-26 02:18  貔貅  阅读(932)  评论(0编辑  收藏  举报