📄 model.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace EAlbum
{
//模型类
public class Model
{
//定义了一系列的列表,用于存储从数据库中取出的数据
public ArrayList idList,categoryList,nameList,descList,searchMark,albumList,timeList,observer;
private SqlConnection sqlConn;
//图片类型
public Image image;
//标识结点位置
public int ListIndex;
//析构函数,用于关闭与数据库的连接
~Model()
{
//if(sqlConn.State == ConnectionState.Open)
//sqlConn.Close();
}
//构造函数,初始化、实例化Model类中的成员变量
public Model()
{
ListIndex=-1;
observer = new ArrayList();
idList=new ArrayList();
albumList=new ArrayList();
categoryList=new ArrayList();
nameList=new ArrayList();
descList=new ArrayList();
searchMark=new ArrayList();
timeList=new ArrayList();
albumList.Add("动物");
albumList.Add("明星");
albumList.Add("汽车");
albumList.Add("风景");
albumList.Add("其他");
sqlConn=new SqlConnection("data source=WJD;initial catalog=EAlbum;Persist Security Info=False;Integrated Security=SSPI;");
//连接数据库
if(sqlConn.State == ConnectionState.Closed)
sqlConn.Open();
if(sqlConn.State == ConnectionState.Open)
{
SqlCommand sqlCmd = new SqlCommand("SELECT category, [id] AS PhotoID, [name] AS Photo, [desc] AS Photo_Desc,[time] AS Photo_Time FROM Photos ", sqlConn);
SqlDataReader sqlPhotoAlbum = sqlCmd.ExecuteReader();
//将除图片本身以外的数据库表Photos中的信息全部取出,放在事先定义的列表中
while( sqlPhotoAlbum.Read() )
{
idList.Add((int)sqlPhotoAlbum["PhotoID"]);
nameList.Add(sqlPhotoAlbum["Photo"].ToString());
descList.Add(sqlPhotoAlbum["Photo_Desc"].ToString());
timeList.Add(sqlPhotoAlbum["Photo_Time"].ToString());
categoryList.Add((int)sqlPhotoAlbum["category"]);
//每添加一项就给对应的项做一个标记,为查找作准备
searchMark.Add(0);
}
sqlPhotoAlbum.Close();
}
}
//用来向模型中登记观察者,o是一个view的实例
public void registerObserver(Observer o)
{
observer.Add(o);
}
// 用来向模型中注销观察者.
public void removeObserver(Observer o)
{
observer.Remove(o);
}
//通知视图进行更新操作,str代表操作的类型,npara为此操作的参数(只在添加时有用)
private void dataUpdate(string str,int npara)
{
for(int i=0;i<observer.Count;i++)
{
((Observer)observer[i]).dataUpdate(this,str,npara);
}
}
/*下面定义了一系列对数据的操作
* 包括照片的选取、查找、删除、添加和照片属性的更新
* 这里用数字1、2、3、4、5分别表示这相应的5种操作
* */
//选取,index为此照片在数据库中的ID——对应着picture的数据的改变(PictureBox)
public void select(int index)
{
ListIndex=-1;
image=null;
//没有选取照片
if(index<0)
{
dataUpdate("1",0);
return;
}
//将被选中的照片的ID和已经取出的所有照片相比,
//如果匹配则记下它的索引值——ListIndex——在整个列表中的序号
for(int i=0;i<nameList.Count;i++)
{
if (((int)idList[i])==index)
{
ListIndex=i;
break;
}
}
//从数据库中取出此照片,放在image中
string strCmd = String.Format("SELECT photo FROM Photos WHERE id = {0}", index);
SqlCommand cmd = new SqlCommand(strCmd, sqlConn);
byte[] b = (byte[])cmd.ExecuteScalar();
if(b.Length > 0)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
stream.Write(b, 0, b.Length);
image=new Bitmap(stream);
stream.Close();
}
//更新
dataUpdate("1",0);
}
//查找,支持模糊查询,index的可能值为0、1、2
//分别代表在照片名称、照相时间和描述信息中查找
public void search(int index,string str)
{
//照片名称
if(index==0)
{
for(int i=0;i<nameList.Count;i++)
{
//如果在nameList中匹配到用户输入的关键字则将这个纪录对应的searchMark置为1(初始化时是0)
if (((string)nameList[i]).IndexOf(str)>-1)
searchMark[i]=1;
else
searchMark[i]=0;
}
}
//照相时间
if(index==1)
{
for(int i=0;i<timeList.Count;i++)
{
//如果在timeList中匹配到用户输入的关键字则将这个纪录对应的searchMark置为1(初始化时是0)
if (((string)timeList[i]).IndexOf(str)>-1)
searchMark[i]=1;
else
searchMark[i]=0;
}
}
//描述信息
if(index==2)
{
for(int i=0;i<descList.Count;i++)
{
//如果在descList中匹配到用户输入的关键字则将这个纪录对应的searchMark置为1(初始化时是0)
if (((string)descList[i]).IndexOf(str)>-1)
searchMark[i]=1;
else
searchMark[i]=0;
}
}
dataUpdate("2",0);
}
//删除照片
public int deletephoto()
{
//没有选定照片或数据库中没有照片,不执行任何删除操作
if(ListIndex<0 || idList.Count==0)
return ListIndex;
//取出照片的ID,从数据库中删除
int index=(int)(idList[ListIndex]);
string strCmd= String.Format("DELETE FROM Photos WHERE id = {0}", index);
SqlCommand cmd = new SqlCommand(strCmd, sqlConn);
cmd.ExecuteNonQuery();
//把所有列表中关于此照片的数据删除
idList.RemoveAt(ListIndex);
nameList.RemoveAt(ListIndex);
descList.RemoveAt(ListIndex);
categoryList.RemoveAt(ListIndex);
searchMark.RemoveAt(ListIndex);
timeList.RemoveAt(ListIndex);
image=null;
int revalue=ListIndex;
//将“指针”指向被删除照片的前一张照片
ListIndex--;
dataUpdate("3",ListIndex);
return revalue;
}
//添加照片,参数file为文件名,cateid为照片属于的种类
public int addphoto(string file,int cateid)
{
//将文件名为file的文件读入到buffer中
System.IO.FileStream stream = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
string strName = System.IO.Path.GetFileNameWithoutExtension(file);
//调用sp_InsertPhoto存储过程,添加照片
SqlCommand cmd = new SqlCommand("sp_InsertPhoto", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = cmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
param.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
cmd.Parameters.Add("@album", SqlDbType.Int).Value = cateid;
cmd.ExecuteNonQuery();
//获得返回的照片ID
int nID = (int)cmd.Parameters["RETURN_VALUE"].Value;
//将照片添加到列表中
idList.Add(nID);
nameList.Add(strName);
descList.Add("");
searchMark.Add(0);
categoryList.Add(cateid);
timeList.Add("");
//buffer清空
buffer = null;
//更新视图,cateid参数表示照片属于的种类
dataUpdate("4",cateid);
return nID;
}
//更新照片的属性信息
public void update(string newname,string newtime, string newdesc)
{
if(ListIndex<0)
return;
string strCmd= String.Format("UPDATE Photos SET [name] = '{0}',[time]='{1}',[desc]='{2}' WHERE id = {3}", newname,newtime,newdesc, idList[ListIndex]);
//在列表中更新
nameList[ListIndex]=newname;
timeList[ListIndex]=newtime;
descList[ListIndex]=newdesc;
//在数据库中更新
SqlCommand cmd = new SqlCommand(strCmd, sqlConn);
cmd.ExecuteNonQuery();
dataUpdate("5",0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -