📄 piclib.cs
字号:
using System;
using System.Data;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
namespace DataAccessLayer
{
/// <summary>
/// 文件夹节点实体类。
/// </summary>
public class PicLib
{
public int PicLibID = 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 PicLibAccessObj : BaseAccessObj
{
private AttachFileAccessObj FileSaver;
public PicLibAccessObj(OLEDBAccessObj dbObj) : base(dbObj)
{
this.FileSaver = new AttachFileAccessObj(dbObj);
}
/// <summary>
/// 将自身数据存入数据库中,不用保存文件,因为已经在创建文件时保存了
/// </summary>
/// <param name="obj"></param>
public void SaveMeToDB(PicLib obj)
{
if(this.dbObj == null)
return;
string strSQL;
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
Attachment fileObj;
int PicLibFileID;
if(obj.BelongTo.Substring(obj.BelongTo.Length - 1, 1) != "\\")
{
obj.BelongTo = obj.BelongTo + "\\";
}
try
{
//保存主记录
strSQL = "AddPicLibRecord";
pars.Add("PicLibIDValue", obj.PicLibID);
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 = "AddPicLibFileRecord";
for(int i=0; i < obj.AttachFiles.Count; i++)
{
fileObj = obj.AttachFiles[i] as Attachment;
pars.Clear();
//获取PicLibFile表的下一个ID
PicLibFileID = this.dbObj.GetMaxValueFromTable("PicLibFile", "PicLibFileID") + 1;
pars.Add("PicLibFileIDValue", PicLibFileID);
pars.Add("PicLibIDValue", obj.PicLibID);
pars.Add("FileIDValue", fileObj.FileID);
pars.Add("InputDateValue", obj.InputDate);
this.dbObj.ExecSQLCommand2(strSQL, pars);
}
}
}
catch(Exception ex)
{
throw new Exception("在执行存储过程AddPicLibRecord或AddPicLibFileRecord时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 更新对象所对应的数据库记录信息,不更新文件内容
/// </summary>
public void UpdateDBRow(PicLib obj, bool FileListNotChange)
{
if(obj == null)
return;
string strSQL;
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
Attachment fileObj;
int PicLibFileID;
try
{
strSQL = "UpdatePicLibRecord";
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("PicLibIDValue", obj.PicLibID);
this.dbObj.ExecSQLCommand2(strSQL, pars);
//更新关联表记录
//如果文件列表有改变
if(FileListNotChange == false)
{
//先删除所有的相关关联表记录
strSQL = "Delete * from PicLibFile where PicLibFileID=" + obj.PicLibID;
this.dbObj.ExecSQLCommand(strSQL);
//如果有附件
if(obj.AttachFiles.Count > 0)
{
//插入数据库中的关联表
strSQL = "AddPicLibFileRecord";
for(int i=0; i<obj.AttachFiles.Count; i++)
{
fileObj = obj.AttachFiles[i] as Attachment;
//获取PicLibFile表的下一个ID
PicLibFileID = this.dbObj.GetMaxValueFromTable("PicLibFile", "PicLibFileID") + 1;
pars.Clear();
pars.Add("PicLibFileIDValue", PicLibFileID);
pars.Add("PicLibIDValue", obj.PicLibID);
pars.Add("FileIDValue", fileObj.FileID);
pars.Add("InputDateValue", obj.InputDate);
this.dbObj.ExecSQLCommand2(strSQL, pars);
}
}
}
}
catch(Exception ex)
{
throw new Exception("在执行存储过程UpdatePicLibRecord时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 删除数据库记录
/// </summary>
/// <param name="BelongTo"></param>
public void DeleteDBRow(string BelongTo)
{
string strSQL;
try
{
//删除文件
strSQL = "DeleteFileOfPicLibNode";
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
pars.Add("BelongValue", BelongTo + "%");
this.dbObj.ExecSQLCommand2(strSQL, pars);
//删除关联记录
strSQL = "Delete * From PicLibFile where PicLibFile.PicLibID in (Select PicLib.PicLibID From PicLib where PicLib.BelongTo Like '" + BelongTo + "%')";
//MessageBox.Show(strSQL);
this.dbObj.ExecSQLCommand(strSQL);
//删除自身记录
strSQL = "Delete * From PicLib where BelongTo Like '" + BelongTo + "%'";
this.dbObj.ExecSQLCommand(strSQL);
}
catch(Exception ex)
{
throw new Exception("在删除PicLib记录时出错,系统给出的信息是:" + ex.Message);
}
}
/// <summary>
/// 给关联表增加一条记录
/// </summary>
/// <param name="FolderID"></param>
/// <param name="fileObj"></param>
public void AddPicLibFileRecord(long PicLibID, Attachment fileObj)
{
if(fileObj == null)
return;
if(PicLibID == 0)
throw new Exception("Error:PicLibID=0,In Table PicLibFile, PicLibID can not be zero!");
string strSQL;
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
int PicLibfileID;
//插入数据库中的关联表
strSQL = "AddPicLibFileRecord";
//获取PicLibFile表的下一个ID
PicLibfileID = this.dbObj.GetMaxValueFromTable("PicLibFile", "PicLibFileID") + 1;
try
{
pars.Add("PicLibFileIDValue", PicLibfileID);
pars.Add("PicLibIDValue", PicLibID);
pars.Add("FileIDValue", fileObj.FileID);
pars.Add("InputDateValue", DateTime.Today);
this.dbObj.ExecSQLCommand2(strSQL, pars);
}
catch(Exception ex)
{
throw new Exception("在使用存储过程AddPicLibFileRecord插入PicLibFile记录时出错,系统给出的信息:" + ex.Message);
}
}
/// <summary>
/// 根据路径名获取对象,没有返回null,如果有附件,则自动地将对应文件对象装入ArrayList中
/// </summary>
/// <param name="strPath"></param>
/// <returns></returns>
public PicLib GetObjectByPathStr(string strPath)
{
string strSQL = "Select * From PicLib where BelongTo = '" + strPath + "'";
DataSet ds = this.dbObj.GetSQLDataSet(strSQL, "PicLib");
DataRow dr;
PicLib obj = null;
int count;
Attachment fileObj;
if(ds.Tables["PicLib"].Rows.Count > 0)
{
dr = ds.Tables["PicLib"].Rows[0];
obj = this.LoadFromDataRow(dr);
//装入文件对象
strSQL = "Select * From PicLibFile where PicLibID = " + obj.PicLibID;
ds = this.dbObj.GetSQLDataSet(strSQL, "PicLibFile");
count = ds.Tables["PicLibFile"].Rows.Count;
if(count > 0)
{
for(int i=0; i<count; i++)
{
dr = ds.Tables["PicLibFile"].Rows[i];
fileObj = this.FileSaver.LoadAttachmentObjFromID((int)dr["FileID"]);
obj.AttachFiles.Add(fileObj);
}
}
}
return obj;
}
/// <summary>
/// 用新路径名刷新老路径名,完成后,所有PicLib表中以OldPathStr开头的记录最后部分用新路径NewPathStr取代
/// </summary>
/// <param name="OldPathStr"></param>
/// <param name="NewPathStr"></param>
public void RenameNodePath(string OldPathStr, string NewPathStr)
{
string strSQL = "RenamePicLibNode";
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("在执行存储过程RenamePicLibNode时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 当节点移动时,所有PicLib表中以OldPathStr开头的记录完全用新路径NewPathStr取代
/// </summary>
/// <param name="OldPathStr"></param>
/// <param name="NewPathStr"></param>
public void MoveNodePath(string OldPathStr, string NewPathStr)
{
//string strSQL = "MovePicLibNode";
string strSQL = "RenamePicLibNode";
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("在执行存储过程MovePicLibNode时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 获取下一个可用的对象ID
/// </summary>
/// <returns></returns>
public int GetNextID()
{
return this.dbObj.GetMaxValueFromTable("PicLib", "PicLibID") + 1;
}
public void SaveToDataRow(DataRow dr, PicLib obj)
{
if(dr == null || obj == null)
{
throw new Exception("PicLib.SaveToDataRow need two Argument:dr and obj ,both can not be null");
}
dr["PicLibID"] = obj.PicLibID;
dr["BelongTo"] = obj.BelongTo;
dr["InputDate"] = obj.InputDate;
dr["Text"] = obj.Text;
dr["RTFText"] = obj.RTFText;
}
public PicLib LoadFromDataRow(DataRow dr)
{
if(dr == null)
{
throw new Exception("PicLib:LoadFromDataRow need an Argument:dr which can not be null");
}
PicLib obj = new PicLib();
obj.PicLibID = (int)dr["PicLibID"];
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(PicLib 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.AddPicLibFileRecord(obj.PicLibID, 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 PicLibID, int fileID)
{
string strSQL;
try
{
//删除文件内容本身
this.FileSaver.DeleteObj(fileID);
//删除关联表记录
strSQL = "Delete * From PicLibFile where PicLibID = " + PicLibID + " and FileID = " + fileID;
this.dbObj.ExecSQLCommand(strSQL);
}
catch(Exception ex)
{
throw new Exception("从数据库中删除文件对象时出错,在PicLibAccessObj.DeleteFile中,系统给出的信息:" + ex.Message);
}
}
private MemoryStream mem = new MemoryStream();
/// <summary>
/// 从数据库中装入一个图像,失败返回null
/// </summary>
/// <param name="ImageFileObj"></param>
/// <returns></returns>
public Bitmap GetPicFromDB(Attachment ImageFileObj)
{
Bitmap bmp;
if(ImageFileObj == null)
return null;
string strSQL = "Select FileContent From Files where FileID=" + ImageFileObj.FileID;
try
{
//流移到开头
mem.Position = 0;
this.dbObj.SaveBLOBToMemoryStream(strSQL, 0, mem);
bmp = new Bitmap(mem);
}
catch(Exception ex)
{
throw new NotImageTypeException("尝试装入的文件不是图像文件:" + ex.GetType().Name + "-" + ex.Message);
}
return bmp;
}
}
public class NotImageTypeException : Exception
{
public NotImageTypeException(string Msg) : base(Msg)
{}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -