Three-tier architecture-database access layer full

Source: Internet
Author: User

Using system;
Using system. Data;
Using system. Data. sqlclient;
Using system. configuration;

Namespace dbbase
{

Public abstract class base
{

# Region "fields of base calss"

Protected static string strconn = configurationsettings. deleettings ["strconnection"];

Protected static string strsql;

# Endregion

# Region "properties of base class"
}

# Endregion

# Region "functions of base class"
Public base ()
{
//
// Todo: Add constructor logic here
//
}

/// <Summary>
/// Executing SQL commands
/// </Summary>
/// <Param name = "strsql"> string </param>
/// <Returns> return int </returns>
Protected static int executesql (string strsql)
{
Sqlconnection mycn = new sqlconnection (strconn );
Sqlcommand mycmd = new sqlcommand (strsql, mycn );
Try
{
Mycn. open ();
Mycmd. executenonquery ();
Return 0;
}
Catch (system. Data. sqlclient. sqlexception E)
{
Throw new exception (E. Message );
}
Finally
{
Mycmd. Dispose ();
Mycn. Close ();
}
}

/// <Summary>
/// Executing SQL commands
/// </Summary>
/// <Param name = "strsql"> SQL statement to be executed, string type </param>
/// <Returns> returns the execution status and integer value. </returns>
Protected static int executesqlex (string strsql)
{
Sqlconnection mycn = new sqlconnection (strconn );
Sqlcommand mycmd = new sqlcommand (strsql, mycn );

Try
{
Mycn. open ();
Sqldatareader myreader = mycmd. executereader ();
If (myreader. Read ())
{
Return 0;
}
Else
{
Throw new exception ("value unavailable! ");
}
}
Catch (system. Data. sqlclient. sqlexception E)
{
Throw new exception (E. Message );
}
Finally
{
Mycmd. Dispose ();
Mycn. Close ();
}
}

/// <Summary>
/// Get Dataset
/// </Summary>
/// <Param name = "strsql"> (string) </param>
/// <Returns> (Dataset) </returns>
Protected static dataset executesql4ds (string strsql)
{
Sqlconnection mycn = new sqlconnection (strconn );
Try
{
Mycn. open ();
Sqldataadapter SDA = new sqldataadapter (strsql, mycn );
Dataset DS = new dataset ("ds ");
SDA. Fill (DS );
Return Ds;
}
Catch (system. Data. sqlclient. sqlexception E)
{
Throw new exception (E. Message );
}
Finally
{
Mycn. Close ();
}
}

/// <Summary>
/// Get single value [from: 51item.net]
/// </Summary>
/// <Param name = "strsql"> (string) </param>
/// <Returns> (INT) </returns>
Protected static int executesql4value (string strsql)
{
Sqlconnection mycn = new sqlconnection (strconn );
Sqlcommand mycmd = new sqlcommand (strsql, mycn );
Try
{
Mycn. open ();
Object r = mycmd. executescalar ();
If (object. Equals (R, null ))
{
Throw new exception ("value unavailable! ");
}
Else
{
Return (INT) R;
}
}
Catch (system. Data. sqlclient. sqlexception E)
{
Throw new exception (E. Message );
}
Finally
{
Mycmd. Dispose ();
Mycn. Close ();
}
}

/// <Summary>
/// Get object
/// </Summary>
/// <Param name = "strsql"> (string) </param>
/// <Returns> (object) </returns>
Protected static object executesql4valueex (string strsql)
{
Sqlconnection mycn = new sqlconnection (strconn );
Sqlcommand mycmd = new sqlcommand (strsql, mycn );
Try
{
Mycn. open ();
Object r = mycmd. executescalar ();
If (object. Equals (R, null ))
{
Throw new exception ("Object unavailable! ");
}
Else
{
Return R;
}
}
Catch (system. Data. sqlclient. sqlexception E)
{
Throw new exception (E. Message );
}
Finally
{
Mycmd. Dispose ();
Mycn. Close ();
}
}

/// <Summary>
/// Execute multipul SQL commands
/// </Summary>
/// <Param name = "strsqls"> string </param>
/// <Returns> int </returns>
Protected static int executesqls (string [] strsqls)
{
Sqlconnection mycn = new sqlconnection (strconn );
Sqlcommand mycmd = new sqlcommand ();
Int J = strsqls. length;

Try
{
Mycn. open ();
}
Catch (system. Data. sqlclient. sqlexception E)
{
Throw new exception (E. Message );
}
Sqltransaction mytrans = mycn. begintransaction ();

try
{< br> mycmd. Connection = mycn;
mycmd. Transaction = mytrans;

Foreach (string STR in strsqls)
{
Mycmd. commandtext = STR;
Mycmd. executenonquery ();
}
Mytrans. Commit ();
Return 0;
}
Catch (system. Data. sqlclient. sqlexception E)
{
Mytrans. rollback ();
Throw new exception (E. Message );
}
Finally
{
Mycmd. Dispose ();
Mycn. Close ();
}
}

# Endregion
}
}

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.