Bosses help plus a try-catch and written to a local abnormal log, make the code to be more perfect,
Code or comment whether there are any errors, whether the transaction can be finally, whether also lack of other method?
Thank you all for bosses, the code is as follows:
Public class SqlHelper
{
//create a database connection
Public static readonly string connString=ConfigurationManager. ConnectionStrings [r]. "MySql" ConnectionString;
///
///wait for executing SQL statements SqlCommand object
///
///
///
///
///
///
Private static void PrepareCommand (SqlConnection conn, SqlCommand comm, CommandType commType, string commText, SqlParameter [] commParam)
{
//whether the database connection open
If (conn. State!=ConnectionState. Open)
{
//open the database connection
conn.Open();
}
//set the SqlCommand connection
Comm. Connection=conn;
//set the SqlCommand text
Comm.Com mandText=commText;
//set the SqlCommand type
Comm.Com mandType=commType;
//determine whether SqlParameter is empty
If (commParam!=null)
{
//loop populate the data
Foreach (SqlParameter param in commParam)
{
Comm. The Parameters. The Add (param);
}
}
}
///
///waiting for the execution of the stored procedure SqlCommand object
///
///
///
///
///
Private static void PrepareCommand (SqlConnection conn, SqlCommand comm, string commName, params object [] commParam)
{
//whether the database connection open
If (conn. State!=ConnectionState. Open)
{
//open the database connection
conn.Open();
}
//set the SqlCommand connection
Comm. Connection=conn;
//set the SqlCommand stored procedure name
Comm.Com mandText=commName;
//set the SqlCommand type
Comm.Com mandType=CommandType. StoredProcedure;
//get the parameters of the stored procedure
SqlCommandBuilder. DeriveParameters (comm);
//remove the Return_Value parameter
Comm. The Parameters. The RemoveAt (0);
//determine whether SqlParameter is empty
If (commParam!=null)
{
//loop assignment
for (int i=0; I & lt; Comm. The Parameters. The Count; I++)
{
Comm. The Parameters [I] Value=https://bbs.csdn.net/topics/commParam [I];
}
}
}
///
///executing SQL statements command
///
///
///
///
///
///The number of rows affected & lt;/returns>
Public static int ExecuteNonQuery (string connString, CommandType commType, string commText, params SqlParameter [] commParam)
{
//created SqlCommand command
SqlCommand comm=new SqlCommand ();
//use Using early release SqlConnection resources
Using (SqlConnection conn=new SqlConnection (connString))
{
//execution method and passing parameters
PrepareCommand (conn, comm, commType commText, commParam);
//perform operations and receive the results
Int val=comm. ExecuteNonQuery ();
//to empty SqlCommand parameter list
Comm. The Parameters. The Clear ();
//returns the
return val;
}
}
///
///execute SQL stored procedure
///note: cannot perform the output (OUT) parameters of the stored procedure
///
///
///
///
///The number of rows affected & lt;/returns>
Public static int ExecuteNonQuery (string connString, string commName, params object [] commParam)
{
//release SqlConnection use Using resources
Using (SqlConnection conn=new SqlConnection (connString))
{
//created SqlCommand command
SqlCommand comm=new SqlCommand ();
//execution method and passing parameters
PrepareCommand (conn, comm, commName commParam);
//perform operations and receive the results
Int val=comm. ExecuteNonQuery ();
//returns the
return val;
}
}
///
///executing SQL statements command
///
///
///
///
///
///SqlDataReader object & lt;/returns>
Public static SqlDataReader ExecuteReader (string connString, CommandType commType, string commText, params SqlParameter [] commParam)
{
//create a database connection
SqlConnection conn=new SqlConnection (connString);
Try
{
//created SqlCommand command
SqlCommand comm=new SqlCommand ();
//execution method and passing parameters
PrepareCommand (conn, comm, commType commText, commParam);
//create a SqlDataReader receiving data
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull