Asp.net data access layer base class

Source: Internet
Author: User

PartCode:

Copy code The 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.