Using a SQL Table instead of a Dictionary Reference in C#

Right now I have all of my data that I need for my program stored in a separate class Tool and within that class their is a Dictionary<int, string> myTools. I have heard that for future use, it would be better to store this in a table within an SQL Database.

Here’s what I have now:

  • SQL sum by year report, looking for an elegant solution
  • T-SQL to convert Excel Date Serial Number to Regular Date
  • UWP Communication with SQL Server Using .Net Standard 2.0
  • Login failed for user ''
  • In SSIS, how do I get the number of rows returned from the Source that SHOULD be processed
  • Generate SQL Create Scripts for existing tables with Query
  • public static Dictionary<int, string> myTools = new Dictionary<int, string>()
        {1001, "Tool 1"},
        {1002, "Tool 2"},
        {1003, "Tool 3"},
        //...and so on...

    With this in my program, I am able to reference the integer associated and the output the string “name” that is attached with that integer using something like this: Tool.myTools[1002]. I like this method because I can easily change up the names of the tools without having to change the way they are called. However, I would like to ultimately control the tool names for multiple programs all through one database, and that is why I am looking to essentially copy this Dictionary class over to a SQL server. I think this is done in the form of a single table, but I am BARELY experienced with SQL Server Management Studio, and that is why I am coming here for help.

    What I want to know: How can I create a table in SQL that allows me to reference the tools by their corresponding integer similar to the way I am already doing it? If it is as simple as copying all the data into a table and then referencing it with a slightly different function, then I apologize for the simple question, and a simple answer would still be welcomed!

    Thanks in advance!

    EDIT: Apparently I didn’t give myself enough credit. I already have a table created within a database through SQL Server Management Studio. This table just has two columns, number and name. If this is wrong, then I was asking what I need to do to change or fix it, and then if this is correct, I just want to know what I need to do within Visual Studio in order to reference these tools just like I can with the dictionary. Is it really as simple as importing the database and then referencing it with db.Tools.[number] or something like that?

  • How delete many-to-many relation in Entity Framework 6
  • SQL Server - Inserted Table
  • Safety from SQL injection
  • Returning table with CLR
  • In MVC4, how do I upload a file (an image) to SQL Server that's part of my domain model?
  • Can the script for median be generalised and put into a function type structure
  • One Solution collect form web for “Using a SQL Table instead of a Dictionary Reference in C#”

    I would recommend googling some entity framework turorials, particular Code First.

    When you learn how to create models, this would be a good starting point for your model:

    class Tool { 
      int ToolId {get;set;}  
      string Name {get;set;}

    To grab a specific tool

    Tool myTool = db.Tools.Single(t=>t.ToolId == theVariableWithToolIdIWant);
    string theNameOfTheTool = myTool.Name;

    This won’t make much sense at the moment, but after you go through some tutorials and setup your database context, hopefully it will help give you a jump start on what you want to do.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.