Codesmith data layer template makes operations easier

Source: Internet
Author: User

 

<% @ Codetemplate Language = "C #" targetlanguage = "text" src = "" inherits = "" DEBUG = "false" Description = "template description here." %>
<% @ Assembly name = "schemaexplorer" %>

<% @ Assembly name = "system. Data" %>

<% @ Property name = "targettable" type = "schemaexplorer. tableschema" Category = "context" Description = "targettable that the object is based on." %>

<% @ Property name = "author" type = "system. String" default = "Xiao Changjian" Category = "context" Description = "the author for this procedure." %>

<% @ Property name = "namespace" type = "system. String" default = "MySpace" Category = "context" Description = "targettable that the object is based on." %>

<% @ Property name = "Description" type = "system. String" default = "" Category = "context" Description = "the description for this procedure." %>

<% @ Map Name = "csharpalias" src = "system-csharpalias" Description = "system to C # type map" %>

<% @ Import namespace = "schemaexplorer" %>

<% @ Import namespace = "system. Data" %>

/*----------------------------------------------------------------------

* File name: <% = gettablename (targettable) %> Dal. CS

*

* Create Author: <% = author %>

*

* Create datetime: <% = datetime. Now. tow.datestring () %>

*

* Description: <% = Description %>

*----------------------------------------------------------------------*/

Using system;

Using system. Collections. Generic;

Using system. text;

Using system. Data;

Using system. Data. sqlclient;

Using dbutilitys;

Namespace <% = namespace %>

{

Public class <% = gettablename (targettable) %> dal

{

Private Static readonly <% = gettablename (targettable) %> Dal _ classinstance = new <% = gettablename (targettable) %> Dal ();

Public static <% = gettablename (targettable) %> Dal classinstance {get {return _ classinstance ;}}

/// <Summary>

/// Add record

/// </Summary>

Public int insert _ <% = gettablename (targettable) %> (<% = gettablename (targettable) %> model instance)

{

String executesqlstr = "insert into <% = targettable. name %> (<% = getcolumnnamenopk (targettable) %>) values (<% = evaluatecolumnvaluenopk (targettable) %> )";

Sqlparameter [] para = new sqlparameter [] {

<% Int I = 0; foreach (columnschema column in targettable. nonprimarykeycolumns) {%>

New sqlparameter ("@ <% = column. name %> ", instance. <% = column. name %>) <% if (I <targettable. columns. count-2) {%>,<%}%>

<% I ++ ;}%>

};

Return sqlhelper. executenonquery (sqlhelper. sqlconnectionstring, commandtype. Text, executesqlstr, para );

}

/// <Summary>

/// Delete record

/// </Summary>

Public int Delete _ <% = gettablename (targettable) %> (string pkid)

{

String executesqlstr = "delete <% = targettable. name %> where <% = getprimarykeyname (targettable) % >=@ <% = getprimarykeyname (targettable) %> ";

Sqlparameter [] para = new sqlparameter [] {

New sqlparameter ("@ <% = getprimarykeyname (targettable) %>", pkid)

};

Return sqlhelper. executenonquery (sqlhelper. sqlconnectionstring, commandtype. Text, executesqlstr, para );

}

/// <Summary>

/// Update record

/// </Summary>

Public int update _ <% = gettablename (targettable) %> (<% = gettablename (targettable) %> model instance)

{

String executesqlstr = "Update <% = targettable. name %> set <% = updatecolumn (targettable) %> where <% = getprimarykeyname (targettable) % >=@ <% = getprimarykeyname (targettable) %> ";

Sqlparameter [] para = new sqlparameter [] {

<% Int J = 0; foreach (columnschema column in targettable. columns) {%>

New sqlparameter ("@ <% = column. name %> ", instance. <% = column. name %>) <% IF (j <targettable. columns. count-1) {%>,<%}%>

<% J ++ ;}%>

};

Return sqlhelper. executenonquery (sqlhelper. sqlconnectionstring, commandtype. Text, executesqlstr, para );

}

/// <Summary>

/// Get record by primary key

/// </Summary>

Public <% = gettablename (targettable) %> model get _ <% = gettablename (targettable) %> bypkid (string pkid)

{

String executesqlstr = "select <% = getcolumnname (targettable) %> from <% = gettablename (targettable) %> where <% = getprimarykeyname (targettable) % >=@ <% = getprimarykeyname (targettable) %> ";

Sqlparameter [] para = new sqlparameter [] {

New sqlparameter ("@ <% = getprimarykeyname (targettable) %>", pkid)

};

<% = Gettablename (targettable) %> model modelinstance = new <% = gettablename (targettable) %> model ();

Using (sqldatareader SDR = sqlhelper. executereader (sqlhelper. sqlconnectionstring, commandtype. Text, executesqlstr, para ))

{

If (SDR. Read ())

{

Modelinstance = evaluatemodel (SDR );

}

}

Return modelinstance;

}

 

 

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.