Home > Enterprise >  Compare text file data with database records in C#
Compare text file data with database records in C#

Time:10-21

I have to make a select statement on a database and than compare the results with a text file using only C# in Visual Studio. If the text file has a bigger value than the record in the database, the program returns the value from the text file, and if the database record has a bigger value, the program returns the value from the database. The results are added to a list being a class WynikPorownania{}. The user types in the file an index of a product and the value(in this case the value is it's availability condition).

For example:

The textfile says this:

WYR_WR_CZ1=12

VIDIS_JIMU_BOX=3

REREK_KOTEK_T=5

In the database, theses indexes are connected to an availability condition like this

WYR_WR_CZ1=-1.0000

VIDIS_JIMU_BOX=-13.0000

REREK_KOTEK_T=0.0000

Now the program should return the bigger value in a list being the WynikPorownania{} class.

For now, I managed to do this much: I took every record from the select query and put it in to a list as a class. I have a function that checks the "standysp"(availability condition) for a specified index, and I asigned the text file value to a string. I think that it could be done maybe with this "zwr" function and maybe a loop but I don't really know where to go from now on. This is my code for now:

    using System;
using FirebirdSql.Data.FirebirdClient;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace dokselect
{
    class indexstan
    {
        public string index;
        public double standysp;
    }

    class WynikPorownania
    {
        public string Indeks;
        public int Ilosc;
    }
    class Program
    {
        public static void Main()
        {
            ///////CONNECTION

            string conn = "database=C:/PCBiznes/BAZA/IXION2_LOGMAG.FB;user=SYSDBA;password=masterkey;DataSource=192.168.24.112;Port=3050";
            FbConnection myConnection = new FbConnection(conn);
            FbDataReader myReader = null;

            string sql = "select KARTOTEKA.indeks,STANMAG.standysp FROM kartoteka JOIN stanmag using(ID_KARTOTEKA);";
            FbCommand myCommand = new FbCommand(sql, myConnection);

            myConnection.Open();
            myReader = myCommand.ExecuteReader();

            ///////LIST lista1
            List<indexstan> lista1 = new List<indexstan>();
            double standysp;
            string index;
            while (myReader.Read())
            {
                index = myReader[0].ToString();
                standysp = Convert.ToDouble(myReader[1]);
                lista1.Add(new indexstan { index=index, standysp=standysp });
                //Console.WriteLine(myReader[0].ToString());
            }
            myConnection.Close();
            Console.WriteLine(lista1.Count);


            //RETURN STANDYSP FUNCTION
            double zwr(string myIndex)
            {
                var result = lista1.FirstOrDefault(lista1 => lista1.index == myIndex).standysp;
                return result;
            }
            zwr("EMPIS_DESKA_FASOLKA");

            //READ FROM TXT
            string path = "C:/Users/Praktykant/Documents/textdocs/dok1.txt";
            string plik = File.ReadAllText(path);
            //Console.WriteLine(plik);
            StreamReader objReader = new StreamReader(path);

            myConnection.Close();
        }
    }

CodePudding user response:

You mean something like this?

var list = File.ReadAllLines(path).Select(line =>
{
    var tokens = line.Split("=");
    var index = tokens[0];
    var value = int.Parse(tokens[1]);
    return new WynikPorownania
    {
        Indeks = index,
        Ilosc = (int)Math.Max(value, zwr(index).standysp)
    }
}).ToList();

(Depending on the size of the file, a StreamReader would be better than ReadAllLines)

CodePudding user response:

You're almost there.

Extract text file line by line, and compute bigger value

StringReader r = new StringReader(plik);
string line;
while((line = r.ReadLine()) != null)
{
    string index = line.Split('=')[0];
    string textValue = line.Split('=')[1];
    double biggerValue = Math.Max(
        zwr(index),
        double.Parse(textValue);

    Console.WriteLine($"{index} > {biggerValue}");
}

  • Related