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

📄 startform.cs

📁 Adding and making operations LOB data in a database with C#
💻 CS
字号:
/**********************************************************************************************
@author  Chandar
@version 1.0
Development Environment        :  Visual C#.Net Beta2
Name of the File               :  StartForm.cs
Creation/Modification History  :
                                  02-AUG-2001     Created

Sample Overview
---------------
 This sample uses ADO functionality with OraOLEDB to communicate to database.
 The sample demonstrates the feature of Oracle OLEDB provider for Oracle to
 pass LOB parameters to and from a database stored procedure. It also demonstrates
 that Oracle provider for OLEDB bypasses PL/SQL limitation of parameter size greater
 than 32K to a database stored procedure.The image passed to and from the stored procedure 
 could be of size greater than 32K.

 The property 'SPPrmsLOB' is to be set to true when LOB parameter is used in stored procedure.
 The sample shows how to set this property for the command object
  
 When this application is run, a dialog box with products loaded from "Product_Information"
 table appears. When a product is selected from the list its image is retrieved from
 database using stored procedure and displayed in "Existing Image " picture box.
 
 In case the product image needs to be changed, the user can select an image by clicking
 the "Browse" button, the selected  image from the common file dialog is displayed in the 
 "New Image" picture box. To update the new image to the database "Update" button can be 
 clicked. The selected image is converted to binary form before insertion and a 
 database stored procedure "setProductImage" is called, to which the selected image is 
 passed as LOB parameter. The image passed to  the stored procedure could be of
 size greater than 32K also. This bypasses the PL/SQL limitation of passing parameters
 greater than 32K.
 
 Note :The maximum size of image(LOB) that can be passed as  OUT parameter to stored 
       procedure with OraOLEDB is 64K. Images greater than this size may not be
	   retrieved properly from database.
*********************************************************************************************
*/

