sqlHelper --- reprint

// introduced namespace 
the using the System.Data.SqlClient; 

///  <Summary> 
/// the SqlHelper summary description
 ///  </ Summary> 
public  class the SqlHelper 
{ 
    Private  static  Readonly  String sqlcon ConfigurationManager.ConnectionStrings = [ " enterpriseCon " ] .ConnectionString; 

    // establish a connection method 
    public the SqlConnection createCon () 
    { 
        the SqlConnection CON = new new the SqlConnection (sqlcon);
         IF (! = con.State ConnectionState.Open) 
            con.Open ();
        return CON; 
    } 
    // prepare command command 
    public  static  void prepareCommand (the SqlConnection CON, the SqlCommand cmd, the SqlTransaction Trans, the CommandType cmdType, String cmdText The, the SqlParameter [] para) 
    { 
        // If the database connection is not open then open the connection 
        IF (con.State ! = ConnectionState.Open) 
        { 
            con.Open (); 
        } 
        cmd.Connection = CON; 
        cmd.CommandText = cmdText the;
         IF ! (trans = null ) // if present, perform the following trans command 
         {
            cmd.Transaction= Trans; 
        } 
        cmd.CommandText = cmdText The;
         IF (para =! Null ) // if not para para traversed again empty 
        {
             the foreach (the SqlParameter PA in para) 
            { 
                cmd.Parameters.Add (PA); 
            } 
        } 
    } 

    // reading data from the source database and returns a SqlDataReader object 
    public  static SqlDataReader ExecuteReader ( String cmdText the, the CommandType cmdType, the SqlParameter [] para) 
    { 
        // definition of connecting
        CON = the SqlConnection new newThe SqlConnection (sqlcon);
         // catch exceptions may occur in 
        the try 
        { 
            the SqlCommand cmd = new new the SqlCommand (); 
            prepareCommand (CON, cmd, null , cmdType, cmdText The, para); 

            the SqlDataReader SDR = cmd.ExecuteReader (the CommandBehavior.CloseConnection); 
            cmd .Parameters.Clear (); 
            return SDR; 
        } 
        the catch (Exception) 
        { 
            the throw ; 
        } 
    } 

    // update database methods (addition, deletion) 
    public  static  int ExecuteNonQuery ( String cmdText, CommandType cmdType, SqlParameter[] para)
    {
        using (SqlConnection con = new SqlConnection(sqlcon))
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                prepareCommand(con, cmd, null, cmdType, cmdText, para);
                int result = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

    //取得数据库中一行的值
    public static string executeScalar(string cmdText, CommandType cmdType, SqlParameter[] para)
    {
        using (SqlConnection con = new SqlConnection(sqlcon))
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                prepareCommand(con, cmd, null, cmdType, cmdText, para);
                string result = Convert.ToString(cmd.ExecuteScalar());
                cmd.Parameters.Clear();
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }

    //DataSet
    public static DataSet executeDataAdapter(string cmdText,CommandType cmdType,SqlParameter[] para) {
        using (SqlConnection con=new SqlConnection(sqlcon))
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                prepareCommand(con, cmd, null, cmdType, cmdText, para);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                cmd.Parameters.Clear();
                return ds;
            }
            catch (Exception)
            {
                throw;
            }
        }    
    }
}

 

Guess you like

Origin www.cnblogs.com/bedfly/p/12465801.html