Hi there,

I'm working on an application for a lab project and I'm making it in C#. It's supposed to import results from a text file that is exported from the application we use to run the tests and right now, I've hit a road block.

I've gotten the program to save around 250 decimal values as a single-dimension array but then I'm trying to get the array itself to be able to saved in an SQL database so that I can later retrieve the array and use the decimal values to construct a plot of the points.

I need the entire array to be imported into the database as one single value though because the lab project has many more specimens each with their own set of 250 or so Decimal points (which will be stored as arrays, too)

Thanks for your help.

Unless you definitely need to store all your values in a single database, why not leave it as it is and make your application read out a directory, where all your separate data files are stored and make as many plots as you want?
It is not because SQL databases exist, you have to use one.

Unless you definitely need to store all your values in a single database, why not leave it as it is and make your application read out a directory, where all your separate data files are stored and make as many plots as you want?
It is not because SQL databases exist, you have to use one.

Yeah that would probably work, too but I wanted to use a database so I could query it since there will be several specimens and each specimen has several tests run on it each with its own results...

Try this:

private static decimal[] GetTestData()
    {
      List<decimal> lst = new List<decimal>();
      for (int i1 = 0; i1 < 250; i1++)
        lst.Add(Convert.ToDecimal(i1));
      return lst.ToArray();
    }

    private static string PackArray(decimal[] values)
    {
      List<string> lst = new List<string>();
      foreach (decimal d in values)
        lst.Add(Convert.ToString(d));
      return string.Join(",", lst.ToArray());
    }
    private static decimal[] UnpackArray(string valueLine)
    {
      List<decimal> lst = new List<decimal>();
      string[] values = valueLine.Split(new char[] { ',' }, StringSplitOptions.None);
      foreach (string s in values)
        lst.Add(Convert.ToDecimal(s));
      return lst.ToArray();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      decimal[] testData = GetTestData();
      string lineToSaveToDatabase = PackArray(testData);
      decimal[] unpackedData = UnpackArray(lineToSaveToDatabase);
      System.Diagnostics.Debugger.Break();
    }

Try this:

private static decimal[] GetTestData()
    {
      List<decimal> lst = new List<decimal>();
      for (int i1 = 0; i1 < 250; i1++)
        lst.Add(Convert.ToDecimal(i1));
      return lst.ToArray();
    }

    private static string PackArray(decimal[] values)
    {
      List<string> lst = new List<string>();
      foreach (decimal d in values)
        lst.Add(Convert.ToString(d));
      return string.Join(",", lst.ToArray());
    }
    private static decimal[] UnpackArray(string valueLine)
    {
      List<decimal> lst = new List<decimal>();
      string[] values = valueLine.Split(new char[] { ',' }, StringSplitOptions.None);
      foreach (string s in values)
        lst.Add(Convert.ToDecimal(s));
      return lst.ToArray();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      decimal[] testData = GetTestData();
      string lineToSaveToDatabase = PackArray(testData);
      decimal[] unpackedData = UnpackArray(lineToSaveToDatabase);
      System.Diagnostics.Debugger.Break();
    }

Well, I'm really new to C# development.. What would that do?

I was planning on serializing the Decimal Array and storing that in the SQL database.

It does that. PackArray turns it in to a CSV of decimals: 1.0000,2.0000,3.0000 and UnpackArray parses the commas and returns them as an array of decimals. You could use XML serialization but that is overkill for this task. The benefit of using CSV here is you can keep the size of the varchar() column to a minimum. If you have a large serialized class you may have to go to varchar(max) which will not give you nearly the performance of a smaller table definition.

Run the code and try it out :)

This article has been dead for over six months. Start a new discussion instead.