Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i have 2 textboxes and one is barcode image i want to add it in sql server and retrive it how can i do, can u help me ?
Posted 14-Dec-12 5:21am
Edited 14-Dec-12 6:33am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

HI,
 
Try this solution.
 
public void saveImage(File file){
        try {
            String img_id=JOptionPane.showInputDialog("Enter Image ID");
            FileInputStream fis=null;
            String query="insert into image(image_id,image) values (?,?)";
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:image");
            PreparedStatement pstm=con.prepareStatement(query);
            fis=new FileInputStream(file);
            pstm.setString(1, img_id);       
            pstm.setBinaryStream(2, (InputStream)fis, (int)file.length());
            pstm.executeUpdate();
            JOptionPane.showMessageDialog(null, "Image Successfully Uploaded to Database");
            pstm.close();
            con.close();
 
        } catch (Exception ex) {
            System.out.println("Exception Occured: "+ex);
        }
    }
 
And Retrieve it by again making Image from binary and Save it on some physical Location:
public void getSavedImages(){
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:image");
            PreparedStatement pstm1 = con.prepareStatement("select * from image");
            ResultSet rs1 = pstm1.executeQuery();
            while(rs1.next()) {
                InputStream fis1;
                FileOutputStream fos;
                String image_id;
                try {
                    fis1 = rs1.getBinaryStream("image");
                    image_id=rs1.getString("image_id");
                    fos = new FileOutputStream(new File(Path to "C:\\" + (image_id) + "Your Extension(.jpg/.gif)"));
                    int c;
                    while ((c = fis1.read()) != -1) {
                        fos.write(c);
                    }
                    fis1.close();
                    fos.close();
 
                } catch (Exception ex) {
                    System.out.println(ex);
                }
            } 
            pstm1.close();
            con.close();
        } catch (Exception ex) {
            System.out.println("Exception Occured:"+ex);
        }
    }
 
Thanks
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can refer to following post to save the Image in Database
Save Image To Database[^]
//Read the byte array from database and then use the following method to //convert the byte array back into image
 public static Image DB2Image(byte[] byteArrayIn)
  {
   MemoryStream ms = new MemoryStream(byteArrayIn);
   Image returnImage = Image.FromStream(ms);
   return returnImage;
  }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

For save Image file,u can use
fileuplode.posted file.filename
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 165
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100