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 4:21am
Edited 14-Dec-12 5:33am
v2
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  
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  

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

  Print Answers RSS
0 Dnyaneshwar@Pune 604
1 thatraja 370
2 OriginalGriff 319
3 Sergey Alexandrovich Kryukov 241
4 kbrandwijk 204
0 OriginalGriff 737
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 505
3 Sergey Alexandrovich Kryukov 395
4 thatraja 370


Advertise | Privacy | Mobile
Web04 | 2.8.140902.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