|
16金钱
飞哥,我运行程序的时候出现了如图这样的问题,麻烦你帮我看一下该怎么解决,先谢谢啦
using System;
using System.Drawing;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace 系统
{
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("其他");
string conStr = @"Data Source=127.0.0.1;Initial Catalog=db_毕业论文ersist Security Info=True;User ID=sa;pwd=zack931104";
sqlConn = new SqlConnection(conStr);
//连接数据库
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["hotoID"]);
nameList.Add(sqlPhotoAlbum["hoto"].ToString());
descList.Add(sqlPhotoAlbum["hoto_Desc"].ToString());
timeList.Add(sqlPhotoAlbum["hoto_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).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) == 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);
}
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).IndexOf(str) > -1)
searchMark = 1;
else
searchMark = 0;
}
}
//照相时间
if (index == 1)
{
for (int i = 0; i < timeList.Count; i++)
{
//如果在timeList中匹配到用户输入的关键字则将这个纪录对应的searchMark置为1(初始化时是0)
if (((string)timeList).IndexOf(str) > -1)
searchMark = 1;
else
searchMark = 0;
}
}
//描述信息
if (index == 2)
{
for (int i = 0; i < descList.Count; i++)
{
//如果在descList中匹配到用户输入的关键字则将这个纪录对应的searchMark置为1(初始化时是0)
if (((string)descList).IndexOf(str) > -1)
searchMark = 1;
else
searchMark = 0;
}
}
dataUpdate("2", 0);
}
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);
}
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 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;
}
}
}
using System;
using System.Windows.Forms;
namespace 系统
{
public enum TextType { name, time, desc };
//显示照片名称、照相时间和描述信息,继承了 TextBox,Observer两个类
public class Attribute : TextBox, Observer
{
private Model model;
public TextType type;
//构造函数
public Attribute()
{
}
//实现自己的dataUpdate
public void dataUpdate(Model model, string str, int npara)
{
this.model = model;
DrawText();
}
//设置视图对应的模型
public void SetModel(Model model)
{
this.model = model;
}
//重画
private void DrawText()
{
int index = model.ListIndex;
if (index >= 0 && model.nameList.Count > 0)
{
if (this.type == TextType.name)
this.Text = (string)model.nameList[index];
else if (this.type == TextType.desc)
this.Text = (string)model.descList[index];
else if (this.type == TextType.time)
this.Text = (string)model.timeList[index];
}
else
this.Text = "";
}
}
}
|
-
|