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

📄 model.cs

📁 这是一个关于用C#从数据库中加载图片和读取图片的小应用程序
💻 CS
字号:
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("server=.;database=EAlbum;uid=sa;pwd=84119;");

			//连接数据库
			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();
			
			
		}
		


		

	}
}

⌨️ 快捷键说明

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