📄 piclib.cs
字号:
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 + -