欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 数据访问层基类

程序员文章站 2024-03-31 20:34:28
部分代码:复制代码 代码如下:using system; using system.collections; using system.collections.specia...

部分代码:

复制代码 代码如下:

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
{
//数据库连接字符串

public static string connectionstring = configurationmanager.connectionstrings["conn"].tostring();


public static sqlconnection conn = new sqlconnection(connectionstring);
public dbhelpersql()
{
}

#region 公用方法
/// <summary>
/// 判断是否存在某表的某个字段
/// </summary>
/// <param name="tablename">表名称</param>
/// <param name="columnname">列名称</param>
/// <returns>是否存在</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>
/// 表是否存在
/// </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;
}
}
}

打包下载