Database generic category, database generic

Source: Internet
Author: User

Database generic category, database generic

Private static string connString = ConfigurationManager. ConnStrings ["connString"]. ToString (); // Add the corresponding database connection configuration file in app. config.

1. Format SQL statements

// Add, delete, modify, and query

Public static int Update (string SQL)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand (SQL, conn );

Try

{

Conn. Open ();

Return cmd. ExecuteNonQuery ();

}

Catch (Exception ex)

{

Throw ex;

}

Finally

{

Conn. Close ();

}

}

// Query a single result

Public static object GetSingleResult (string SQL)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand (SQL, conn );

Try

{

Conn. Open ();

Return cmd. ExecuteScalar ();

}

Catch (Exception ex)

{

Throw ex;

}

Finally

{

Conn. Close ();

}

}

// Read-only result set Query

Public static SqlDataReader GetReader (string SQL)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand (SQL, conn );

Try

{

Conn. Open ();

Return cmd. ExecuteReader (CommandBehavior. CloseConnection );

}

Catch (Exception ex)

{

Conn. Close ();

Throw ex;

}

}

// DataSet Query

Public static DataSet GetDataSet (string SQL)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand (SQL, conn );

DataSet ds = new DataSet ();

SqlDataAdapter da = new SqlDataAdapter ();

Try

{

Conn. Open ();

Da. Fill (ds );

Return ds;

}

Catch (Exception ex)

{

Throw ex;

}

Finally

{

Conn. Close ();

}

}

2. SQL statements with Parameters

// Add, delete, modify, and query

Public static int Update (string SQL, SqlParameter [] param)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand (SQL, conn );

Try

{

Conn. Open ();

Cmd. Parameters. AddRange (param );

Return cmd. ExecuteNonQuery ();

}

Catch (Exception ex)

{

Throw ex;

}

Finally

{

Conn. Close ();

}

}

// Query a single result

Public static object GetSingleResult (string SQL, SqlParameter [] param)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand (SQL, conn );

Try

{

Conn. Open ();

Cmd. Parameters. AddRange (param );

Retrurn cmd. ExecuteScalar ();

}

Catch (Exception ex)

{

Throw ex;

}

Finally

{

Conn. Close ();

}

}

// Query the read-only result set

Public static SqlDataReader GetReader (string SQL, SqlParameter [] param)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand (SQL, conn );

Try

{

Conn. Open ();

Cmd. Parameters. AddRange (param );

Return cmd. ExecuteReader (CommandBehavior. CloseConnection );

}

Catch (Exception ex)

{

Conn. Close ();

Throw ex;

}

}

3. Stored Procedures with Parameters

// Add, delete, modify, and query

Public static int UpdateByProcedure (string procedureName, SqlParameter [] param)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand ();

Cmd. Connection = conn;

Try

{

Conn. Open ();

Cmd. CommandType = CommandType. StoredProcedure;

Cmd. CommandText = procedureName;

Cmd. Parameters. AddRange (param );

Return cmd. ExecuteNonQuery ();

}

Catch (Exception ex)

{

Throw ex;

}

Finally

{

Conn. Close ();

}

}

// Query a single result

Public static object GetSingleResultByProcedure (string procedureName, SqlParameter [] param)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand ();

Cmd. Connection = conn;

Try

{

Conn. Open ();

Cmd. CommandType = CommandType. StoredProcedure;

Cmd. CommandText = procedureName;

Cmd. Parameters. AddRange (param );

Retrurn cmd. ExecuteScalar ();

}

Catch (Exception ex)

{

Throw ex;

}

Finally

{

Conn. Close ();

}

}

// Query the read-only result set

Public static SqlDataReader GetReaderByProcedure (string procedureName, SqlParameter [] param)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand ();

Cmd. Connection = conn;

Try

{

Conn. Open ();

Cmd. CommandType = CommandType. StoredProcedure;

Cmd. CommandText = procedureName;

Cmd. Parameters. AddRange (param );

Return cmd. ExecuteReader (CommandBehavior. CloseConnection );

}

Catch (Exception ex)

{

Conn. Close ();

Throw ex;

}

}

4. Use transaction update

Public static bool UpdateByTracsaction (List <string> sqlList)

{

SqlConnection conn = new SqlConnection (connString );

SqlCommand cmd = new SqlCommand ();

Cmd. Connection = conn;

Try

{

Conn. Open ();

Cmd. Transaction = conn. BeginTransaction (); // start the Transaction

Foreach (string SQL in sqlList)

{

Cmd. CommandText = SQL;

Cmd. ExecuteNonQuery ();

}

Cmd. Transaction. Commit (); // submit the Transaction

Return true;

}

Catch (Exception ex)

{

If (cmd. Transaction! = Null)

{

Cmd. Transaction. Rollback (); // roll back the Transaction

}

Throw ex;

}

Finally

{

If (cmd. Transaction! = Null)

{

Cmd. Transaction = null; // clear the Transaction

}

Conn. Close ();

}

}

Related Article

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.