ASP.NET封装的数据库访问基类

王朝asp·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

namespace Db

{

/// <summary>

/// Base 的摘要说明。

/// </summary>

public class Base

{

public Base()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

protected static SqlConnection conn =new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);

protected static SqlCommand cmd = new SqlCommand(strSp,conn);

protected static SqlDataAdapter da = new SqlDataAdapter();

protected static DataSet ds = new DataSet();

protected static DataView dv = new DataView();

protected static SqlDataReader dr;

protected static SqlParameter[] prams;

protected static string strSp;

protected static SqlDataReader drSelectAll(string strSp)

{

cmd.CommandType = CommandType.StoredProcedure;

conn.Open();

dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

return dr;

}//返回一个SqlDataReader

protected static DataSet dsSelectAll(string strSp)

{

da.SelectCommand = new SqlCommand(strSp,conn);

da.SelectCommand.CommandType = CommandType.StoredProcedure;

da.Fill(ds);

conn.Open();

try

{

da.SelectCommand.ExecuteNonQuery();

return ds;

}

catch(Exception ex)

{

throw new Exception(ex.Message);

}

finally

{

conn.Dispose();

conn.Close();

}

}//返回一个SqlDataSet

protected static DataView dvSelectAll(string strSp)

{

cmd.CommandType = CommandType.StoredProcedure;

da.SelectCommand = new SqlCommand(strSp,conn);

da.Fill(ds);

conn.Open();

try

{

da.SelectCommand.ExecuteNonQuery();

dv = ds.Tables[0].DefaultView;

return dv;

}

catch(Exception ex)

{

throw new Exception(ex.Message);

}

finally

{

conn.Dispose();

conn.Close();

}

}//返回一个DataView

protected static string strCmd(string strSp,SqlParameter[] prams,SqlDataReader dr)

{

CreateCmd(strSp,prams,dr);

conn.Open();

try

{

cmd.ExecuteNonQuery();

return "1";

}

catch(Exception ex)

{

throw new Exception(ex.Message);

}

finally

{

conn.Dispose();

conn.Close();

}

}//返回一个数据库操作

protected static SqlCommand CreateCmd(string strSp, SqlParameter[] prams,SqlDataReader dr)

{

cmd.CommandType = CommandType.StoredProcedure;

if (prams != null)

{

foreach (SqlParameter parameter in prams)

cmd.Parameters.Add(parameter);

}

cmd.Parameters.Add(

new SqlParameter("ReturnValue", SqlDbType.Int, 4,

ParameterDirection.ReturnValue, false, 0, 0,

string.Empty, DataRowVersion.Default, null));

return cmd;

}//返回带参数的命令

protected static bool ExecuteSQLs()

{

cmd.CommandType = CommandType.StoredProcedure;

conn.Open();

try

{

int i = (int)cmd.ExecuteScalar();

if(i>0)

{

return true;

}

else

{

return false;

}

}

catch(Exception ex)

{

throw new Exception(ex.Message);

}

finally

{

conn.Dispose();

conn.Close();

}

}//返回第一行的数据操作

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航