Objectdatasouce combined with Object Design

Source: Internet
Author: User

In the past, when I wrote objectdatasouce, I used dataset, datatable, and datareader to return the value. The maintenance operation was also a step-by-step parameter transfer. Later, I saw Jeff's big article.Article[Objectdatasoruce binds the businessobject control], which can be passed in the way of objects. So Xiao meow started to check whether it can be processed in the way of objects.

First, Xiao meow designs a test data table. The data table (employees) field is as follows:

Next, we will create an employee category for this data table. In the future, this object category can be extracted as a component of the business logic layer or data layer, add, modify, delete, query, and other related functions.ProgramCodeAs follows:

Imports system. Data

Imports system. Data. sqlclient

Imports Microsoft. VisualBasic

Public class objemployee

''' <Summary>

''' Employee ID field

''' </Summary>

Private m_employeeid as integer

''' <Summary>

''' Employee name field

''' </Summary>

Private m_empname as string

''' <Summary>

''' Employee phone number field

''' </Summary>

Private m_emptel as string

Private oconns as new objconns

Private connstr as string = oconns. connstr

''' <Summary>

''' Employee ID attributes

''' </Summary>

Public property employeeid () as integer

Get

Return m_employeeid

End get

Set (byval value as integer)

M_employeeid = Value

End set

End Property

''' <Summary>

''' Employee name attributes

''' </Summary>

Public property empname () as string

Get

Return m_empname

End get

Set (byval value as string)

M_empname = Value

End set

End Property

''' <Summary>

''' Employee phone Properties

''' </Summary>

Public property emptel () as string

Get

Return m_emptel

End get

Set (byval value as string)

M_emptel = Value

End set

End Property

'Constructors

Public sub new ()

End sub

Public sub new (byval myempid as integer)

Getemp (myempid)

End sub

Private sub getemp (byval myempid as integer)

Try

Using conn as new sqlconnection (connstr)

Conn. open ()

Dim sqltxt as string = ""

Sqltxt + = "select *"

Sqltxt + = "from employees"

Sqltxt + = "where employeeid = @ employeeid"

Dim cmd as new sqlcommand (sqltxt, Conn)

Cmmd. Parameters. addwithvalue ("@ employeeid", myempid)

Dim Dr as sqldatareader = cmmd. executereader

If dr. hasrows then

While dr. Read

M_employeeid = myempid

M_empname = dr. Item ("empname ")

M_emptel = dr. Item ("emptel ")

End while

End if

Dr. Close ()

End using

Catch ex as exception

Throw new exception (ex. Message)

End try

End sub

''' <Summary>

''' Adds an employee.

''' </Summary>

Public sub add ()

Try

Using conn as new sqlconnection (connstr)

Conn. open ()

Dim sqltxt as string = ""

Sqltxt + = "insert into employees"

Sqltxt + = "(empname, emptel )"

Sqltxt + = "values (@ empname, @ emptel )"

Sqltxt + = ""

Dim cmd as new sqlcommand (sqltxt, Conn)

Cmmd. Parameters. addwithvalue ("@ empname", m_empname)

Cmmd. Parameters. addwithvalue ("@ emptel", m_emptel)

Cmmd. executenonquery ()

End using

Catch ex as exception

Throw new exception (ex. Message)

End try

End sub

''' <Summary>

''' Delete an employee

''' </Summary>

Public sub del ()

Try

Using conn as new sqlconnection (connstr)

Conn. open ()

Dim sqltxt as string = ""

Sqltxt + = "delete employees"

Sqltxt + = "where employeeid = @ employeeid"

Sqltxt + = ""

Dim cmd as new sqlcommand (sqltxt, Conn)

Cmmd. Parameters. addwithvalue ("@ employeeid", m_employeeid)

Cmmd. executenonquery ()

End using

Catch ex as exception

Throw new exception (ex. Message)

End try

End sub

''' <Summary>

''' Modify a single employee

