Data access layer design (2)

Source: Internet
Author: User

2 Type Design

1) connectiontype database connection

Using system;

Namespace hkh. database. Type
{
/// <Summary>
/// Database connection string
/// </Summary>
/// <Remarks>
/// Create by liwt on 2006-06-23
/// Save various database connection Parameters
/// </Remarks>
Public class connectiontype
{
# Region private variable

Private databasetype m_dbtype;

Private string m_provider;

Private string m_servername;

Private string m_databasename;

Private string m_userid;

Private string m_password;

Private bool m_integratedsecurity;

Private int m_connecttimeout;

Private string m_applicationname;

Private string m_attachdbfilename;

Private string m_strconnectionstring;

Private string m_extendedproperties;

Private string m_mode;

# Endregion

# Region attributes

/// <Summary>
/// Obtain or set the Database Type
/// </Summary>
Public databasetype dbtype
{
Get
{
Return m_dbtype;
}
Set
{
M_dbtype = value;
}
}

/// <Summary>
/// Obtain or set the driver provider (oledb)
/// </Summary>
Public String provider
{
Get
{
Return m_provider;
}
Set
{
M_provider = value;
}
}

/// <Summary>
/// Obtain or set extended attributes (oledb)
/// </Summary>
Public String extendedproperties
{
Get
{
Return m_extendedproperties;
}
Set
{
M_extendedproperties = value;
}
}

/// <Summary>
/// Obtain or set the access mode (oledb)
/// </Summary>
Public String Mode
{
Get
{
Return m_mode;
}
Set
{
M_mode = value;
}
}

/// <Summary>
/// Get or set the server name
/// </Summary>
Public String servername
{
Get
{
Return m_servername;
}
Set
{
M_servername = value;
}
}

/// <Summary>
/// Obtain or set the connection string
/// </Summary>
Public String connectionstring
{
Get
{
If (null! = M_strconnectionstring &&""! = M_strconnectionstring)
Return m_strconnectionstring;
Else
Return this. toconnectionstring ();
}
Set
{
This. m_strconnectionstring = value;
}
}

/// <Summary>
/// Obtain or set the Database Name
/// </Summary>
Public String databasename
{
Get
{
Return m_databasename;
}
Set
{
M_databasename = value;
}
}

/// <Summary>
/// Obtain or set the Database User ID
/// </Summary>
Public String userid
{
Get
{
Return m_userid;
}
Set
{
M_userid = value;
}
}

/// <Summary>
/// Obtain or set the Database User Password
/// </Summary>
Public String Password
{
Get
{
Return m_password;
}
Set
{
M_password = value;
}
}

/// <Summary>
/// Obtain or set the Security Authentication Mode
/// </Summary>
Public bool integratedsecurity
{
Get
{
Return m_integratedsecurity;
}
Set
{
M_integratedsecurity = value;
}
}

/// <Summary>
/// Get or set connection timeout
/// </Summary>
Public int connecttimeout
{
Get
{
Return m_connecttimeout;
}
Set
{
M_connecttimeout = value;
}
}

/// <Summary>
/// Get or set the application name
/// </Summary>
Public String applicationname
{
Get
{
Return m_applicationname;
}
Set
{
M_applicationname = value;
}
}

/// <Summary>
/// Get or set the Object Name of the additional data
/// </Summary>
Public String attachdbfilename
{
Get
{
Return m_attachdbfilename;
}
Set
{
M_attachdbfilename = value;
}
}

# Endregion

# Region private Method
/// <Summary>
/// Generate the connection string
/// </Summary>
/// <Returns> connection string </returns>
Private string toconnectionstring ()
{
String strconnstring;
If (! (0 = m_strconnectionstring.length ))
{
Return m_strconnectionstring;
}
Strconnstring = "";

// Sqlserver
If (this. m_dbtype = databasetype. sqlserver)
{
If (true! = M_integratedsecurity)
{
Strconnstring + = "Integrated Security = false ";
}
Else
{
Strconnstring + = "Integrated Security = true ";
}
If (m_connecttimeout <0)
{
Strconnstring + = "; Connect timeout =" + "15 ";
}
Else
{
Strconnstring + = "; Connect timeout =" + m_connecttimeout.tostring ();
}
If (0! = M_servername.length)
{
Strconnstring + = "; Data Source =" + m_servername;
}
If (0! = M_databasename.length)
{
Strconnstring + = "; initial catalog =" + m_databasename;
}
If (0! = M_userid.length)
{
Strconnstring + = "; user id =" + m_userid;
}
If (0! = M_password.length)
{
Strconnstring + = "; Password =" + m_password;
}
If (0! = M_applicationname.length)
{
Strconnstring + = "" + m_applicationname;
}
If (0! = M_attachdbfilename.length)
{
Strconnstring + = "" + m_attachdbfilename;
}
}

// Oledb
If (this. m_dbtype = databasetype. oledb)
{
If (0! = M_provider.length)
{
Strconnstring + = "provider =" + m_provider;
}
If (0! = M_databasename.length)
{
Strconnstring + = "; Data Source =" + m_databasename;
}
If (0! = M_userid.length)
{
Strconnstring + = "; user id =" + m_userid;
}
If (0! = M_password.length)
{
Strconnstring + = "; Password =" + m_password;
}
If (0! = M_mode.length)
{
Strconnstring + = "; mode =" + m_mode;
}
If (0! = M_extendedproperties.length)
{
Strconnstring + = "; extended properties =" + m_extendedproperties;
}
}

// ODBC
If (databasetype. ODBC = m_dbtype)
{
}

Return strconnstring;
}

# Endregion

# Region Constructor

/// <Summary>
/// Constructor
/// </Summary>
Public connectiontype ()
{
M_dbtype = databasetype. sqlserver;
M_provider = "";
M_servername = "localhost ";
M_databasename = "master ";
M_userid = "sa ";
M_password = "";
M_integratedsecurity = true;
M_connecttimeout = 15;
M_applicationname = "";
M_attachdbfilename = "";
M_strconnectionstring = "";
M_extendedproperties = "";
M_mode = "";
}

# Endregion
}
}

