Enterprise Application (2) basic operations of data access program block daab (VB. NET)

Source: Internet
Author: User

First of all, it is for the app. config or web. config is configured. For specific daab configurations, you can directly refer to terrylee's Enterprise Library step by step series (3): Data Access Program block-entryArticle, This article skipped. I want to talk about how daabCodeTo add, delete, edit, save, query, and control stored procedures.

In the design environment, first reference the enterpriselibrary. Common and enterpriselibrary. Data classes in enterprise and declare them.
1. Basic data operations:

private sub dataview ()
'use executedataset in enterpriselibrary to perform data Operations
dim dB as database
'create a default data link
'DB = databasefactory. createdatabase ()
'Add a parameter to map to the specified configuration file
DB = databasefactory. createdatabase ("Temp")
dim ds as new dataset

The 'executedataset method returns a dataset type and executereader and other methods.
'Here, executedataset (commandtype. Text, "select * from book") can also be written
'Executedataset ("proc_book_select") Where proc_book_select is the Stored Procedure
'Here, a select * from book statement is put, and the two implementation results are the same.

DS = dB. executedataset (commandtype. Text, "select * from book ")
Datagrid1.datasource = Ds. Tables (0)
Datagrid1.databind ()
End sub

2. perform operations using the stored procedure:
It is generally divided into three types: No parameter, with parameter, and with input and output.

private sub procdata ()
'stored procedures without parameters
'Where dbcommandwrapper is a base class, encapsulate commands and parameters to process a single object
'while executedataset only performs database operations for one dbcommandwrapper

Dim dB as database
Dim comwrapper as dbcommandwrapper
Dim ds as Dataset
'Create a database instance
DB = databasefactory. createdatabase ("Temp ")
'Passing parameters-stored procedure name
Comwrapper = dB. getsqlstringcommandwrapper ("proc_book_select ")
'Execute the Stored Procedure
DS = dB. executedataset (comwrapper)

Datagrid1.datasource = DS
Datagrid1.databind ()

'---------------------------------------------------------------------------
'Condition with Parameters
Comwrapper = dB. getsqlstringcommandwrapper ("proc_book_query ")
'Transfer Parameters
Dim sqlok = "and bname like's % '"
Comwrapper. addinparameter ("@ sqlstr", dbtype. String, sqlok)

'Execute the Stored Procedure
DS = dB. executedataset (comwrapper)

datagrid1.datasource = DS
datagrid1.databind ()
'prop
'condition with input and output parameters
dim DBC as dbcommandwrapper

DBC = dB. getstoredproccommandwrapper ("proc_book_out ")
'Introduce input parameters
DBC. addinparameter ("@ instr", dbtype. String, "test ")
'Introduce output parameters
DBC. addoutparameter ("@ outstr", dbtype. String, 20)

DB. executenonquery (DBC)
'Output result
Textbox1.text = DBC. getparametervalue ("@ outstr"). tostring
 End sub

It is very convenient and convenient to use daab to control the database.

  • Executedataset

  • Loaddataset

  • Executereader

  • Executescalar

  • Executenonquery

  • Updatedataset

    It can basically complete a large number of database operations, optimize and reuse code, and improve code consistency and security.
    With the above methods, basic database operations, such as adding, deleting, editing, saving, and querying, will all be SQL statements or
    The storage process is working, and daab only needs simple definition and operation.

  • 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.