9 Replies - 1050 Views - Last Post: 19 December 2014 - 03:33 AM Rate Topic: -----

#1 Sven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-December 14

What is the best way to store and load an image into an Access Datab

Posted 12 December 2014 - 01:44 PM

I'm trying to find a monkey proof solution to the following problem. If anything should be unclear, I apologize in advance. I'm not a native English speaker and I'm definitely not an experienced programmer.

I have:

An Access database in which the picture right now is saved by its directory, together with other field like title, publisher, developer,..
Visual Studio form in which a load button gets that directory as text
Details button that reads the ID of a selected item (I have a list of games) and displays the corresponding picture by reading the directory

The problem right now is that once someone else opens the program on his/her computer, the directory changes, making the whole thing useless.

Is there a way to get around this problem? The books I've been reading don't really adress this.

This is what I believe is the relevant code I've made so far;

Directory is a string in class 'Game'; it's short text in the database

private void buttonFoto_Click(object sender, EventArgs e)
{
    LoadNewFile();

}

private void LoadNewFile()
{
    OpenFileDialog ofd = new OpenFileDialog();
    System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
    if (dr == DialogResult.OK)
    {
        userSelectedFilePath = ofd.FileName;
    }
}

public string userSelectedFilePath
{
    get
    {
        return textBoxDirectory.Text;
    }
    set
    {
        textBoxDirectory.Text = value;
    }
}

public static void Insert(Game game)
{
    OleDbConnection conn = new OleDbConnection(connectionString);
    OleDbCommand command = new OleDbCommand();
    OleDbDataAdapter adapter = new OleDbDataAdapter();

    conn.Open();

    string commando = "INSERT INTO Games " +
                "(Titel, Genre, Developer, Publisher, foto, prijs) " +
                "VALUES ('" + game.Titel + "','" + game.Genre + "', '" + game.Developer + "','" + game.Publisher + "','" +game.Directory+"','"+game.Prijs+"')";

    command.Connection = conn;
    command.CommandText = commando;
    adapter.InsertCommand = command;

    adapter.InsertCommand.ExecuteNonQuery(); 

    conn.Close();
}

private void buttonDetails_Click(object sender, EventArgs e)
{
    Graphics paper = pictureBox1.CreateGraphics();

    paper.Clear(Color.FromKnownColor(KnownColor.ActiveBorder));

    int gameId = Convert.ToInt32(listBoxGames.SelectedValue);
    selectedGame = GameRepository.GetGameById(gameId);

    listBoxPunten.DataSource = selectedGame.Reviewlijst;
    listBoxPunten.DisplayMember = "punten";

    textBoxGameId.Text = Convert.ToString(selectedGame.GameId);
    textBoxDeveloper.Text = selectedGame.Developer;
    textBoxGenre.Text = selectedGame.Genre;
    textBoxPublisher.Text = selectedGame.Publisher;
    textBoxTitel.Text = selectedGame.Titel;
    textBoxPrijs.Text = Convert.ToString(selectedGame.Prijs);

    Bitmap pic = new Bitmap(selectedGame.Directory);

    pic.SetResolution(pictureBox1.Width/6, pictureBox1.Height/7);
    paper.DrawImage(pic,0,0);

    groupBoxDetails.Visible = true;  
} 

public static Game GetGameById(int id)
{
    string command;
    command = "SELECT gameid, titel, genre, developer, publisher, prijs,foto " 
        + "FROM Games WHERE gameid =" + id;

    OleDbDataAdapter adapter = 
    new OleDbDataAdapter(command, connectionString);
    DataTable datatable = new DataTable();
    adapter.Fill(datatable);

    if (datatable.Rows.Count > 0)
    {
        Game game = new Game(); 

        game.GameId = datatable.Rows[0].Field<int>("gameid"); 
        game.Titel = datatable.Rows[0].Field<string>("titel");
        game.Genre = datatable.Rows[0].Field<string>("genre");
        game.Developer = datatable.Rows[0].Field<string>("developer");
        game.Publisher = datatable.Rows[0].Field<string>("publisher");
        game.Prijs = datatable.Rows[0].Field<double>("prijs");
        game.Directory = datatable.Rows[0].Field<string>("foto");

        return game;
    }
    else
    {
        return null;
    }}


Hopefully this is sufficient to be able to help me. If not, please tell me what's missing and I'll add it straightaway.

I've tried searching for a way to force the save directory to one particular map, so the directory is correct every time. If anyone knows how to do that, that would also be a good solution. The second thing I tried was copying the file to the folder I want it to be in using the File.Copy method, but I'm pretty sure I'm doing it wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: What is the best way to store and load an image into an Access Datab

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: What is the best way to store and load an image into an Access Datab

Posted 12 December 2014 - 02:56 PM

Quote

The problem right now is that once someone else opens the program on his/her computer, the directory changes, making the whole thing useless.

Please clarify:

Is this a WinForm Application?
Are you embedding the Access database in your application?
How is it that other users/installers of your application are able to attempt to load images that other users have saved (the path to) in the database?

