Model.cs
资源名称:Visual.rar [点击查看]
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:2k
源码类别:
C#编程
开发平台:
Others
- using System;
- using System.Drawing;
- using System.Collections;
- using System.Data;
- using System.Data.SqlClient;
- namespace photo
- {
- //模型类
- 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()
- {
- sqlConn=new SqlConnection("data source=Localhost;initial catalog=EAlbum;User ID=sa;Password=sa;");
- //连接数据库
- if(sqlConn.State == ConnectionState.Closed)
- sqlConn.Open();
- }
- //添加照片,参数file为文件名,cateid为照片属于的种类
- public void addphoto2(string file)
- {
- //将文件名为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_InsertPhoto2", 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;
- */
- //return nID;
- }
- }
- }