个人整理完善中...
下载提供 download
using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Xml;
using System.IO;
using System.Web.UI.WebControls;
namespace lyapp
{
#region OperatorAcc类 对Access数据库的操作类
/// <summary>
/// 定义静态方法:ExecuteDataSet()||ExecuteDataTable()||ExecuteNonQuery()||ExecuteScalar()
/// 功能强大的Reader()
/// </summary>
public class OperatorAcc : Page
{
// Statics
public static OleDbConnection ConnAcc;
public static OleDbDataReader dr;
public static string DataAcc;
public static string DataAccConStr;
// Constructors
static OperatorAcc ()
{
//OperatorDataLink由Web.config添加<add key="OperatorDataLink" value="News.mdb"/>
OperatorAcc.DataAcc = ConfigurationSettings.AppSettings["OperatorDataLink"];
DataAccConStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath(OperatorAcc.DataAcc) + ";";
}
public OperatorAcc()
{
}
// Methods
public static void Open ()
{
OperatorAcc.ConnAcc = new OleDbConnection(DataAccConStr);
if (OperatorAcc.ConnAcc.State == ConnectionState.Closed)
{
OperatorAcc.ConnAcc.Open();
}
}
public static void Close ()
{
OperatorAcc.ConnAcc.Close();
OperatorAcc.ConnAcc.Dispose();
OperatorAcc.ConnAcc = null;
}
public static DataSet ExecuteDataSet (string sql, int startindex, int num, string dataname)
{
OperatorAcc.Open();
DataSet dataSet = new DataSet();
new OleDbDataAdapter(sql, OperatorAcc.ConnAcc).Fill(dataSet, startindex, num, dataname);
OperatorAcc.Close();
return dataSet;
}
public static DataSet ExecuteDataSet (string sql)
{
OperatorAcc.Open();
DataSet dataSet = new DataSet();
new OleDbDataAdapter(sql, OperatorAcc.ConnAcc).Fill(dataSet);
OperatorAcc.Close();
return dataSet;
}
public static DataTable ExecuteDataTable (string sql)
{
OperatorAcc.Open();
DataTable dt = new DataTable();
new OleDbDataAdapter(sql, OperatorAcc.ConnAcc).Fill(dt);
OperatorAcc.Close();
return dt;
}
public static void ExecuteNonQuery (string sql)
{
OperatorAcc.Open();
new OleDbCommand(sql, OperatorAcc.ConnAcc).ExecuteNonQuery();
OperatorAcc.Close();
}
public static object ExecuteScalar (string SQL, int i)
{
OperatorAcc.Open();
OleDbCommand command1 = new OleDbCommand(SQL, OperatorAcc.ConnAcc);
if (i == 1)
{
int num1 = (int) command1.ExecuteScalar();
OperatorAcc.Close();
return num1;
}
double num2 = (double) command1.ExecuteScalar();
OperatorAcc.Close();
return num2;
}
#region Reader方法 以一行一行的形式返回sql语句 ||可以指定返回的行数,列数,也可以是全部行||
public static string [,] Reader(string sql,int num,int filedsCount,string [] filedsNames)
{
OperatorAcc.Open();
OleDbCommand cmd=new OleDbCommand(sql,OperatorAcc.ConnAcc);
dr=cmd.ExecuteReader();
int length=dr.FieldCount;
string [,] aa;
if (num!=0)
{
aa=new string[num,filedsCount];
}
else
{
aa=new string[2,filedsCount];
}
int i=0,j=0;
if(i!=num)
{
while (dr.Read()&&i<num)
{
while (j<filedsNames.Length)
{
aa[i,j]=dr[filedsNames[j]].ToString();
j++;
}
j=0;
i++;
}
}
else
{
while (dr.Read())
{
while (j<filedsNames.Length)
{
if(i>=aa.GetLength(0))
Add(ref aa);
aa[i,j]=dr[filedsNames[j]].ToString();
j++;
}
j=0;
i++;
}
}
dr.Close();
OperatorAcc.Close();
return aa;
}
public static void Add(ref string[,] myStrings)
{
int rowCount = myStrings.GetLength(0);
int colCount = myStrings.GetLength(1);
string[,] temp = new string[rowCount+1, colCount];
for(int i=0;i<rowCount;i++)
for(int j=0;j<colCount;j++)
temp[i,j]=myStrings[i,j];
myStrings=temp;
}
#endregion
}
#endregion
#region Access类 实现对Access数据库实例的操作
/// <summary>
/// 公共方法:Create()||Compact()||Backup()||Recover()
/// ||调用||创建实例:命名空间.类名.方法|| lyapp.Access.Back()
/// </summary>
public class Access:Page
{
///根据指定的文件名称创建ACCESS数据库
///mdbPath:要创件的ACCESS绝对路径
public void Create( string mdbPath )
{
if(File.Exists(mdbPath) ) //检查数据库是否已存在
{
throw new Exception("目标数据库已存在,无法创建");
}
// 可以加上密码,这样创建后的数据库必须输入密码后才能打开
mdbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
// 创建一个CatalogClass对象的实例,
ADOX.CatalogClass cat = new ADOX.CatalogClass();
// 使用CatalogClass对象的Create方法创建ACCESS数据库
cat.Create(mdbPath);
}
///压缩修复ACCESS数据库,mdbPath为数据库绝对路径
public void Compact( string mdbPath )
{
if( !File.Exists(mdbPath) ) //检查数据库是否已存在
{
throw new Exception("目标数据库不存在,无法压缩");
}
//声明临时数据库的名称
string temp = DateTime.Now.Year.ToString();
temp += DateTime.Now.Month.ToString();
temp += DateTime.Now.Day.ToString();
temp += DateTime.Now.Hour.ToString();
temp += DateTime.Now.Minute.ToString();
temp += DateTime.Now.Second.ToString() + ".bak";
temp = mdbPath.Substring(0, mdbPath.LastIndexOf("\\")+1) + temp;
//定义临时数据库的连接字符串
string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp;
//定义目标数据库的连接字符串
string mdbPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
//创建一个JetEngineClass对象的实例
JRO.JetEngineClass jt = new JRO.JetEngineClass();
//使用JetEngineClass对象的CompactDatabase方法压缩修复数据库
jt.CompactDatabase( mdbPath2, temp2 );
//拷贝临时数据库到目标数据库(覆盖)
File.Copy( temp, mdbPath, true );
//最后删除临时数据库
File.Delete( temp );
}
/// 备份数据库,mdb1,源数据库绝对路径; mdb2: 目标数据库绝对路径
public void Backup( string mdb1, string mdb2 )
{
if( !File.Exists(mdb1) )
{
throw new Exception("源数据库不存在");
}
try
{
File.Copy( mdb1, mdb2, true );
}
catch( IOException ixp )
{
throw new Exception(ixp.ToString());
}
}
///恢复数据库,mdb1为备份数据库绝对路径,mdb2为当前数据库绝对路径
public void Recover( string mdb1, string mdb2 )
{
if( !File.Exists(mdb1) )
{
//throw new Exception("备份数据库不存在");
Response.Write("<script language='javascript'>alert('备份数据库不存在!');</script>");
}
try
{
File.Copy( mdb1, mdb2, true );
}
catch( IOException ixp )
{
throw new Exception(ixp.ToString());
}
}
}
#endregion
public class OOP:Page
{
private void GetPictureNames(DropDownList lbxPictures) //using System.Web.UI.WebControls;
{
string [] pictureNameList;
string pictureDir = Server.MapPath("images");
pictureNameList = Directory.GetFiles(pictureDir);
lbxPictures.Items.Clear();
foreach (string currentFileName in pictureNameList)
{
//Response.Write(currentFileName.ToString());
lbxPictures.Items.Add(currentFileName.Substring(currentFileName.LastIndexOf("\\") + 1));
}
}
}
}