//include standard namespaces used by the sample
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace LOBSample
{
    //This class is used for adding the product names and ids retrieved from
	//database into the list box.
	//The object of this class is added to list box for every product .The 'display' string 
	//is shown in the listbox and 'data' string is used when list box item is selected
	public class ComboData
	{
		public ComboData(string display,string data)
		{
			this.display=display; 
			this.data=data;
		}
		public override string ToString() 
		{
			return display;
		}
		
		public string display;    //variable for name
		public string data;       //variable for id
	}


	/// <summary>
	/// Summary description for StartForm.
	/// </summary>
	public class StartForm : System.Windows.Forms.Form
	{
		//Declaration of dialog box components
		private System.Windows.Forms.Label label1;
		public System.Windows.Forms.ComboBox comboProduct;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button cmdBrowse;
		private System.Windows.Forms.Button cmdUpdate;
		private System.Windows.Forms.Button cmdCancel;
		private System.Windows.Forms.PictureBox pictureNew;
		private System.Windows.Forms.PictureBox pictureExisting;
		public string m_newPicture;
        
		//Define bitmab objects for shown the images in picture box
		public System.Drawing.Bitmap myImage;  
		public System.Drawing.Bitmap newImage;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public StartForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
             m_newPicture="";
			
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected  override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cmdUpdate = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.cmdCancel = new System.Windows.Forms.Button();
			this.pictureExisting = new System.Windows.Forms.PictureBox();
			this.cmdBrowse = new System.Windows.Forms.Button();
			this.comboProduct = new System.Windows.Forms.ComboBox();
			this.pictureNew = new System.Windows.Forms.PictureBox();
			this.SuspendLayout();
			// 
			// cmdUpdate
			// 
			this.cmdUpdate.Location = new System.Drawing.Point(248, 424);
			this.cmdUpdate.Name = "cmdUpdate";
			this.cmdUpdate.Size = new System.Drawing.Size(80, 32);
			this.cmdUpdate.TabIndex = 6;
			this.cmdUpdate.Text = "Update";
			this.cmdUpdate.Click += new System.EventHandler(this.cmdUpdate_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(96, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "Select Product";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(24, 80);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 24);
			this.label2.TabIndex = 2;
			this.label2.Text = "Existing Image";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(24, 240);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(96, 24);
			this.label3.TabIndex = 3;
			this.label3.Text = "New Image";
			// 
			// cmdCancel
			// 
			this.cmdCancel.Location = new System.Drawing.Point(360, 424);
			this.cmdCancel.Name = "cmdCancel";
			this.cmdCancel.Size = new System.Drawing.Size(80, 32);
			this.cmdCancel.TabIndex = 7;
			this.cmdCancel.Text = "Cancel";
			this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
			// 
			// pictureExisting
			// 
			this.pictureExisting.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pictureExisting.Location = new System.Drawing.Point(168, 80);
			this.pictureExisting.Name = "pictureExisting";
			this.pictureExisting.Size = new System.Drawing.Size(176, 144);
			this.pictureExisting.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureExisting.TabIndex = 4;
			this.pictureExisting.TabStop = false;
			// 
			// cmdBrowse
			// 
			this.cmdBrowse.Location = new System.Drawing.Point(360, 360);
			this.cmdBrowse.Name = "cmdBrowse";
			this.cmdBrowse.Size = new System.Drawing.Size(80, 32);
			this.cmdBrowse.TabIndex = 5;
			this.cmdBrowse.Text = "Browse...";
			this.cmdBrowse.Click += new System.EventHandler(this.cmdBrowse_Click);
			// 
			// comboProduct
			// 
			this.comboProduct.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboProduct.DropDownWidth = 176;
			this.comboProduct.Location = new System.Drawing.Point(168, 24);
			this.comboProduct.Name = "comboProduct";
			this.comboProduct.Size = new System.Drawing.Size(176, 24);
			this.comboProduct.TabIndex = 1;
			this.comboProduct.SelectedIndexChanged += new System.EventHandler(this.comboProduct_SelectedIndexChanged);
			// 
			// pictureNew
			// 
			this.pictureNew.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pictureNew.Location = new System.Drawing.Point(168, 248);
			this.pictureNew.Name = "pictureNew";
			this.pictureNew.Size = new System.Drawing.Size(176, 144);
			this.pictureNew.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureNew.TabIndex = 4;
			this.pictureNew.TabStop = false;
			// 
			// StartForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(456, 469);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.pictureExisting,
																		  this.cmdCancel,
																		  this.cmdUpdate,
																		  this.cmdBrowse,
																		  this.pictureNew,
																		  this.label3,
																		  this.label2,
																		  this.comboProduct,
																		  this.label1});
			this.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "StartForm";
			this.Text = "Product Image";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// Here product names are retrieved from database and shown in listbox
		/// </summary>
		[STAThread]
		static void Main() 
		{   //Create an instance of StartForm
			StartForm imgDlg= new StartForm();

			//Object for handling database functions
			OraLOB dbObj= new OraLOB();    

			//Open connection to database
			bool check = dbObj.ConnectDatabase();

			if (check==true)     //if connected
			{
				//Get the product names from database into the listbox
				bool retval = dbObj.getProductNames(imgDlg);
				if (retval==true)
				{
					//close database connection
					dbObj.CloseDatabase();
				
					//Run the application and show the startup form
					Application.Run(imgDlg);
				}
			}
			else
			{   System.Windows.Forms.MessageBox.Show("Cannot Connect to database");
				Application.Exit();
			}
       
		}
		
        /**************************************************************************
		 This function is called when list box selection is changed. It gets the id
		 of selected product, retrieves the image for it from database and shows it
		 in the picture box
		**************************************************************************/ 
		private void comboProduct_SelectedIndexChanged(object sender, System.EventArgs e)
		{   
			//Get the id of selected product .It returns the object of ComboData class
			//whose data variable returns the product id.
			string productid=((ComboData)comboProduct.SelectedItem).data;

			OraLOB dbObj= new OraLOB();    //Object for handling database functions

			//Make a connection to database
			bool retval = dbObj.ConnectDatabase();

			if (retval==true)
			{   
				//set the image in picture boxes to null
				pictureExisting.Image=null;
				pictureNew.Image=null;

				//Release the bitmap used for existing image picture box
				if(myImage!=null)
                   myImage.Dispose();

				//Relase the bitmap used for new image picture box
				if(newImage!=null)
					newImage.Dispose();

				//Call the function to get image from database
				bool check = dbObj.GetProductImage(productid);
			    dbObj.CloseDatabase();
				if (check==true)  //If image was retrieved successfully
            	{  
					//Show the image written in temporary file in picture box
					 myImage=new System.Drawing.Bitmap(Application.StartupPath+"/tempimage.gif");;
					pictureExisting.Image=(Image)myImage;
				}
				
			}
			else
                    System.Windows.Forms.MessageBox.Show("Cannot Connect to database");
			
		}
        
		/********************************************************************
		 * This function is called on click event of 'Browse' button. It opens
		 * up a file dialog box . When a particular image file is selected,
		 * the image is shown in picture box for new image
		 * ******************************************************************/
		private void cmdBrowse_Click(object sender, System.EventArgs e)
		{
			
            //Create an instance of openfile dialog
			System.Windows.Forms.OpenFileDialog fileDlg=new System.Windows.Forms.OpenFileDialog();
			
			//filter the types of files shown to image files
			fileDlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";

			fileDlg.RestoreDirectory = true ;
 
			//Show the dialog box
			if(fileDlg.ShowDialog()== DialogResult.OK)
			{
				//Get the name of selected file into a variable
				m_newPicture=fileDlg.FileName;	

				//Create a bitmap for selected image
		        newImage= new System.Drawing.Bitmap(m_newPicture);

				//Show the bitmap in picture box
               pictureNew.Image=(Image)newImage;
           	}
			
			fileDlg=null;
			
		}

		/************************************************************************
		 * This function is called when update button is clicked. It gets the 
		 * selected product and new image for it and updates it to the database.
		 * ***********************************************************************/
		private void cmdUpdate_Click(object sender, System.EventArgs e)
		{
			string productid="";
			
			//Check if user has selected product and a new image for it
			if((comboProduct.SelectedIndex==-1)||m_newPicture.Equals(""))
				System.Windows.Forms.MessageBox.Show("Select Product and New Image");
			else
			{ 
				//Get the product id of selected product. Selected item is returned
				//as object of ComboData class whose data variable contains the 
				//product id.
				productid=((ComboData)comboProduct.SelectedItem).data;
				OraLOB dbObj= new OraLOB();  //Object to handle database function

				//Connect to the database
				bool check = dbObj.ConnectDatabase();
				
                if (check==true)  //if connected
				{   
					//Call the database procedure to update the product image
					bool retval= dbObj.updateProductImage(productid,m_newPicture);
					
					if(retval==true)  //if image was updated
						System.Windows.Forms.MessageBox.Show("Product Image Updated successfully");
					else
						System.Windows.Forms.MessageBox.Show("Image cannot be updated");

					dbObj.CloseDatabase();
				}
				else
					System.Windows.Forms.MessageBox.Show("Cannot Connect to database");
			}
		}

        /*************************************************************
		 * This function is called when cancel button is clicked. It 
		 * cleans up the resources used by application and exits
		 * *********************************************************/ 
		private void cmdCancel_Click(object sender, System.EventArgs e)
		{   
			//if bitmap objects are not null , dispose them
			if(myImage!=null)
				myImage.Dispose();
			if(newImage!=null)
				newImage.Dispose();
			
			//Delete the temporary file created by the application
			System.IO.File.Delete(System.Windows.Forms.Application.StartupPath +"/tempimage.gif"); 
			
			//Close the form and exit
			this.Close();
			Application.Exit();
		}
	}
}

⌨️ 快捷键说明

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