📄 folder.cs
字号:
using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Windows.Forms;
namespace DataAccessLayer
{
/// <summary>
///文件夹节点实体类。
/// </summary>
public class Folder
{
public int FolderID = 0;
//用于区分是否已更改过,保存之后,由外部对象重置为False
public bool HasChanged = false;
private string _text = "";
public string Text
{
get
{
return this._text;
}
set
{
if(this._text != value)
{
this._text = value;
this.HasChanged = true;
}
}
}
private string _rtf = "";
public string RTFText
{
get
{
return this._rtf;
}
set
{
if(this._rtf != value)
{
this._rtf = value;
this.HasChanged = true;
}
}
}
private string _belongto = "";
public string BelongTo
{
get
{
return this._belongto;
}
set
{
if(value.Length > 255)
{
value = value.Substring(0, 255);
}
if(value == string.Empty)
{
throw new Exception("节点的路径不能为空");
}
//有可能出错,检查是否路径以"\"结束
if(value.Substring(value.Length - 1, 1) != "\\")
{
value = value + "\\";
}
//----------------------------------------
if(this._belongto != value)
{
this._belongto = value;
this.HasChanged = true;
}
}
}
public DateTime InputDate = DateTime.Today;
//所包含之文件对象(Attachment)清单
public ArrayList AttachFiles = new ArrayList();
}
public class FolderAccessObj : BaseAccessObj
{
private AttachFileAccessObj FileSaver;
public FolderAccessObj(OLEDBAccessObj dbObj) : base(dbObj)
{
this.FileSaver = new AttachFileAccessObj(dbObj);
}
/// <summary>
/// 将自身数据存入数据库中,不用保存文件,因为已经在创建文件时保存了
/// </summary>
/// <param name="obj"></param>
public void SaveMeToDB(Folder obj)
{
if(this.dbObj == null)
return;
string strSQL;
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
Attachment fileObj;
int folderFileID;
if(obj.BelongTo.Substring(obj.BelongTo.Length - 1, 1) != "\\")
{
obj.BelongTo = obj.BelongTo + "\\";
}
try
{
//保存主记录
strSQL = "AddFolderRecord";
pars.Add("FolderIDValue", obj.FolderID);
pars.Add("TextValue", obj.Text);
pars.Add("BelongToValue", obj.BelongTo);
pars.Add("InputDateValue", obj.InputDate);
pars.Add("RTFTextValue", obj.RTFText);
this.dbObj.ExecSQLCommand2(strSQL, pars);
//如果有附件
if(obj.AttachFiles.Count > 0)
{
//插入数据库的关联表
strSQL = "AddFoderFileRecord";
for(int i=0; i < obj.AttachFiles.Count; i++)
{
fileObj = obj.AttachFiles[i] as Attachment;
pars.Clear();
//获取FolderFile表的下一个ID
folderFileID = this.dbObj.GetMaxValueFromTable("FolderFile", "FolderFileID") + 1;
pars.Add("FolderFileIDValue", folderFileID);
pars.Add("FolderIDValue", obj.FolderID);
pars.Add("FileIDValue", fileObj.FileID);
pars.Add("InputDateValue", obj.InputDate);
this.dbObj.ExecSQLCommand2(strSQL, pars);
}
}
}
catch(Exception ex)
{
throw new Exception("在执行存储过程AddFolderRecord或AddFolderFileRecord时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 更新对象所对应的数据库记录信息,不更新文件内容
/// </summary>
public void UpdateDBRow(Folder obj, bool FileListNotChange)
{
if(obj == null)
return;
string strSQL;
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
Attachment fileObj;
int folderFileID;
try
{
strSQL = "UpdateFolderRecord";
if(obj.Text == null)
obj.Text = "";
//有可能出错,检查是否路径以"\"结束
if(obj.BelongTo.Substring(obj.BelongTo.Length - 1, 1) != "\\")
{
obj.BelongTo = obj.BelongTo + "\\";
}
//更新主记录
pars.Add("TextValue", obj.Text);
pars.Add("BelongToValue", obj.BelongTo);
pars.Add("InputDateValue", obj.InputDate);
pars.Add("RTFTextValue", obj.RTFText);
pars.Add("FolderIDValue", obj.FolderID);
this.dbObj.ExecSQLCommand2(strSQL, pars);
//更新关联表记录
//如果文件列表有改变
if(FileListNotChange == false)
{
//先删除所有的相关关联表记录
strSQL = "Delete * from FolderFile where FolderFileID=" + obj.FolderID;
this.dbObj.ExecSQLCommand(strSQL);
//如果有附件
if(obj.AttachFiles.Count > 0)
{
//插入数据库中的关联表
strSQL = "AddFolderFileRecord";
for(int i=0; i<obj.AttachFiles.Count; i++)
{
fileObj = obj.AttachFiles[i] as Attachment;
//获取FolderFile表的下一个ID
folderFileID = this.dbObj.GetMaxValueFromTable("FolderFile", "FolderFileID") + 1;
pars.Clear();
pars.Add("FolderFileIDValue", folderFileID);
pars.Add("FolderIDValue", obj.FolderID);
pars.Add("FileIDValue", fileObj.FileID);
pars.Add("InputDateValue", obj.InputDate);
this.dbObj.ExecSQLCommand2(strSQL, pars);
}
}
}
}
catch(Exception ex)
{
throw new Exception("在执行存储过程UpdateFolderRecord时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 删除数据库记录
/// </summary>
/// <param name="BelongTo"></param>
public void DeleteDBRow(string BelongTo)
{
string strSQL;
try
{
//删除文件
strSQL = "DeleteFileOfFolderNode";
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
pars.Add("BelongValue", BelongTo + "%");
this.dbObj.ExecSQLCommand2(strSQL, pars);
//删除关联记录
strSQL = "Delete * From FolderFile where FolderFile.FolderID in (Select Folder.FolderID From Folder where Folder.BelongTo Like '" + BelongTo + "%')";
this.dbObj.ExecSQLCommand(strSQL);
//删除自身记录
strSQL = "Delete * From Folder where BelongTo Like '" + BelongTo + "%'";
this.dbObj.ExecSQLCommand(strSQL);
}
catch(Exception ex)
{
throw new Exception("在删除Folder记录时出错,系统给出的信息是:" + ex.Message);
}
}
/// <summary>
/// 给关联表增加一条记录
/// </summary>
/// <param name="FolderID"></param>
/// <param name="fileObj"></param>
public void AddFolderFileRecord(long FolderID, Attachment fileObj)
{
if(fileObj == null)
return;
if(FolderID == 0)
throw new Exception("Error:FolderID=0,In Table FolderFile, FolderID can not be zero!");
string strSQL;
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
int folderfileID;
//插入数据库中的关联表
strSQL = "AddFolderFileRecord";
//获取FolderFile表的下一个ID
folderfileID = this.dbObj.GetMaxValueFromTable("FolderFile", "FolderFileID") + 1;
try
{
pars.Add("FolderFileIDValue", folderfileID);
pars.Add("FolderIDValue", FolderID);
pars.Add("FileIDValue", fileObj.FileID);
pars.Add("InputDateValue", DateTime.Today);
this.dbObj.ExecSQLCommand2(strSQL, pars);
}
catch(Exception ex)
{
throw new Exception("在使用存储过程AddFolderFileRecord插入FolderFile记录时出错,系统给出的信息:" + ex.Message);
}
}
/// <summary>
/// 根据路径名获取对象,没有返回null,如果有附件,则自动地将对应文件对象装入ArrayList中
/// </summary>
/// <param name="strPath"></param>
/// <returns></returns>
public Folder GetObjectByPathStr(string strPath)
{
string strSQL = "Select * From Folder where BelongTo = '" + strPath + "'";
DataSet ds = this.dbObj.GetSQLDataSet(strSQL, "Folder");
DataRow dr;
Folder obj = null;
int count;
Attachment fileObj;
if(ds.Tables["Folder"].Rows.Count > 0)
{
dr = ds.Tables["Folder"].Rows[0];
obj = this.LoadFromDataRow(dr);
//装入文件对象
strSQL = "Select * From FolderFile where FolderID = " + obj.FolderID;
ds = this.dbObj.GetSQLDataSet(strSQL, "FolderFile");
count = ds.Tables["FolderFile"].Rows.Count;
if(count > 0)
{
for(int i=0; i<count; i++)
{
dr = ds.Tables["FolderFile"].Rows[i];
fileObj = this.FileSaver.LoadAttachmentObjFromID((int)dr["FileID"]);
obj.AttachFiles.Add(fileObj);
}
}
}
return obj;
}
/// <summary>
/// 用新路径名刷新老路径名,完成后,所有Folder表中以OldPathStr开头的记录最后部分用新路径NewPathStr取代
/// </summary>
/// <param name="OldPathStr"></param>
/// <param name="NewPathStr"></param>
public void RenameNodePath(string OldPathStr, string NewPathStr)
{
string strSQL = "RenameFolderNode";
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
//有可能出错,检查是否路径以"\"结束
if(NewPathStr.Substring(NewPathStr.Length - 1, 1) != "\\")
{
NewPathStr = NewPathStr + "\\";
}
if(OldPathStr.Substring(OldPathStr.Length - 1, 1) != "\\")
{
OldPathStr = OldPathStr + "\\";
}
pars.Add("NewText", NewPathStr);
pars.Add("OldText", OldPathStr);
pars.Add("FindStr", OldPathStr + "%");
try
{
this.dbObj.ExecSQLCommand2(strSQL, pars);
}
catch(Exception ex)
{
throw new Exception("在执行存储过程RenameNodePath时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 当节点移动时,所有Folder表中以OldPathStr开头的记录完全用新路径NewPathStr取代
/// </summary>
/// <param name="OldPathStr"></param>
/// <param name="NewPathStr"></param>
public void MoveNodePath(string OldPathStr, string NewPathStr)
{
//string strSQL = "MoveFolderNode";
string strSQL = "RenameFolderNode";
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
//有可能出错,检查是否路径以"\"结束
if(NewPathStr.Substring(NewPathStr.Length - 1, 1) != "\\")
{
NewPathStr = NewPathStr + "\\";
}
if(OldPathStr.Substring(OldPathStr.Length - 1, 1) != "\\")
{
OldPathStr = OldPathStr + "\\";
}
pars.Add("NewText", NewPathStr);
pars.Add("OldText", OldPathStr);
pars.Add("FindStr", OldPathStr + "%");
try
{
this.dbObj.ExecSQLCommand2(strSQL, pars);
}
catch(Exception ex)
{
throw new Exception("在执行存储过程MoveFolderNode时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 获取下一个可用的对象ID
/// </summary>
/// <returns></returns>
public int GetNextID()
{
return this.dbObj.GetMaxValueFromTable("Folder", "FolderID") + 1;
}
public void SaveToDataRow(DataRow dr, Folder obj)
{
if(dr == null || obj == null)
{
throw new Exception("Folder.SaveToDataRow need two Argument:dr and obj ,both can not be null");
}
dr["FolderID"] = obj.FolderID;
dr["BelongTo"] = obj.BelongTo;
dr["InputDate"] = obj.InputDate;
dr["Text"] = obj.Text;
dr["RTFText"] = obj.RTFText;
}
public Folder LoadFromDataRow(DataRow dr)
{
if(dr == null)
{
throw new Exception("Folder:LoadFromDataRow need an Argument:dr which can not be null");
}
Folder obj = new Folder();
obj.FolderID = (int)dr["FolderID"];
obj.BelongTo = dr["BelongTo"].ToString();
obj.InputDate = (DateTime)dr["InputDate"];
obj.Text = dr["Text"].ToString();
if(dr.IsNull("RTFText"))
{
obj.RTFText = "";
}
else
{
obj.RTFText = dr["RTFText"].ToString();
}
return obj;
}
/// <summary>
/// 保存附件
/// </summary>
/// <param name="obj"></param>
/// <param name="SaveFileName"></param>
public void SaveAttachFileToDB(Attachment obj, string SaveFileName)
{
this.FileSaver.SaveBDFileToDisk(obj, SaveFileName);
}
/// <summary>
/// 增加一个文件,将内容存入Files表,并增加一条记录
/// </summary>
/// <param name="obj"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public Attachment AddFile(Folder obj, string fileName)
{
//保存文件对象
Attachment fileObj = new Attachment();
fileObj.FileID = this.FileSaver.GetNextID();
fileObj.FileName = fileName;
FileInfo file = new FileInfo(fileName);
fileObj.FileSize = file.Length;
try
{
//将文件对象保存到数据库中
this.FileSaver.SaveObjAndFileToDB(fileObj);
//给关联表加入记录
this.AddFolderFileRecord(obj.FolderID, fileObj);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return fileObj;
}
/// <summary>
/// 删除一个文件,同时删除关联表记录
/// </summary>
/// <param name="folderID"></param>
/// <param name="fileID"></param>
public void DeleteFile(int folderID, int fileID)
{
string strSQL;
try
{
//删除文件内容本身
this.FileSaver.DeleteObj(fileID);
//删除关联表记录
strSQL = "Delete * From FolderFile where FolderID = " + folderID + " and FileID = " + fileID;
this.dbObj.ExecSQLCommand(strSQL);
}
catch(Exception ex)
{
throw new Exception("从数据库中删除文件对象时出错,在FolderAccessObj.DeleteFile中,系统给出的信息:" + ex.Message);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -