只需单击一次就可以读取文本文件直到c#中的文本结尾 [英] for a single click it has to read a text file till the end of text in c#

查看:78
本文介绍了只需单击一次就可以读取文本文件直到c#中的文本结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取一个文本文件并将其存储到数据库中。只需单击一下,我就可以从文本文件中读取一行,并将其存储到数据库中。



但是我的文本文件很大,所以我需要一个方法,这样一次点击就会开始读取文件,它可以存储到数据库直到文件结尾。



我的代码:



i'm reading a text file and storing it to a data base.For a single click i'm able to read a single line from text file and it can be stored to a database.

But my text file is huge,so i need a method so that a single click will start reading the file and it can be stored to a database till the end of file.

my code:

Program problem = new Program();
        SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=Billing;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();



        string line = "";
        string billno = null;
        string billdate = null;
        string hosp_no = null;
        string ip_no = null;
        string name = null;
        string test = null;
        string ward = null;
        string rad = null;
        string pathNames = "C:\\Users\\Administrator\\Desktop\\Ex_1\\KHL.txt";

        ArrayList rawData = new ArrayList();

         try
        {

            StreamReader readFile = new StreamReader(pathNames);

            do
            {
                //tim

                if ((line = readFile.ReadLine()) != null)
                {
                    //  Console.WriteLine(line);
                    if (line.Contains("RADIOLOGY"))
                    {
                        rawData.Add(line);
                        //Console.ReadKey();
                    }

                }
                else
                {
                    line = null;
                }
            } while (line != null);


            int rIn = rawData.Count;

            for (int i = 0; i < rIn; i++)
            {
                con.Open();

                string[] data = (rawData[i] + "").Split('~');
                string da = "";
                hosp_no = data[1].Substring(data[1].Length - 8, 8);
                ip_no = data[2].Substring(data[1].Length - 7, 7);
                name = data[8].Replace("'", "''");
                billno = data[3];
                billdate = data[4].Substring(5, 2) + "/" + data[4].Substring(8, 2) + "/" + data[4].Substring(0, 4);
                test = data[5];
                ward = data[16];

                if (data.Length == 1 && data[0] == "") da = "Finish";
                else
                {


                    cmd = new SqlCommand("insert into ex_1 values('" + hosp_no + "','" + ip_no + "','" + name.Replace(" ' ", " '' ") + "','" + billno + "','" + billdate + "','" + ward + "','" + test + "')", con);
                    cmd.Parameters.AddWithValue("@name", name);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    Console.Read();

                }
                //Console.WriteLine(da);
                Console.Read();
            }
        }
        catch (Exception f)
        {
            Console.WriteLine(f.Message);
            Console.Read();
        }





[edit]已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

首先,你为什么要通过连接字符串(非常危险)和部分通过参数化查询来部分执行SQL命令?将它全部转换为参数,除非您喜欢SQL注入攻击,并且您的数据库被销毁...



其次,不要使用ArrayList - 使用通用集合代替 - 它们是类型安全的,并且代码更友好:

First off, why are you doing your SQL command partly by concatenating strings (which is extremely dangerous) and partly via a parameterised query? Convert it all to parameters, unless you like SQL Injection attacks, and your database being destroyed...

Secondly, don't use ArrayList - use the Generic Collections instead - they are type safe, and a lot more code friendly:
List<string> rawData = new List<string>();





然后你有准确地决定你想做什么:你的代码打破了一个排队,把它写到数据库然后等待确认,然后转移到下一个。

如果你想全部做然后:

1)移动 con.Open com.Close 调用循环的最终

2)删除 Console.Read()行。



然后你可以决定是否应该设置为使用不同的线程......



Then you have to decide exactly what you want to do: your code breaks a line up, writes it to the database and then waits for confirmation before moving to the next one.
If you want to "do them all" then:
1) Move the con.Open and com.Close calls outside the final for loop.
2) Remove both the Console.Read() lines.

Then you can decide if this should be set to use a different thread...


这篇关于只需单击一次就可以读取文本文件直到c#中的文本结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