📄 onlyfile.cs
字号:
using System;
using System.Data;
namespace DataAccessLayer
{
/// <summary>
/// 文件型节点数据。
/// </summary>
public class OnlyFile
{
public int OnlyFileID = 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;
//所附之文件
public Attachment AttachFile = null;
}
public class OnlyFileAccessObj : BaseAccessObj
{
private AttachFileAccessObj FileSaver;
public OnlyFileAccessObj(OLEDBAccessObj dbObj) : base(dbObj)
{
this.FileSaver = new AttachFileAccessObj(dbObj);
}
/// <summary>
/// 将自身数据存入数据库中,不用保存文件,因为已经在创建文件时保存了
/// </summary>
/// <param name="obj"></param>
public void SaveMeToDB(OnlyFile obj)
{
if(this.dbObj == null)
return;
string strSQL;
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
if(obj.BelongTo.Substring(obj.BelongTo.Length - 1, 1) != "\\")
{
obj.BelongTo = obj.BelongTo + "\\";
}
//保存主记录
strSQL = "AddOnlyFileRecord";
pars.Add("OnlyFileIDValue", obj.OnlyFileID);
pars.Add("TextValue", obj.Text);
pars.Add("BelongToValue", obj.BelongTo);
pars.Add("InputDateValue", obj.InputDate);
if(obj.AttachFile == null)
{
pars.Add("FileIDValue", 0);
pars.Add("FileNameValue", "");
pars.Add("FileLengthValue", 0);
}
else
{
pars.Add("FileIDValue", obj.AttachFile.FileID);
pars.Add("FileNameValue", obj.AttachFile.FileName);
pars.Add("FileLengthValue", obj.AttachFile.FileSize);
}
pars.Add("RTFTextValue", obj.RTFText);
try
{
this.dbObj.ExecSQLCommand2(strSQL, pars);
}
catch(Exception ex)
{
throw new Exception("在执行存储过程AddOnlyFileRecord时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 更新对象所对应的数据库记录信息,不更新文件内容
/// </summary>
public void UpdateDBRow(OnlyFile obj)
{
if(obj == null)
return;
string strSQL;
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
strSQL = "UpdateOnlyFileRecord";
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);
if(obj.AttachFile == null)
{
pars.Add("FileIDValue", 0);
pars.Add("FileNameValue", "");
pars.Add("FileLengthValue", 0);
}
else
{
pars.Add("FileIDValue", obj.AttachFile.FileID);
pars.Add("FileNameValue", obj.AttachFile.FileName);
pars.Add("FileLengthValue", obj.AttachFile.FileSize);
}
pars.Add("RTFTextValue", obj.RTFText);
pars.Add("OnlyFileIDValue", obj.OnlyFileID);
try
{
this.dbObj.ExecSQLCommand2(strSQL, pars);
}
catch(Exception ex)
{
throw new Exception("在执行存储过程UpdateOnlyFileRecord时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 删除数据库记录
/// </summary>
/// <param name="BelongTo"></param>
public void DeleteDBRow(string BelongTo)
{
string strSQL;
try
{
//删除文件
strSQL = "DeleteFileOfOnlyFileNode";
System.Collections.Specialized.ListDictionary pars = new System.Collections.Specialized.ListDictionary();
pars.Add("BelongValue", BelongTo + "%");
this.dbObj.ExecSQLCommand2(strSQL, pars);
//删除自身记录
strSQL = "Delete * From OnlyFile where BelongTo Like '" + BelongTo + "%'";
this.dbObj.ExecSQLCommand(strSQL);
}
catch(Exception ex)
{
throw new Exception("在删除OnlyFile记录时出错,系统给出的信息是:" + ex.Message);
}
}
/// <summary>
/// 根据路径名获取对象,没有返回null,如果有附件,则自动地将对应文件对象装入ArrayList中
/// </summary>
/// <param name="strPath"></param>
/// <returns></returns>
public OnlyFile GetObjectByPathStr(string strPath)
{
string strSQL = "Select * From OnlyFile where BelongTo = '" + strPath + "'";
DataSet ds = this.dbObj.GetSQLDataSet(strSQL, "OnlyFile");
DataRow dr;
if(ds.Tables["OnlyFile"].Rows.Count > 0)
{
dr = ds.Tables["OnlyFile"].Rows[0];
return this.LoadFromDataRow(dr);
}
return null;
}
/// <summary>
/// 用新路径名刷新老路径名,完成后,所有OnlyFile表中以OldPathStr开头的记录最后部分用新路径NewPathStr取代
/// </summary>
/// <param name="OldPathStr"></param>
/// <param name="NewPathStr"></param>
public void RenameNodePath(string OldPathStr, string NewPathStr)
{
string strSQL = "RenameOnlyFileNode";
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("在执行存储过程RenameOnlyFileNode时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 当节点移动时,所有OnlyFile表中以OldPathStr开头的记录完全用新路径NewPathStr取代
/// </summary>
/// <param name="OldPathStr"></param>
/// <param name="NewPathStr"></param>
public void MoveNodePath(string OldPathStr, string NewPathStr)
{
//string strSQL = "MoveOnlyFileNode";
string strSQL = "RenameOnlyFileNode";
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("在执行存储过程MoveOnlyFileNode时出错,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 获取下一个可用的对象ID
/// </summary>
/// <returns></returns>
public int GetNextID()
{
return this.dbObj.GetMaxValueFromTable("OnlyFile", "OnlyFileID") + 1;
}
public void SaveToDataRow(DataRow dr, OnlyFile obj)
{
if(dr == null || obj == null)
{
throw new Exception("OnlyFile.SaveToDataRow need two Argument:dr and obj ,both can not be null");
}
dr["OnlyFileID"] = obj.OnlyFileID;
dr["BelongTo"] = obj.BelongTo;
dr["InputDate"] = obj.InputDate;
dr["Text"] = obj.Text;
dr["RTFText"] = obj.RTFText;
if(obj.AttachFile == null)
{
dr["FileID"] = 0;
dr["FileName"] = "";
dr["FileLength"] = 0;
}
else
{
dr["FileID"] = obj.AttachFile.FileID;
dr["FileName"] = obj.AttachFile.FileName;
dr["FileLength"] = obj.AttachFile.FileSize;
}
}
public OnlyFile LoadFromDataRow(DataRow dr)
{
if(dr == null)
{
throw new Exception("OnlyFile:LoadFromDataRow need an Argument:dr which can not be null");
}
OnlyFile obj = new OnlyFile();
obj.OnlyFileID = (int)dr["OnlyFileID"];
obj.BelongTo = dr["BelongTo"].ToString();
obj.InputDate = (DateTime)dr["InputDate"];
obj.Text = dr["Text"].ToString();
if((int)dr["FileID"] > 0)
{
obj.AttachFile = new Attachment();
obj.AttachFile.FileID = (int)dr["FileID"];
obj.AttachFile.FileName = dr["FileName"].ToString();
//System.Windows.Forms.MessageBox.Show(dr["FileLength"].ToString());
obj.AttachFile.FileSize = long.Parse(dr["FileLength"].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);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -