1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

What is the best way to store and access data in tools/bots made in C#

Discussion in 'C, C++, C#' started by sweguy, Jan 23, 2016.

  1. sweguy

    sweguy Regular Member

    Joined:
    Jan 6, 2014
    Messages:
    292
    Likes Received:
    66
    Location:
    top of Mt.Alpen
    I have made an tool in c# as my first project. So am quite new to c# for starters.
    My tool works fine, but i want to make it multithread this is where my brain crash.


    So right now when i click start on my tool, it makes and array and a list and populates them.
    So far so good. Issue here is when i want to add threading to it i get stuck.

    So i am trying to move my main function that do the actions to an class but then it cant access the list or array.
    So i tested to put them in class also and populate them. But it seems to be tricky to read the data from them or copy them to the class that need its info.

    So my question is, how do you normally store your data?

    For example lets say we make a social network bot.

    We store login,pass,proxy etc is it best to work vs a database?
    Is it possible to access the database from all classes etc? If so what database should i go for?


    I would be greatful for all help i can get, its really fun to program but now am stuck for 3 days on this cant really figure it out so need some advices :)

    //Sweguy
     
  2. opahopa

    opahopa Junior Member

    Joined:
    Sep 23, 2012
    Messages:
    164
    Likes Received:
    10
    So my question is, how do you normally store your data?
    -depends on project size, usually for small bots making a data cache in software.

    We store login,pass,proxy etc is it best to work vs a database?
    -data cache implementation / store this info in files, upload on software's load through settings.

    Is it possible to access the database from all classes etc? If so what database should i go for?
    -possible
    -any, up to u. personally using MySQL, PostgreSQL.
     
  3. sweguy

    sweguy Regular Member

    Joined:
    Jan 6, 2014
    Messages:
    292
    Likes Received:
    66
    Location:
    top of Mt.Alpen
    Right now i store my data in Arrays and Lists.

    Issue with this is i dont know how to access the data from other classes.
    This is why i started to wounder if database would make my life more easy.


    For example now i am making a small bot for learning.
    And i have an import button to import acc info to a datagrid.
    Then i can mark those acc i want to use in checkboxes. When i click start it checks for all marked accounts and put them into array.
    Then i grab the info from array before i do the action.

    But now i wanted to inplant mulithread to this bot. And i want to have my functions in diff class so i can call them easy for the threading part. And here is my issue when they are in other class it cant read the array as its "locale". Same with List. And after much reading it seems to read array from other class seems hard. So gotta be an more easy way doing this. And thats when i tought of databases.
     
  4. opahopa

    opahopa Junior Member

    Joined:
    Sep 23, 2012
    Messages:
    164
    Likes Received:
    10
    "Issue with this is i dont know how to access the data from other classes."

    the easiest example - implement a Singleton class and use it as dataCache.
    https://msdn.microsoft.com/en-us/library/ff650316.aspx
    don't forget to use locks on it's object's manipulation if u use multi-threading.

    "And i want to have my functions in diff class so i can call them easy for the threading part"
    -you can do that, if u use a dataCache to access data. DB is also an option.

    For multithreading, suggesting to use some Task implementation. https://msdn.microsoft.com/en-us/library/ee782519(v=vs.110).aspx
     
  5. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    693
    Likes Received:
    340
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    You don't need a database to store your data (sometimes you do and it is best - sometimes you don't)


    If is a simple program, and you are just storing basic data, then a simple text file is sufficient.


    If you are storing a social media account. You could simply write it as a text file, reading a value from each line

    Code:
    version
    username
    email
    password
    
    
    

    The version is required, as later you may decide you want to add more details, perhaps "niche" but that would only exist for version 3, so for files version<3 you wouldn't try and read it.


    Benefits of a simple text file, is you can edit by hand easily.


    Other ways to store data would be:
    xml
    json
    binary



    You can write these manually yourself, or take a look at Serialisation. Which can serialise your data objects


    Code:
    class SocialAccount{
    
    public string Username{get;set;}
    public string Email{get;set;}
    public string Password{get;set;}
    }
    


    automatically for you, without you having to write the code to save and load. Although they will be slower than a simple readlline() system - but not enough that you notice unless you processing large amounts





    Or if you want a kind of database, look at SqlLite. Although for what you need, I wouldn't bother myself
     
  6. NullReferenceX

    NullReferenceX Newbie

    Joined:
    Dec 1, 2015
    Messages:
    41
    Likes Received:
    83
    Occupation:
    Programmer
    Location:
    Germany
    For what your doing serialization would fit you.

    https://msdn.microsoft.com/en-us/library/ms233843.aspx
     
  7. crackcomm

    crackcomm Newbie

    Joined:
    Mar 27, 2008
    Messages:
    23
    Likes Received:
    0
    How about RocksDB or LevelDB?
     
  8. AutomationSorcerer

    AutomationSorcerer Registered Member

    Joined:
    Apr 25, 2015
    Messages:
    73
    Likes Received:
    66
    Occupation:
    Senior Software Engineer
    Location:
    Ethereal Plane
    This is normally when you start using SyncLock's or concurrent versions of collections in the System.Collections.Concurrent namespace.

    It sounds like you're not following object oriented design principles very well. Don't cut corners on proper OOP to make the code easier to solve whatever problem you're currently facing. It will lead to more complex problems later.

    SQL Server, and I use Entity Framework as my ORM. Entity Framework's database first pattern makes it really simple to get database functionality integrated into your application if you know how to design proper databases (settings primary and foreign keys appropriately where needed).

    Yes, any class can use it. Example below creates a connection to the database model defined by the MyEntities database context. It then queries all accounts with a Status of Active and returns a list to them.

    Code:
    using (var db = new MyEntities())
    {
        var accounts = db.Accounts.Where(a => a.Status == AccountStatus.Active).ToList();
        // Do something with it...
    }
    Well this reply might be a little late, but hopefully it helps someone.
     
  9. netomarquis

    netomarquis Junior Member

    Joined:
    Jan 15, 2016
    Messages:
    117
    Likes Received:
    9
    I´ve create several social media bots, I use cache and mssql to do this job, pm me if you need help