La Phyzz
La Phyzz

Reputation: 19

How to make a program create a database or something similar as a save file in c#

Ok, So I am trying to build a program that will allow me to make Recipe database. The Data I am trying to save is as;

class RecipeDataBase
   {
      int RecipeID;
      string RecipeNames;
      string[,] Ingredients = new string[30, 30];
      string[] Steps = new string[30];
    }

What I am trying to get the program to do is first, it has a create new database option, this will create an empty save file in with the recipe database's name (e.g. DinnerRecipes.rdb). Then I have another form that is designed to allow the user to add recipes to whichever database they select and finally I have another one that allows the user to edit the recipes inside the database as well as lists all the current recipe names within whichever database they chose.

The Idea is so I have a program I can create almost digital cookbooks with for another program I am working on. while almost 85% of the code I know and the rest I am getting it this savefile part that I getting me.

Currently, I only have;

//Setting up and restricting the save system
        SaveFileDialog CRDB = new SaveFileDialog();
        CRDB.InitialDirectory = @"./";
        CRDB.RestoreDirectory = true;
        CRDB.FileName = "*.rdb";    
        CRDB.Filter = "Recipe Database |*.rdb";

        //running the save display and if they click ok creating a blank datebase.
        if (CRDB.ShowDialog() == DialogResult.OK)
        {
            //This is to prep the database template for saving and creating the database file.
            RecipeDataBase recipes = new RecipeDataBase();

            Stream fileStream = CRDB.OpenFile();
            StreamWriter sw = new StreamWriter(fileStream);

            sw.Write(recipes);
            sw.Close();
            fileStream.Close();
        }

Ok, I think I might be making this more complex than it really is. however, here is an example of the data I am trying to save

RecipeID = 1;
RecipeNames = "Homemade Pasta Sauce";
Ingredients = ["Tomato", "Basil", "Onions", "Garlic", ect...]["4", "6 leaves or a tablespoon of ground", "3 Medium Sized", "8 clove(minced)", ect..];    
Steps = ["Peel and seed the tomatoes and set aside.", "Chop the onion, mince the garlic, and grate half of the carrot.", "Pour the olive oil into a large stockpot over medium heat.", ect];

What I am worried about saving the data straight into a text file is not every recipe has the same number of ingredients or steps. I want to ensure that when loading the information I don't end up with it pulling the wrong data, I am also wanting to have the RecipeIDs easily searched.

I've spent the last three days trying to find the answer I'm looking for and I don't know if I'm asking it wrong or just not connecting the answers I am finding with my project. Please help me Stackoverflow Kenobi Your may only hope.

Upvotes: 1

Views: 532

Answers (1)

SBFrancies
SBFrancies

Reputation: 4250

You should serialize your data to a format you can save, typically either a JSON or XML string or a binary format such as protobuff or MessagePack. More details on serialization can be found here:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/

and

https://learn.microsoft.com/en-us/dotnet/standard/serialization/basic-serialization

You may have to alter your classes - for example adding default constructors or the [Serializable] attribute.

Some examples of serialization (from the links):

Binary:

MyObject obj = new MyObject();  
obj.n1 = 1;  
obj.n2 = 24;  
obj.str = "Some String";  
IFormatter formatter = new BinaryFormatter();  
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);  
formatter.Serialize(stream, obj);  
stream.Close();  

JSON:

string jsonString = JsonSerializer.Serialize(weatherForecast);
File.WriteAllText(fileName, jsonString);

Upvotes: 1

Related Questions