One thing that I will mention is that you are using OpenFileDialog(), so the user will be saving the location of images according to his/her computer and its file structure. Or will the location be a shared, network location?
Was This Post Helpful? 0
  • +
  • -

#3 Sven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-December 14

Re: What is the best way to store and load an image into an Access Datab

Posted 12 December 2014 - 03:50 PM

Hey Andrew, thanks for your reply

Yes, it's a WinForm application.
And yes, I've added the database into the application.
I'm finding the third question quite difficult to answer, so I'm sorry if it's not exactly what you asked. The reason I think it works now is because the directory of the initial images were put in beforehand (as in directly into the database using Access). I manually stored them in the BIN folder of the project itself. That way it doesn't matter who runs the application or from where. This also meant that I didn't have to fill in the entire directory, but only the file name. (eg Bioshock.jpg instead of //files/...) Loading the image codewise is found within the 'detailsbutton'. The text in the field of the database is read and then used in the Bitmap class.

From then on, once a user saves a new picture, and perhaps doesn't put it in the BIN folder, the entire directory is stored. So in a way the application works if it's only passed on once.

The OpenFileDialog does not lead to a shared location. It simply opens the computer's file structure. So at the moment the user can easily save the picture wherever he or she wants.

This post has been edited by andrewsw: 12 December 2014 - 03:54 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: What is the best way to store and load an image into an Access Datab

Posted 12 December 2014 - 04:01 PM

Quote

From then on, once a user saves a new picture, and perhaps doesn't put it in the BIN folder, the entire directory is stored. So in a way the application works if it's only passed on once.

I am still a little confused. If you are passing the application from person to person, and each person stores the location to an image on their computer, then this location, and the image, won't achieve anything for the next person.

Perhaps a silly question, but you don't think that saving the location will actually save the image itself in the database do you?



Note that it is not necessary to quote the previous post in full, there is a Reply button further down the page.
Was This Post Helpful? 0
  • +
  • -

#5 Sven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-December 14

Re: What is the best way to store and load an image into an Access Datab

Posted 12 December 2014 - 04:22 PM

No, I'm aware of that. The application will work for the first person it's passed on to, but not for anyone after that. The reason for using the directory was to minimize the size of the database. But it turns out it's just giving me more problems. And that's exactly my question. What is the best way to save and load an image from and into a database so that it works no matter who uses the application?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: What is the best way to store and load an image into an Access Datab

Posted 12 December 2014 - 05:22 PM

Do you understand why storing the image-location doesn't, and won't, work?



I have a VB snippet here that demonstrates saving and loading images to an Access database (OLE Object field). You can use this translator to C# if it helps.

Alternatively, it should be possible to save images to a folder with (within) the application, perhaps as newly saved resources. In which case you would only need to save the image names in the database, as they would all be located in the same place. This approach is preferable to bloating the database with images, and avoids the messy packing and unpacking (MemoryStream) of the images.



I'll mention that your approach is a little odd. Only one person will have the current version of your application at any one time. But it seems that this is your intention ;)

This post has been edited by andrewsw: 12 December 2014 - 05:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: What is the best way to store and load an image into an Access Datab

Posted 12 December 2014 - 05:59 PM

I am curious, how are you adding the Access database to your application, and successfully moving it around each time? Embedding (on its own) isn't a solution to this.

This post has been edited by andrewsw: 12 December 2014 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

#8 Sven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-December 14

Re: What is the best way to store and load an image into an Access Datab

Posted 13 December 2014 - 02:57 AM

It would be great if you could tell me how to make it so that users can only save in the folder of the project itself. Like you said, that way only the folder name is sufficient. I'm ok with users only having the current version.

The way I've put in the database is by adding it to the project in Visual Studio (add existing,...), if that's what you mean. That, and, saving the access file in the project folder itself. It's the project that will be passed around. So it's being opened in Visual Studio.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: What is the best way to store and load an image into an Access Datab

Posted 13 December 2014 - 03:55 AM

Quote

It would be great if you could tell me how to make it so that users can only save in the folder of the project itself.

This description puzzles me a little. When the user has selected an image using the OpenFileDialog() you can copy the file from where it is currently to a folder within your project, writing the filename to the database. File.Copy method

It is more complicated to write the image as a new Resource but..

Quote

It's the project that will be passed around. So it's being opened in Visual Studio.

.. as you are just passing the Project files themselves you might as well just copy the images to a sub-folder within your Project.
Was This Post Helpful? 0
  • +
  • -

#10 Mohsin01   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 13-April 12

Re: What is the best way to store and load an image into an Access Datab

Posted 19 December 2014 - 03:33 AM

I would say a simple thing for you to do. it will not make the db much heavy but i think its what you are looking for.

Convert the image into binary data and save it in db. when you wanna get the image convert it into image form again. its best for you i guess.

thats what i did in my library management project :P

This post has been edited by Mohsin01: 19 December 2014 - 03:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1