Asp.net data access layer base class

Source: Internet
Author: User

Some code:
Copy codeThe Code is as follows:
Using System;
Using System. Collections;
Using System. Collections. Specialized;
Using System. Data;
Using System. Data. SqlClient;
Using System. Configuration;
Using System. Data. Common;
Using System. Collections. Generic;

Namespace sosuo8.DBUtility
{

Public abstract class DbHelperSQL
{
// Database connection string

Public static string connectionString = ConfigurationManager. ConnectionStrings ["Conn"]. ToString ();


Public static SqlConnection conn = new SqlConnection (connectionString );
Public DbHelperSQL ()
{
}

# Region Public Method
/// <Summary>
/// Determine whether a field exists in a table
/// </Summary>
/// <Param name = "tableName"> table name </param>
/// <Param name = "columnName"> column name </param>
/// <Returns> existence </returns>
Public static bool ColumnExists (string tableName, string columnName)
{
String SQL = "select count (1) from syscolumns where [id] = object_id ('" + tableName + "') and [name] = '" + columnName + "'";
Object res = GetSingle (SQL );
If (res = null)
{
Return false;
}
Return Convert. ToInt32 (res)> 0;
}
Public static int GetMaxID (string FieldName, string TableName, string wherestr)
{
String strsql = "select max (" + FieldName + ") + 1 from" + TableName;
If (wherestr! = String. Empty)
{
Strsql + = "where" + wherestr;
}
Object obj = DbHelperSQL. GetSingle (strsql );
If (obj = null)
{
Return 1;
}
Else
{
Return int. Parse (obj. ToString ());
}
}
Public static bool Exists (string strSql)
{
Object obj = DbHelperSQL. 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;
}
}
/// <Summary>
/// Whether the table exists
/// </Summary>
/// <Param name = "TableName"> </param>
/// <Returns> </returns>
Public static bool TabExists (string TableName)
{
String strsql = "select count (*) from sysobjects where id = object_id (n' [" + TableName + "] ') and OBJECTPROPERTY (id, n'isusertable ') = 1 ";
// String strsql = "SELECT count (*) FROM sys. objects WHERE object_id = OBJECT_ID (n' [dbo]. ["+ TableName +"] ') AND type in (n'u ')";
Object obj = DbHelperSQL. 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;
}
}
}

Package and download the script

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.