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

📄 form1.cs

📁 原代码详细说明是关于c++方面的希望可以帮助大家使用
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace Exam6_3_3
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Button button6;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button6 = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.button4 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(8, 11);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(29, 17);
			this.label1.TabIndex = 0;
			this.label1.Text = "学号";
			// 
			// pictureBox1
			// 
			this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.pictureBox1.Location = new System.Drawing.Point(144, 8);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(96, 112);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
			this.pictureBox1.TabIndex = 4;
			this.pictureBox1.TabStop = false;
			// 
			// button1
			// 
			this.button1.Enabled = false;
			this.button1.Location = new System.Drawing.Point(248, 8);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(40, 24);
			this.button1.TabIndex = 7;
			this.button1.Text = "设置";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button6
			// 
			this.button6.Location = new System.Drawing.Point(104, 120);
			this.button6.Name = "button6";
			this.button6.Size = new System.Drawing.Size(40, 24);
			this.button6.TabIndex = 9;
			this.button6.Text = "删除";
			this.button6.Click += new System.EventHandler(this.button6_Click);
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(9, 43);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(29, 17);
			this.label2.TabIndex = 12;
			this.label2.Text = "姓名";
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(8, 120);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(40, 24);
			this.button4.TabIndex = 13;
			this.button4.Text = "添加";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button5
			// 
			this.button5.Enabled = false;
			this.button5.Location = new System.Drawing.Point(56, 120);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(40, 24);
			this.button5.TabIndex = 8;
			this.button5.Text = "取消";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(48, 8);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(72, 21);
			this.textBox1.TabIndex = 14;
			this.textBox1.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(48, 40);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(88, 21);
			this.textBox2.TabIndex = 15;
			this.textBox2.Text = "";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(16, 80);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(40, 24);
			this.button2.TabIndex = 16;
			this.button2.Text = "上页";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(72, 80);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(40, 24);
			this.button3.TabIndex = 17;
			this.button3.Text = "下页";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(296, 149);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.button6);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.label1);
			this.Name = "Form1";
			this.Text = "存取照片";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
	    private string fileName="";
		private SqlConnection conn;
		private SqlDataAdapter sqlDataAdapter;
		private DataSet ds;
		private int cur;
		private void Form1_Load(object sender, System.EventArgs e)
		{
			try{
				conn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=xsgl;");
				SqlCommand sqlCommand1= new SqlCommand("select studID,studName from studentPhoto",conn); 
				sqlDataAdapter=new SqlDataAdapter();
				ds=new DataSet();
				sqlDataAdapter.SelectCommand=sqlCommand1;
				sqlDataAdapter.Fill(ds,"studentPhoto");
				textBox2.DataBindings.Add(new Binding("Text",ds, "studentPhoto.studName"));
				textBox1.DataBindings.Add(new Binding("Text",ds, "studentPhoto.studID"));
				this.BindingContext[ds, "studentPhoto"].PositionChanged +=new EventHandler(BindingManagerBase_PositionChanged);			
				getPhoto(textBox1.Text);
			}
			catch(Exception ee){System.Console.WriteLine(ee.Message);}
		}
		private void BindingManagerBase_PositionChanged(object sender, EventArgs e)
		{
            getPhoto(textBox1.Text);			
		}
		public byte[] GetPhoto(string filePath)
		{
			FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
			BinaryReader br = new BinaryReader(fs);
			byte[] photo = br.ReadBytes((int)fs.Length);
			br.Close();
			fs.Close();
			return photo;
		}
		private void getPhoto(string studID)
		{
			try
			{
				pictureBox1.Image=null;
				conn.Open();
				SqlCommand sqlCommand2= new SqlCommand("select studPhoto from studentPhoto where studID=@studID",conn); 
				sqlCommand2.Parameters.Add("@studID",SqlDbType.VarChar, 10).Value=studID;
				SqlDataReader myReader = sqlCommand2.ExecuteReader(CommandBehavior.SequentialAccess);
				MemoryStream ms=new MemoryStream();
				BinaryWriter bw;                        // Streams the BLOB to the FileStream object.
				int bufferSize = 100;                     // 缓冲区大小
				byte[] outbyte = new byte[bufferSize];       // 缓冲区
				long retval;                            // The bytes returned from GetBytes.
				long startIndex = 0;                    // The starting position in the BLOB output.
				if(myReader.Read())
				{
					bw = new BinaryWriter(ms);
					startIndex = 0;
					retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
					while (retval == bufferSize)
					{
						bw.Write(outbyte);
						bw.Flush();
						startIndex += bufferSize;
						retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
					}
					if(retval>1)bw.Write(outbyte, 0, (int)retval - 1);
					bw.Flush();
					if(ms.Length>0)
					   pictureBox1.Image=Image.FromStream(ms);
					else
                       pictureBox1.Image=null;
					bw.Close();
					ms.Close();
				}
				myReader.Close();
				conn.Close();		
			}
			catch(Exception ee){System.Console.WriteLine(ee.Message);}
		}
		private void button1_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog openFileDialog1=new OpenFileDialog();
			openFileDialog1.Filter="File(*.jpg)|*.jpg|File(*.bmp)|*.bmp";
			openFileDialog1.RestoreDirectory=true;
			if(DialogResult.OK==openFileDialog1.ShowDialog())
			{
				pictureBox1.Image=Image.FromFile(openFileDialog1.FileName);
				fileName=openFileDialog1.FileName;
			}
		}
		private void button2_Click(object sender, System.EventArgs e)
		{
			this.BindingContext[ds, "studentPhoto"].Position-=1;
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			this.BindingContext[ds, "studentPhoto"].Position+=1;
		}
		private void button4_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(button4.Text=="添加")
				{
					cur=this.BindingContext[ds, "studentPhoto"].Position;
					button4.Text="保存";
					textBox1.Text="";
					textBox2.Text="";
					pictureBox1.Image=null;
					button1.Enabled=true;
					button2.Enabled=false;
					button3.Enabled=false;
					button5.Enabled=true;
					button6.Enabled=false;
					fileName="";
					//textBox1.Focus();
				}
				else
				{
					button1.Enabled=false;
					button2.Enabled=true;
					button3.Enabled=true;
					button5.Enabled=false;
					button6.Enabled=true;
					button4.Text="添加";
					byte[] photo = GetPhoto(fileName);
					conn.Open();
					SqlCommand sqlCommand3= new SqlCommand("insert studentPhoto values(@studID,@studName,@studPhoto)",conn); 
					sqlCommand3.Parameters.Add("@studID",SqlDbType.VarChar, 10).Value=textBox1.Text;
					sqlCommand3.Parameters.Add("@studName",SqlDbType.VarChar, 10).Value=textBox2.Text;
					sqlCommand3.Parameters.Add("@studPhoto",SqlDbType.Image, photo.Length).Value=photo;
					sqlCommand3.ExecuteNonQuery();
					conn.Close();	
					ds.Tables["studentPhoto"].Clear();
					sqlDataAdapter.Fill(ds,"studentPhoto");
					this.BindingContext[ds, "studentPhoto"].Position=this.BindingContext[ds, "studentPhoto"].Count-1;
					getPhoto(textBox1.Text);
				}
			}
			catch(Exception ee){System.Console.WriteLine(ee.Message);}
		}
		private void button5_Click(object sender, System.EventArgs e)
		{
			button1.Enabled=false;
			button2.Enabled=true;
			button3.Enabled=true;
			button5.Enabled=false;
			button6.Enabled=true;
			button4.Text="添加";
			ds.Tables["studentPhoto"].Clear();
			sqlDataAdapter.Fill(ds,"studentPhoto");
			this.BindingContext[ds, "studentPhoto"].Position=cur;
			getPhoto(textBox1.Text);
		}

		private void button6_Click(object sender, System.EventArgs e)
		{
			conn.Open();
			SqlCommand sqlCommand4= new SqlCommand("delete from studentPhoto where studID=@studID",conn); 
			sqlCommand4.Parameters.Add("@studID",SqlDbType.VarChar, 10).Value=textBox1.Text;
			sqlCommand4.ExecuteNonQuery();
			conn.Close();	
			ds.Tables["studentPhoto"].Clear();
			sqlDataAdapter.Fill(ds,"studentPhoto");
			this.BindingContext[ds, "studentPhoto"].Position=this.BindingContext[ds, "studentPhoto"].Count-1;
			getPhoto(textBox1.Text);
		}
	}
}

⌨️ 快捷键说明

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