C # Operational Database SqlHelper class

Source: Internet
Author: User

/* Creator: Kitchen knife, the blog of the layman
* Date Created: September 01, 2014
* Function: SqlHelper class, Operation database
*
*/

Namespace Net.String.ConsoleApplication
{
Using System;
Using System.Data;
Using System.Data.SqlClient;

public sealed class SqlHelper
{
Private SqlHelper () {}

        public static int ExecuteNonQuery ( String connectionString, CommandType cmdtype, String cmdtext, params sqlparameter[] commandparameters)
         {
            SqlCommand cmd = new SqlCommand ();

            using ( SqlConnection conn = new SqlConnection (connectionString))
             {
                 PrepareCommand (CMD, conn, null, Cmdtype, Cmdtext, commandparameters);
                int val = cmd. ExecuteNonQuery ();
                cmd. Parameters.clear ();
                return Val;
           }
       }

public static int ExecuteNonQuery (SqlConnection connection, CommandType Cmdtype, String cmdtext, params sqlparameter[] Co Mmandparameters)
{
SqlCommand cmd = new SqlCommand ();
Try
{
PrepareCommand (cmd, connection, NULL, Cmdtype, Cmdtext, commandparameters);
int val = cmd. ExecuteNonQuery ();
Cmd. Parameters.clear ();
return Val;
}
catch (Exception ex)
{
Ex. Data.add ("SQL", Cmdtext);
Throw ex;
}
Finally
{
if (connection. State = connectionstate.closed)
{
Connection. Close ();
}
}
}

        public static int ExecuteNonQuery ( SqlTransaction Trans, CommandType Cmdtype, String cmdtext, params sqlparameter[] commandparameters)
         {
            SqlCommand cmd = new SqlCommand ();
            PrepareCommand (cmd, trans. Connection, trans, Cmdtype, Cmdtext, commandparameters);
            int val = cmd. ExecuteNonQuery ();
            cmd. Parameters.clear ();
            return Val;
       }

public static SqlDataReader ExecuteReader (String connectionString, CommandType cmdtype, string cmdtext, params Sqlparameter[] commandparameters)
{
SqlCommand cmd = new SqlCommand ();
SqlConnection conn = new SqlConnection (connectionString);
Try
{
PrepareCommand (CMD, conn, null, Cmdtype, Cmdtext, commandparameters);
SqlDataReader rdr = cmd. ExecuteReader (commandbehavior.closeconnection);
Cmd. Parameters.clear ();
return RDR;
}
Catch
{
Conn. Close ();
Throw
}
}

public static Object ExecuteScalar (String connectionString, CommandType cmdtype, String cmdtext, params sqlparameter[] Co Mmandparameters)
{
SqlCommand cmd = new SqlCommand ();

            using ( SqlConnection connection = new SqlConnection (connectionString))
             {
                 PrepareCommand (cmd, connection, NULL, Cmdtype, Cmdtext, commandparameters);
                Object val = cmd. ExecuteScalar ();
                cmd. Parameters.clear ();
                return Val;
           }
       }

public static object ExecuteScalar (SqlConnection connection, CommandType Cmdtype, String cmdtext, params sqlparameter[] C Ommandparameters)
{
SqlCommand cmd = new SqlCommand ();

PrepareCommand (cmd, connection, NULL, Cmdtype, Cmdtext, commandparameters);
Object val = cmd. ExecuteScalar ();
Cmd. Parameters.clear ();
return Val;
}

public static SqlDataReader ExecuteReader (String connectionString, String strSQL)
{
SqlConnection connection = new SqlConnection (connectionString);
SqlCommand cmd = new SqlCommand (strSQL, connection);
Try
{
Connection. Open ();
SqlDataReader myreader = cmd. ExecuteReader (commandbehavior.closeconnection);
return myreader;
}
catch (System.Data.SqlClient.SqlException e)
{
Throw e;
}
}

        public static DataTable Filldata (string ConnectionString, String strSQL)
        {
             DataTable dt = new DataTable ();
            SqlConnection connection = new SqlConnection (connectionString);
            SqlDataAdapter dr = New SqlDataAdapter ( strSQL, connection);
            Dr. Fill (DT);
            return DT;
       }

private static void PrepareCommand (SqlCommand cmd, SqlConnection conn, SqlTransaction Trans, CommandType cmdtype, string C Mdtext, sqlparameter[] cmdparms)
{
IF (Conn. State = ConnectionState.Open)
Conn. Open ();

Cmd. Connection = conn;
Cmd.commandtext = Cmdtext;

if (trans! = null)
Cmd. Transaction = trans;

Cmd.commandtype = Cmdtype;

if (cmdparms! = null)
{
foreach (SqlParameter parm in cmdparms)
Cmd. Parameters.Add (Parm);
}
}
}
}

C # Operational Database SqlHelper class

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.