Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 14-Dec-12 5:33am
v2
Comments
[no name] 14-Dec-12 10:29am    
Do u want to save the image in the database or you want the image path to be stored in the database?

HI,

Try this solution.

C#
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:
C#
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
 
Share this answer
 
You can refer to following post to save the Image in Database
Save Image To Database[^]
C#
//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;
  }
 
Share this answer
 
v2
For save Image file,u can use
fileuplode.posted file.filename
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900