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