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