''' </Summary>

Public sub Update ()

Try

Using conn as new sqlconnection (connstr)

Conn. open ()

Dim sqltxt as string = ""

Sqltxt + = "Update employees"

Sqltxt + = "set empname = @ empname )"

Sqltxt + = ", emptel = @ emptel"

Sqltxt + = "where employeeid = @ employeeid"

Sqltxt + = ""

Dim cmd as new sqlcommand (sqltxt, Conn)

Cmmd. Parameters. addwithvalue ("@ employeeid", m_employeeid)

Cmmd. Parameters. addwithvalue ("@ empname", m_empname)

Cmmd. Parameters. addwithvalue ("@ emptel", m_emptel)

Cmmd. executenonquery ()

End using

Catch ex as exception

Throw new exception (ex. Message)

End try

End sub

End Class

In addition, to facilitate connection string processing, a small category is also written for storage.

Imports Microsoft. VisualBasic

Public class objconns

Private m_connstr as string = "Data Source =. \ sqlexpress; attachdbfilename = | datadirectory | \ mydb. MDF; Integrated Security = true; user instance = true"

Public readonly property connstr () as string

Get

Return m_connstr

End get

End Property

End Class

Design an object that works with objectdatasouce to add, modify, delete, and query operations.

Imports system. Data

Imports system. Data. sqlclient

Imports Microsoft. VisualBasic

Public class daoemployee

Private oconns as new objconns

Private connstr as string = oconns. connstr

Public Function getallemployee () as List (of objemployee)

Try

Dim temps as new list (of objemployee)

Temps. Clear ()

Using conn as new sqlconnection (connstr)

Conn. open ()

Dim sqltxt as string = ""

Sqltxt + = "select *"

Sqltxt + = "from employees"

Dim cmd as new sqlcommand (sqltxt, Conn)

Dim Dr as sqldatareader = cmmd. executereader

If dr. hasrows then

Dim temp as objemployee

While dr. Read

Temp = new objemployee (dr. Item ("employeeid "))

Temps. Add (temp)

End while

End if

End using

Return temps

Catch ex as exception

Throw

End try

End Function

Public sub empupdate (byval oemp as objemployee)

Try

Oemp. Update ()

Catch ex as exception

Throw

End try

End sub

Public sub empdel (byval oemp as objemployee)

Try

Oemp. Del ()

Catch ex as exception

Throw

End try

End sub

Public sub empaddnew (byval oemp as objemployee)

Try

Oemp. Add ()

Catch ex as exception

Throw

End try

End sub

End Class

Is it interesting to see that getallemployee does not return dataset, able, and dataread, but an object set of objemployee. In addition, the Code for adding, modifying, and deleting programs cannot be reduced to one. There is no other parameter passed, that is, the object, and the operation is the addition, modification, and deletion provided by the object.

Then design the test screen.

<Asp: gridview id = "gvemployees" runat = "server" autogeneratecolumns = "false"

Performanceid = "odsemployees" datakeynames = "employeeid">

<Columns>

<Asp: commandfield showdeletebutton = "true" showeditbutton = "true"/>

<Asp: boundfield datafield = "employeeid" headertext = "employeeid"

Sortexpression = "employeeid"/>

<Asp: boundfield datafield = "empname" headertext = "empname"

Sortexpression = "empname"/>

<Asp: boundfield datafield = "emptel" headertext = "emptel"

Sortexpression = "emptel"/>

</Columns>

</ASP: gridview>

<Asp: objectdatasource id = "odsemployees" runat = "server"

Dataobjecttypename = "objemployee" deletemethod = "empdel"

Selectmethod = "getallemployee" typename = "daoemployee"

Updatemethod = "empupdate" insertmethod = "empaddnew">

</ASP: objectdatasource>

<Asp: detailsview id = "dvemployee" runat = "server" autogeneraterows = "false"

Performanceid = "odsemployees" defaultmode = "insert" Height = "50px"

Width = "125px" datakeynames = "employeeid">

<Fields>

<Asp: boundfield datafield = "employeeid" headertext = "employeeid"

Insertvisible = "false" sortexpression = "employeeid"/>

<Asp: boundfield datafield = "empname" headertext = "empname"

Sortexpression = "empname"/>

<Asp: boundfield datafield = "emptel" headertext = "emptel"

Sortexpression = "emptel"/>

<Asp: commandfield showinsertbutton = "true"/>

</Fields>

</ASP: detailsview>

<Br/>

The only program to be written on the screen is to re-organize the gridview once after the data in the detailview is added.

Protected sub dvemployee_iteminserted (byval sender as object, byval e as system. Web. UI. webcontrols. detailsviewinsertedeventargs) handles dvemployee. iteminserted

Me. gvemployees. databind ()

End sub

In this way, you can use the object method to process and easily extract the object, and you do not need to write the objectdatasouce collocation object, you need to write a bunch of passed parameters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.