⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attachfileaccessobj.cs

📁 个人信息的源代码
💻 CS
字号:
using System;
using System.IO;
namespace DataAccessLayer
{
	/// <summary>
	/// 此类将文件保存到数据库的字段中。
	/// </summary>
	public class AttachFileAccessObj : BaseAccessObj
	{
		public AttachFileAccessObj(OLEDBAccessObj dbobj) : base(dbobj)
		{

		}

		/// <summary>
		/// 获取下一个ID
		/// </summary>
		/// <returns></returns>
		public int GetNextID()
		{
			return this.dbObj.GetMaxValueFromTable("Files", "FileID") + 1;
		}
		/// <summary>
		/// 根据ID取出Attachment对象,注意,不包含文件流本身
		/// </summary>
		/// <param name="objID"></param>
		/// <returns></returns>
		public Attachment LoadAttachmentObjFromID(int objID)
		{
			this.strSQL = "Select FileID,FileName,FileSize,FileNotes From Files Where FileID=" + objID;
			try
			{
				this.ds = this.dbObj.GetSQLDataSet(strSQL, "Files");
				dt = ds.Tables["Files"];
				if(dt.Rows.Count == 0)
				{
					throw new ApplicationException("没能找到指定ID = " + objID + "Files表记录,在AttachFileAccessObj.LoadAttachmentObjFromID中");
				}
				Attachment obj = new Attachment();
				obj.FileID = (int)dt.Rows[0]["FileID"];
				obj.FileName = dt.Rows[0]["FileName"].ToString();
				obj.FileSize = long.Parse(dt.Rows[0]["FileSize"].ToString());
				if(dt.Rows[0].IsNull("FileNotes") == false)
				{
					obj.FileNotes = dt.Rows[0]["FileNotes"].ToString();
				}
				return obj;
			}
			catch(Exception ex)
			{
				System.Windows.Forms.Clipboard.SetDataObject(strSQL);
				throw new Exception("在从数据库中取出Attachment对象时出错,系统给出的信息为:" + ex.Message);
			}
		}
		/// <summary>
		/// 将Obj所代表的文件及对象本身存入表中
		/// </summary>
		/// <param name="obj"></param>
		public void SaveObjAndFileToDB(Attachment obj)
		{
			if(obj == null)
				return;
			byte[] b;
			FileStream fs = null;
			BinaryReader br = null;
			//读文件入内存
			try
			{
				fs = new FileStream(obj.FileName, FileMode.Open, FileAccess.Read);
				br = new BinaryReader(fs);
				b = br.ReadBytes((int)fs.Length);
			}
			catch(Exception ex)
			{
				throw new Exception("在将文件读入内存时出错,在AttachFileAccessObj.SaveObjAndFileToDB中。系统返回的出错信息为:" + ex.Message);
			}
			finally
			{
				br.Close();
				fs.Close();
			}
			//将文件插入到数据库字段中
			try
			{
				strSQL = string.Format("Insert into Files(FileID,FileName,FileSize,FileNotes,FileContent) Values({0},'{1}',{2},'{3}',@FileContent)",obj.FileID, obj.FileName, obj.FileSize, obj.FileNotes);
				this.dbObj.DoWithBLOBField(strSQL, "FileContent", b);
			}
			catch(Exception ex)
			{
				System.Windows.Forms.Clipboard.SetDataObject(strSQL);
				throw new Exception("在将读入内存的文件内容写到数据库中时出错,在AttachFileAccessObj.SaveObjAndFileToDB中,系统给出的信息为:" + ex.Message);
			}
		}
		/// <summary>
		/// 取出Obj所代表的文件,以SaveFileName名字存入硬盘中
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="SaveFileName"></param>
		public void SaveBDFileToDisk(Attachment obj, string SaveFileName)
		{
			try
			{
				strSQL = "Select FileContent from Files where FileID=" + obj.FileID;
				this.dbObj.SaveBLOBToFile(strSQL, 0, SaveFileName);
			}
			catch(Exception ex)
			{
				System.Windows.Forms.Clipboard.SetDataObject(strSQL);
				throw new ApplicationException("在将数据库文件" + obj.FileName + "保存为文件" + SaveFileName + "时出错。在AttachFileAccessObj.SaveBDFileToDisk中,系统给出的信息为:" + ex.Message);
			}
		}
		/// <summary>
		/// 删除一条对象记录
		/// </summary>
		/// <param name="obj"></param>
		public void DeleteObj(Attachment obj)
		{
			try
			{
				strSQL = "Delete * from files where FileID=" + obj.FileID;
				this.dbObj.ExecSQLCommand(strSQL);
			}
			catch(Exception ex)
			{
				System.Windows.Forms.Clipboard.SetDataObject(strSQL);
				throw new Exception("在删除Files表记录时出错,在AttachFileAccessObj.DeleteObj中,系统给出的信息为:" + ex.Message);
			}
		}
		/// <summary>
		/// 按ID删除对象
		/// </summary>
		/// <param name="obj"></param>
		public void DeleteObj(int fileID)
		{
			try
			{
				strSQL = "Delete * from files where FileID=" + fileID;
				this.dbObj.ExecSQLCommand(strSQL);
			}
			catch(Exception ex)
			{
				System.Windows.Forms.Clipboard.SetDataObject(strSQL);
				throw new Exception("在删除Files表记录时出错,在AttachFileAccessObj.DeleteObj中,系统给出的信息为:" + ex.Message);
			}
		}
		/// <summary>
		/// 更新Attachment对象信息(不更新文件流)
		/// </summary>
		/// <param name="obj"></param>
		public void UpdateAttachmentObjInDB(Attachment obj)
		{
			if(obj == null)
				return;
			try
			{
				strSQL = string.Format("Update Files Set FileName='{0}',FileSize={1},FileNotes='{2}' where FileID={3}", obj.FileName, obj.FileSize, obj.FileNotes, obj.FileID);
				this.dbObj.ExecSQLCommand(strSQL);
			}
			catch(Exception ex)
			{
				throw new ApplicationException("更新文件信息时出错,在AttachFileAccessObj.UpdateAttachmentObjInDB中。系统返回的出错信息为:" + ex.Message);
			}
		}
	}
	/// <summary>
	/// 代表文件对象
	/// </summary>
	public class Attachment
	{
		public int FileID = 0;		//此ID对应于Files表中的ID
		public string FileName = "";//附件文件名
		public long FileSize = 0;	//附件文件大小

		private string _notes = ""; //对文件的说明,最多255个字符

		public bool HasChanged = false;

		public string FileNotes
		{
			get
			{
				return _notes;
			}
			set
			{
				if(this._notes != value)
				{
					this.HasChanged = true;
				}
				if(value.Length > 255)
				{
					this._notes = value.Substring(0,255);
				}
				else
				{
					this._notes = value;
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -