A SqlHelper class

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Data. SqlClient;
Using System. Collections;
Using System. Data;
Using System. Configuration;
Using System. Web;
/// <Summary>
/// Summary of SqlHelper
/// </Summary>
Public class SqlHelper
{
Private static readonly string connectionString = ConfigurationManager. ConnectionStrings ["DataBaseString"]. ConnectionString;
Public SqlHelper ()
{
}
 
# Region Public Method
Public static int GetMaxID (string FieldName, string TableName)
{
String strsql = "select max (" + FieldName + ") + 1 from" + TableName;
Object obj = SqlHelper. GetSingle (strsql );
If (obj = null)
{
Return 1;
}
Else
{
Return int. Parse (obj. ToString ());
}
}

Public static bool Exists (string strSql)
{
Object obj = SqlHelper. GetSingle (strSql );
Int cmdresult;
If (Object. Equals (obj, null) | (Object. Equals (obj, System. DBNull. Value )))
{
Cmdresult = 0;
}
Else
{
Cmdresult = int. Parse (obj. ToString ());
}
If (cmdresult = 0)
{
Return false;
}
Else
{
Return true;
}
}

Public static bool Exists (string strSql, params SqlParameter [] partition parms)
{
Object obj = SqlHelper. GetSingle (strSql, limit parms );
Int cmdresult;
If (Object. Equals (obj, null) | (Object. Equals (obj, System. DBNull. Value )))
{
Cmdresult = 0;
}
Else
{
Cmdresult = int. Parse (obj. ToString ());
}
If (cmdresult = 0)
{
Return false;
}
Else
{
Return true;
}
}
# Endregion
 
# Region execute simple SQL statements
/// <Summary>
/// Execute the SQL statement and return the number of affected records
/// </Summary>
/// <Param name = "SQLString"> SQL statement </param>
/// <Returns> Number of affected records </returns>
Public static int ExecuteSql (string SQLString)
{
Using (SqlConnection connection = new SqlConnection (connectionString ))
{
Using (SqlCommand cmd = new SqlCommand (SQLString, connection ))
{
Try
{
Connection. Open ();
Int rows = cmd. ExecuteNonQuery ();
Return rows;
}
Catch (System. Data. SqlClient. SqlException E)
{
Connection. Close ();
Throw new Exception (E. Message );
}
}
}
}

/// <Summary>

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.