Smark. Data 1.8, open source data access component

Source: Internet
Author: User

This topic describes how to use Smark. Data 1.8 for Rapid database application development, including configuration, SQL Execution, stored procedures, and other basic data operations.

Configuration

Before using the component, you must perform an Initialization Configuration to configure the connection information of the data access device and the corresponding database. The component can configure the data in two ways: the configuration file andCode.

    • ConfigConfiguration
 
<Configsections> <section name = "smarkdata" type = "Smark. data. smarkdatasection, Smark. data "/> </configsections> <smarkdata> <connection> <Add name =" 0 "type =" Smark. data. MSSQL, Smark. data "connectionstring =" Data Source = WIN-KHLG2RFMS2R; initial catalog = northwind; user id = sa; password = abc_123 "/> </connection> <Assembly> <clear/> <assembly/> </smarkdata>
    • CodeConfiguration
Dbcontext. setconnectiondriver <MSSQL> (connectiontype. context1); dbcontext. setconnectionstring (connectiontype. context1, @ "Data Source = WIN-KHLG2RFMS2R; initial catalog = northwind; user id = sa; Password = abc_123 ");

The above is the configuration of an MSSQL data access device and the corresponding database connection information. May someone ask if the component supports multi-database configuration? The answer is yes. The component supports up to 30 different data access information. to put it simply, use Smark. data can access up to 30 databases at the same time during database development. I believe this configuration can fully meet the needs of most applications. If it is not enough, contact me or download the code to modify it :)

Execute SQL

After configuring the data access context, it is easy to execute SQL statements.

  • Execute SQL and fillTo Object List

    Query <ilist <employee> query = "select * from employees"; foreach (employee item in query. Value ){}

    The component automatically matches the field name with the object property name and fills in the corresponding value. Do you need to describe the filled object? Components are not required to be processed at runtime. Is reflection filled? No components will generate proxy methods for the application in the runtime instead of reflection operations.

  • In many cases, we need to query a record.
    Query <employee> query = "select * from employees where employeeid = 3"; string firstname = query. value. firstname;
  • Execute a Statistical Statement
    Query <int> query = "select count (*) from employees"; int COUNT = query. value;
  • Sometimes the Query Needs dynamic combination
    Because SQL statements are dynamically combined in many cases, the component provides an SQL object to handle these situations.
    Datetime? Start = NULL, end = NULL; SQL = "select * from orders where 1 = 1"; if (start! = NULL) SQL = SQL ["start", start. value] + "orderdate >=@ start"; if (end! = NULL) SQL = SQL ["end", end. value] + "orderdate <@ end"; query <ilist <orderbase> query = SQL;
Execute the Stored Procedure

An object is required for a component to call a stored procedure.

  • Stored Procedure

    Alter procedure [DBO]. [custorderhist] @ customerid nchar (5) asselect productname, total = sum (Quantity) from products P, [Order Details] OD, orders o, customers cwhere C. customerid = @ customeridand C. customerid = O. customerid and O. orderid = OD. orderid and OD. productid = P. productidgroup by productname
  • Description Structure
    [Proc] public class custorderhist: storedprocedure {[procparameter] Public String customerid {Get; Set ;}}

    A stored procedure description can be implemented simply by inheriting the storeprocedure class, And the stored procedure name is described through the proc attribute. By default, the stored claim name is the corresponding class name; procparameter is used to describe the storage parameters. You can use direction to set the parameter type, after the object is executed, the corresponding output or return value can be obtained by accessing the relevant property value.

  • Execute the Stored Procedure
    Custorderhist COH = new custorderhist (); COH. customerid = "alfki"; query <ilist <orderhist> query = CoH;

    You only need to set the description object to the corresponding query object.

The above is the use of SQL and storage for components. If you are a loyal fans of SQL or stored procedures, believe in Smark. the functions provided by data make basic data operations easier. don't be disappointed with friends who prefer object ing operations, because the next chapter will detail the object ing function of the skill component, and I believe it will also bring surprises in object operations.

Q: Why didn't I see the specified dB information during all access operations?
In fact, the component uses the first configured dbcontext by default, so it is used for operations without specifying dbcontext.

Address: http://smark.codeplex.com/

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.