Beispiel #1
0
 //Update Photo in DB
 private void insertPicture()
 {
     if (fileName != "")
     {
         //code from http://stackoverflow.com/questions/779211/programmatically-managing-microsoft-access-attachment-typed-field-with-net
         DBEngine  dbe = new DBEngine();
         Database  db  = dbe.OpenDatabase("PineSpringsPottery.accdb", false, false, "");
         Recordset rs  = db.OpenRecordset("SELECT * FROM PATTERN WHERE PATTERN.PatternNo = " + currentPattern.patternNo, RecordsetTypeEnum.dbOpenDynaset, 0, LockTypeEnum.dbOptimistic);
         rs.MoveFirst();
         rs.Edit();
         Recordset2 rs2 = (Recordset2)rs.Fields["PatternPicture"].Value;
         //delete previous pics
         if (rs2.RecordCount != 0)
         {
             rs2.Delete();
         }
         //if not just deleting previous, add new
         if (fileName != "delete")
         {
             rs2.AddNew();
             Field2 f2 = (Field2)rs2.Fields["FileData"];
             f2.LoadFromFile(fileName);
             rs2._30_Update();
         }
         rs2.Close();
         rs._30_Update();
         rs.Close();
     }
 }
    private void Form1_Load(object sender, EventArgs e)
    {
        DBEngine  dbe    = new DBEngine();
        Database  db     = dbe.OpenDatabase(@"C:\Users\Public\AttachmentsDB.accdb");
        Recordset rsMain = db.OpenRecordset(
            "select solution from tab2 where id = 1",
            RecordsetTypeEnum.dbOpenSnapshot);
        Recordset2 rsAttach  = rsMain.Fields["solution"].Value;
        Field2     fldAttach = (Field2)rsAttach.Fields["FileData"];

        tempFileName = System.IO.Path.GetTempPath() + "\\" + rsAttach.Fields["FileName"].Value;
        try
        {
            System.IO.File.Delete(tempFileName);
        }
        catch { }
        fldAttach.SaveToFile(tempFileName);
        rsAttach.Close();
        rsMain.Close();
        db.Close();
        img = Image.FromFile(tempFileName);
        pictureBox1.Image = img;
    }