上传一个txt文件并保存到数据库中

时间:2017-10-12 07:23:21

标签: c# .net database string winforms

我必须制作一个c#程序,它将读取文本文件并提取所需的数据并创建数据库。文本文件将类似于下面的示例,这只是一个类别国家结果,在国家之后是区域结果,并且对于每个区域都有所有选举位置。所有这些都在同一个文本文件中。我如何阅读这样的大型文本文件,程序可以了解其国家,地区或位置是否采用所有值,以及带有值的名称并将其存储在相应的数据库中?我是新手,我已经编写了一些代码,但是它逐行读取。因此,如果文件中添加了1行,则程序将无法正确读取。

现在我试图找到一种方法来提取我需要的数据。这不完全是文件的方式,因为我不能包含真实数据及其在希腊语中。但是你明白了,现在我只是逐行阅读。但是txt文件必须始终与使用这种方式编写的方式完全相同,以便获得数据的相关性。因为我是2个月的编程新手,所以这是一项非常艰巨的任务:)

COUNTRY RESULTS
ON A TOTAL: 545,491 REGISTERED VOTES (PARTICIPATION = 100,00%)
FROM: 1,138 ELECTION CENTERS (PARTICIPATION = 100,00%)
LOST: 0 CENTERS WITH 0 REGISTERED VOTES
STAY: 91.957 16.86%
VOTED: 453,534 (447,489 + 6,045) 83,14%
NOK: 7.819 1.72%
WHITE: 4.459 0.98%
QUARTERLY: 441.256 97.29%
WRITTEN WITHOUT WARRANTIES: 161
NAME VOTES PERCENTAGE
BOB TAYLOR 1.278 0.29% N / A
MARIA MARIA 213 0,05% T / S
GEORGE GEO 109.996 24.93% T / A
JOHN SOLLA 118.755 26.91% T / S
LISA TERRA 1.898 0.43% T / S


DISTRICT RESULTS
ON A TOTAL: 545,491 REGISTERED VOTES (PARTICIPATION = 100,00%)
FROM: 1,138 ELECTION CENTERS (PARTICIPATION = 100,00%)
LOST: 0 CENTERS WITH 0 REGISTERED VOTES
STAY: 91.957 16.86%
VOTED: 453,534 (447,489 + 6,045) 83,14%


private void insrtBtn_Click(object sender, EventArgs e)
    {
        String line;
        try
        {
            //Pass the file path and file name to the StreamReader constructor
            StreamReader sr = new StreamReader(textBox1.Text);

            //Read the first line of text
            line = sr.ReadLine();

            //Continue to read until you reach end of file
            while (line !=null)
            {
                //write the lie to console window
                MessageBox.Show(line);


                string firstWord = line.Substring(0, line.IndexOf(" "));//Read first word of string
                line = sr.ReadLine();//Read the next line
                switch (firstWord)
                {
                    case "COUNTRY":
                        if(line.StartsWith("ON"))
                        {


                            getOneNumber(line);

                          line = sr.ReadLine();



                        }
                        if (line.StartsWith("FROM:"))
                        {

                            getOneNumber(line);
                            line = sr.ReadLine();

                        }
                        if (line.StartsWith("LOST:"))
                        {

                            line = sr.ReadLine();
                        }

                        if (line.StartsWith("STAY:"))
                        {

                            getTwoNumbers(line);
                            line = sr.ReadLine();

                        }
                        if (line.StartsWith("VOTED:"))
                        {
                            string str = line;
                            str = (str.Remove(0, str.IndexOf(":") + 1)).Trim();
                            string str1 = str.Substring(0, str.IndexOf(" ")).Trim();
                            string str2 = (str.Replace(str1, " ")).Trim();
                            str2 = (str2.Remove(0,str2.IndexOf("\t"))).Trim();
                            MessageBox.Show(str1);

                            line = sr.ReadLine();
                        }
                        if (line.StartsWith("NOK:"))
                        {

                            getTwoNumbers(line);
                            line = sr.ReadLine();

                        }
                        if (line.StartsWith("WHITE:"))
                        {

                            getTwoNumbers(line);
                            line = sr.ReadLine();

                        }
                        if (line.StartsWith("QUARTERLY:"))
                        {

                            getTwoNumbers(line);
                            line = sr.ReadLine();

                        }
                        if (line.StartsWith("WRITTEN"))
                        {

                            string str = line;
                            str = (str.Remove(0, str.IndexOf(":")+1)).Trim();
                        //    str = str.Substring(0, str.IndexOf("")).Trim();
                            MessageBox.Show(str);
                            line = sr.ReadLine();

                        }
                        if (line.StartsWith("NAME VOTES"))
                        {

                            line = sr.ReadLine();
                        }

                        while (line != "")

                        {
                            char[] delimiterChars = { ' ', ':', '\t' };

                            string text = line;
                            string[] words = text.Split(delimiterChars);


                            foreach (string s in words)
                            { 

                                MessageBox.Show(s);
                            }

                            line = sr.ReadLine();


                        }
                        line = sr.ReadLine();
                        line = sr.ReadLine();



                        break;

0 个答案:

没有答案