SQL Server database universal access class

Source: Internet
Author: User

private static string connstring=configurationmanager.connstrings["ConnString"]. ToString (); Add the appropriate database connection profile to the configuration in app. Config

1. Formatting SQL statements

Perform additions and deletions and change the search

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 ();

}

}

Single result query

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 Data Set 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

Perform additions and deletions and change the search

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 ();

}

}

Querying single results

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 ();

}

}

Querying a 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

Perform additions and deletions and change the search

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 ();

}

}

Querying single results

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 ();

}

}

Querying a 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. Using Transaction Updates

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 ();//Open transaction

foreach (String sql in Sqllist)

{

Cmd.commandtext=sql;

Cmd. ExecuteNonQuery ();

}

Cmd.Transaction.Commit ();//Commit a transaction

return true;

}

catch (Exception ex)

{

if (cmd. Transaction!=null)

{

Cmd. Transaction.rollback ();//ROLLBACK TRANSACTION

}

Throw ex;

}

Finally

{

if (cmd. Transaction!=null)

{

Cmd. transaction=null;//emptying transactions

}

Conn. Close ();

}

}

SQL Server database universal access class

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.