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