2) databasetype Database Type

Using system;

Namespace hkh. database. Type
{
/// <Summary>
/// Database Type
/// </Summary>
Public Enum databasetype
{
Sqlserver, // MS SQL-SERVER
ODBC, // Oracle
Oledb // access
}
}

3) Type of stored procedure parameters of procparametertype

Using system;
Using system. Data;

Namespace hkh. database. Type
{
/// <Summary>
/// Stored Procedure Parameter type
/// </Summary>
/// <Remarks>
/// Create by liwt on 2006-06-23
/// Because the current mainstream database is SQL-SERVER, the following types of Prototype Are sqldbtype
/// When using other databases in the future, you can make minor adjustments in combination.
/// </Remarks>
Public Enum procparametertype
{
Bigint, // int64 8 bytes
Binary, // byte [] 1 to 8,000
Bit, // Boolean 1 byte
Char, // string 1 to 8,000
Datetime, // datetime, January 1
Decimal, // Decimal-10 (38)-1 and 10 (38)-1
Float, // double-1.79e + 308 to 1.79e + 308
Image, // byte [] 0 to 231-1
Int, // int32 4 bytes
Money, // Decimal-2 (63) (I .e.-922,337,203,685,477.5808) to 2 (63)-1
Nchar, // string 1 to 4,000 (UNICODE)
Ntext, // string 1 to 2 (30)-1 (UNICODE)
Nvarchar, // string 1 to 4,000 (UNICODE)
Real, // single-3.40e + 38 to 3.40e + 38
Smalldatetime, // datetime, January 1, January 1-20, 1900-January 1, June 6
Smallint, // int16 2 bytes
Smallmoney, // Decimal-214,748.3648 to + 214,748.3647
Text, // string 1 to 2 (31)-1
Timestamp, // byte [] 8 bytes
Tinyint, // byte 1 byte
Uniqueidentifier, // guid
Varbinary, // byte [] 1 through 8,000
Varchar, // string 1 to 8,000
Variant // object
}

/// <Summary>
/// Construct the custom structure of stored procedure parameters
/// </Summary>
/// <Remarks>
/// Create by liwt on 2006-06-23
/// </Remarks>
Public struct procparameter
{
# Region variable

Private string parametername;
Private object parametervalue;
Private procparametertype parametertype;
Private int parametersize;
Private parameterdirection parameterdirec;

# Endregion

# Region attributes

/// <Summary>
/// Parameter name
/// </Summary>
Public String parametername
{
Get
{
Return this. parametername;
}
Set
{
This. parametername = value;
}
}

/// <Summary>
/// Parameter value
/// </Summary>
Public object parametervalue
{
Get
{
Return this. parametervalue;
}
Set
{
This. parametervalue = value;
}
}

/// <Summary>
/// Parameter type
/// </Summary>
Public procparametertype parametertype
{
Get
{
Return this. parametertype;
}
Set
{
This. parametertype = value;
}
}

/// <Summary>
/// Parameter length
/// </Summary>
Public int parametersize
{
Get
{
Return this. parametersize;
}
Set
{
This. parametersize = value;
}
}

/// <Summary>
/// Parameter transmission direction
/// </Summary>
Public parameterdirection parameterdirec
{
Get
{
Return this. parameterdirec;
}
Set
{
This. parameterdirec = value;
}
}

# Endregion

# Region construction

Public procparameter (string pname, object pvalue, procparametertype ptype, int psize, parameterdirection pdirec)
{
This. parametername = pname;
This. parametervalue = pvalue;
This. parametertype = ptype;
This. parametersize = psize;
This. parameterdirec = pdirec;
}

Public procparameter (string pname, object pvalue, procparametertype ptype, int psize)
{
This. parametername = pname;
This. parametervalue = pvalue;
This. parametertype = ptype;
This. parametersize = psize;
This. parameterdirec = parameterdirection. input;
}

Public procparameter (string pname, object pvalue)
{
This. parametername = pname;
This. parametervalue = pvalue;
This. parametertype = procparametertype. Variant;
This. parametersize = 0;
This. parameterdirec = parameterdirection. input;
}

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