Tuesday, 21 August 2012

c# tutorial

 CODE TO INSERT IMAGE INTO DATABASE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace pictureProjectIlinqToSql
{
    public partial class Picture : Form
    {
        public Picture()
        {
            InitializeComponent();
        }

        private void btnsubmit_Click(object sender, EventArgs e)
        {
            bool resp = inserstudent();
        
                MessageBox.Show(resp.ToString());
        
        }
        OpenFileDialog opendiag;
        string path;
        private void btnbrowse_Click(object sender, EventArgs e)
        {
            opendiag = new OpenFileDialog();
            opendiag.ShowDialog();
            pictureBoxsubmitpicture.SizeMode = PictureBoxSizeMode.StretchImage;
            path = opendiag.FileName;
            pictureBoxsubmitpicture.Image = Image.FromFile(path);
            insertImage();
         
        }
     
        public void getpicturePath()
        {
          
        }
        byte[] pix;
        FileStream fs;
        bool flag;
        public bool insertImage()
        {

            fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            pix=new byte[fs.Length];
            fs.Read(pix, 0, Convert.ToInt32(fs.Length));

         

            return true;
        }
        public bool inserstudent()
        {
         
            StudentDataContext stu = new StudentDataContext();
            StudeterDataContext stud = new StudeterDataContext();
          
            StudentTB stub = new StudentTB()
            {
              
                Matricno = txtboxmatricno.Text,
                photo = pix,

            };
                stud.StudentTBs.InsertOnSubmit(stub);
                stud.SubmitChanges();
         
            return true;
        }
    }
}



No comments:

Post a Comment