An instance of a database operation class (A. Command class section)

Source: Internet
Author: User
Tags datetime integer tostring trim
command| Data | database using System;
Namespace Com.joybase.DB
{
<summary>
The database operations class, through the external interface of this class, can obtain the following characteristics:
1. Do not need to distinguish between database types, but to consider using System.Data.SqlClient implementation, or using System.Data.OleDB to achieve;
2. You can stereotype SQL statements, such as input "SELECT * FROM tablename where username=?" ORDER by? Desc ", and then use the modeling statement to keep the statement intact;
3. Database connection Implementation Connection Pool (this feature is based on ado.net)
4. Support DataReader output of SQL statements, no output or dataset output;
5. Support ado.net transaction level processing;
6. Support the input and output of the stored procedure;
</summary>
public class Command
{
The final SQL statement;
Private System.Text.StringBuilder M_sql;
An array of SQL statements for intermediate procedures
Private string[] M_sqlarr;
An array of alternate strings;
Private string[] M_replacetext;
The number of substitution strings;
private int m_count;
Connection type;
Private System.Data.CommandType M_commandtype;
The label used in the configuration file to configure the database connection string;
private string M_connstringsetname;
<summary>
Construction method
</summary>
<param name= "P_sql" > Mode SQL statements </param>
Public Command (String p_sql)
{
if (p_sql==null) throw new Exception ("Error Value");
if (p_sql. Trim () = = "") throw new Exception ("Error Value");
This.m_sql=new System.Text.StringBuilder ();
M_sqlarr=p_sql. Split ('? ');
M_count=m_sqlarr.length;
M_replacetext=new String[m_count-1];
M_connstringsetname= "";
This.m_commandtype=system.data.commandtype.text;
}
<summary>
The label used in the configuration file to configure the database connection string, and if not set, "database.connectionstring" is used as the connection string
</summary>
public string Connstringsetname
{
Set
{
This.m_connstringsetname=value;
}
}
<summary>
The type of command being executed, that is, a stored procedure or a normal SQL statement;
</summary>
Public System.Data.CommandType CommandType
{
Set
{
This.m_commandtype=value;
}
}
<summary>
Replaces the "?" in the p_loaction position with the specified string;
</summary>
<param name= "p_location" > Location index </param>
<param name= "p_instring" > to replace the string </param>
public void setstring (int p_location,string p_instring)
{
if (p_instring==null) throw new Exception ("Error Value");
if (P_instring.trim () ==null) throw new Exception ("Error Value");
M_replacetext[p_location-1]= "'" +p_instring.trim () + "'";
}
<summary>
Replaces the "?" in the p_location position with the specified integer type.
</summary>
<param name= "p_location" > Location index </param>
<param name= "P_inint" > to replace integer </param>
public void Setint (int p_location,int p_inint)
{
M_replacetext[p_location-1]=p_inint.tostring ();
}
<summary>
Replaces the "?" in the p_location position with the specified long integer type.
</summary>
<param name= "p_location" > Location index </param>
<param name= "P_inlong" > Long integer </param> to be substituted
public void Setlong (int p_location,int p_inlong)
{
M_replacetext[p_location-1]=p_inlong.tostring ();
}
<summary>
Replace the "?" at the p_location position at a specified time.
</summary>
<param name= "p_location" > Location index </param>
<param name= "P_indatetime" > to replace the time </param>
public void SetDateTime (int p_location,system.datetime p_indatetime)
{
M_replacetext[p_location-1]=p_indatetime.tostring ();
}
<summary>
Replace the "?" in the p_location position with the system time.
</summary>
<param name= "p_location" > Location index </param>
<param name= "P_onlydate" > If true, replace to date only, or false to time </param>
public void SetDateTime (int p_location,bool p_onlydate)
{
if (p_onlydate)
M_replacetext[p_location-1]=system.datetime.now.date.tostring ();
Else
M_replacetext[p_location-1]=system.datetime.now.tostring ();
}

private String joinsqlstring ()
{
for (int i=0;i<m_count;i++)
{
if (i!=0) this.m_sql. Append (M_replacetext[i-1]);
This.m_sql. Append (M_sqlarr[i]);

}
Return This.m_sql. ToString ();
}
<summary>
Executes the command and returns the result set of the System.Data.IDataReader result
</summary>
<returns></returns>
Public System.Data.IDataReader result ()
{
System.Data.IDataReader Result=null;

Try
{
System.Data.IDbCommand Command=provider.getconn (m_connstringsetname). CreateCommand ();
Command.commandtext=this.joinsqlstring ();
Command.commandtype=this.m_commandtype;
Command. Connection.close ();
Command. Connection.Open ();
Command. Prepare ();
if (Provider.getconn (m_connstringsetname). state==system.data.connectionstate.closed)
Provider.getconn (M_connstringsetname). Open ();
Result=command. ExecuteReader ();
Command. Connection.close ();

Provider.getconn (). Close ();
}
Catch
{
throw new Exception ("Error at Execute DB command");
}
return result;
}
<summary>
No results Execute command
</summary>
public void Executenoresult ()
{
Try
{
System.Data.IDbCommand Command=provider.getconn (m_connstringsetname). CreateCommand ();
Command.commandtext=this.joinsqlstring ();
Command.commandtype=this.m_commandtype;
Command. Connection.close ();
Command. Connection.Open ();
if (Provider.getconn (m_connstringsetname). state==system.data.connectionstate.closed)
Provider.getconn (M_connstringsetname). Open ();
Command. ExecuteNonQuery ();

}
Catch
{
throw new Exception ("Error at execute DB Command");
}


}
}
}


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.