62,046
社区成员
发帖
与我相关
我的任务
分享
public class Text_Info : Sql.AccDB
{
/// <summary>
/// 数据列表
/// </summary>
public DataTable List(out int result)
{
srcTblName = "tmp"; //临时表名,随意
StartIn = 1; //取数起始
EndSize = 100; //取多少条
result = 0;
strSQL = Sql.SqlUtil.Text_Info.SqlStr_List; //SQL语句
ReturnDt(); //执行
//计算数据总数并返回
strSQL = Sql.SqlUtil.Text_Info.SqlStr_List_Count; //数据集总数
srcTblName = "";
ReturnDt();
if(dtReturn.Rows.Count>0)
result = (int)dtReturn.Rows[0][0];
return dsReturn.Tables[0]; //返回数据集
}
}
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web.UI;
namespace Sql
{
/// <summary>
/// Access数据操作底类
/// </summary>
public class AccDB
{
OleDbConnection oleConn = null;
DataTable dt;
DataSet ds;
string Ers;
int resultInt;
string ConString = "DB";//web.config中相应数据库连接字符串.
string strsql = "";
int Start = 0;
int MaxSize = 0;
string TblName = "";
string Properties="";
string strPath;
protected void Run()
{
using(oleConn = oleConns())
{
try
{
oleConn.Open();
//执行
OleDbCommand cmd = new OleDbCommand(strsql ,oleConn);
resultInt = cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch(Exception ex)
{
Ers = ex.Message.ToString();
#if DEBUG
throw new InvalidOperationException("DEBUG SQL执行出错:"+strsql+"\\n"+ex.Message.ToString());
#else
Log.CShowError.ShowError("SQL执行出错:"+strsql+"\\n"+ex.Message.ToString());
#endif
}
finally
{
oleConn.Close();
oleConn.Dispose();
}
}
}
public void ReturnDt()
{
using(oleConn = oleConns())
{
try
{
oleConn.Open();
//查询
OleDbDataAdapter myDapter = new OleDbDataAdapter(strsql ,oleConn);
if(TblName=="")
{
dt = new DataTable();
resultInt = myDapter.Fill(dt);
}
else
{
ds = new DataSet();
resultInt = myDapter.Fill(ds,Start,MaxSize,TblName);
}
}
catch(Exception ex)
{
Ers = ex.Message.ToString();
#if DEBUG
throw new InvalidOperationException("DEBUG SQL执行出错:"+strsql+"\\n"+ex.Message.ToString());
#else
Log.CShowError.ShowError("SQL执行出错:"+strsql+"\\n"+ex.Message.ToString());
#endif
}
finally
{
oleConn.Close();
oleConn.Dispose();
}
}
}
private string Paths()
{
System.Web.UI.Page p = new System.Web.UI.Page();
return p.Server.MapPath(ConfigurationSettings.AppSettings[ConString]);
}
private OleDbConnection oleConns()
{
if(strPath==null)
strPath = Paths();
return new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+strPath+";"+Properties);
}
#region 提供外部访问域成员并设置属性
#region 只读属性域成员
/// <summary>
/// 返回数据集DataTable [只读]
/// </summary>
public DataTable dtReturn
{
get
{
return dt;
}
}
/// <summary>
/// 返回分页数据集DataSet [只读]
/// </summary>
public DataSet dsReturn
{
get
{
return ds;
}
}
/// <summary>
/// 返回错误信息 [只读]
/// </summary>
public string strErs
{
get
{
return Ers;
}
}
/// <summary>
/// 返回操作结果集数 [只读]
/// </summary>
public int result
{
get
{
return resultInt;
}
}
#endregion
#region 只写属性域成员
/// <summary>
/// 数据库联接文件 [只写]
/// </summary>
public string ConnString
{
set
{
ConString = value;
}
}
/// <summary>
/// SQL执行语句 [只写]
/// </summary>
public string strSQL
{
set
{
strsql = value;
}
}
/// <summary>
/// 分页参数:起始数 [只写]
/// </summary>
public int StartIn
{
set
{
Start = value;
}
}
/// <summary>
/// 分页参数:结束数 [只写]
/// </summary>
public int EndSize
{
set
{
MaxSize = value;
}
}
/// <summary>
/// 驱动格式,默认为MDB文件驱动。
/// 如果要驱动ExCle文件,请载入 Extended Properties=Excel 8.0;
/// </summary>
public string strProperties
{
set
{
Properties = value;
}
}
/// <summary>
/// 文件路径,如果不传则按照WebConFig中ConString默认配置来走
/// 如果传入文件路径参数则严格按照传入路径来读取
/// </summary>
public string strFilePath
{
set
{
strPath = value;
}
}
#endregion
#region 读写属性域成员
/// <summary>
/// 分页参数: DataSet 临时表名 [读写]
/// </summary>
public string srcTblName
{
get
{
return TblName;
}
set
{
TblName = value;
}
}
#endregion
#endregion
}
}