如何读取目录中的多个文件,然后读取到MS SQL数据库 [英] How to read multiple files in a directory then to MS SQL database

查看:73
本文介绍了如何读取目录中的多个文件,然后读取到MS SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我需要这段代码的帮助。来自目录的代码准备好多个文本文件然后将文件的内容添加到ArrayList。不知怎的,我只能读取数组列表中索引为零的第一项。索引1到n出现索引超出范围。必须是非负数且小于集合的大小。

参数名称:index






请参阅下面的代码



Hi Guys

I need help with this piece of code. The code ready multiple text file from a directory then add the contents of a file to an ArrayList. Somehow I can only read the first item at index zero on the array list. indexed 1 to n comes up with "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


.
Please see code below

protected void GetFileValues()
        {
            
            int lineCounter = 0;
            string line = "";
              
            foreach (string files in Directory.GetFiles(Server.MapPath("DataFiles"), "*.whl"))
            {
                StreamReader sr = new StreamReader(files);
                ArrayList list = new ArrayList();
               
                while (((line = sr.ReadLine()) != null) && (lineCounter < 3))
                {
                    string[] token = line.Split('=');

                    if (String.IsNullOrEmpty(token[1]))
                    {
                        list.Add("0");
                    }
                    else
                    {
                        list.Add(token[1]);// add the value to the right of '=' into an arraylist
                    }
                 lineCounter++;
                  
               }//end while
               
                sr.Close();
                ListBox1.Items.Add((string)list[1]);
            }
        }

推荐答案

每次循环迭代时,您都在重置数组列表。

您需要移动该行:

You are resetting the array list each time your loop iterates.
You need to move the line:
ArrayList list = new ArrayList();

在foreach lop之外,所以它不会被重置。



基于评论1的附加回复:



这一行:

outside the foreach lop so it doesn't get reset.

Additional Response based on comment 1:

This line:

ListBox1.Items.Add((string)list[1]);



这只写入读取元素1您的列表,因此在屏幕上(即在您的列表框1中)您将看到相同的条目重复。



现在,我有点不确定它到底是什么你试图从你的代码中实现。



通过将 ArrayList 移到外部for循环之外,它将导致在所有存储的文件中的每个条目中在列表中,从0 - >; X.



用于将其写入数据库的代码在哪里,因为这不在代码示例中?


This is written to only read element 1 from your list, as a result on the screen (i.e. in your listbox1) you will see the same entry repeated.

Now, I'm slightly unsure what it is exactly you are trying to achieve from your code.

By moving ArrayList to outside the outer for loop it will result in every entry in all of the files being stored in the list, from 0 -> X.

Where is the code you are using to write this to a database as this is not in the code example?


这里是插入数据库的代码



SqlConnection conn = new SqlConnection(connStr);

string insertToDB =;

试试

{







conn .Open();



insertToDB =INSERT INTO tblMiniprof([SerialNo],[ProgramName],ProgramVer])+



VALUES @ SerialNo,@ ProgramName,@ ProgramVer;



SqlCommand cmd = new SqlCommand(insertToDB,conn);



cmd.Parameters.AddWithValue(@ SerialNo,(string)list [2] + - +(string)list [3]);

cmd.Parameters.AddWithValue(@ ProgramName,(string)li st [0]);

cmd.Parameters.AddWithValue(@ ProgramVer,(string)list [1]);



cmd.ExecuteNonQuery();



}

终于

{

conn .Close();

}
here is the code that inserts to the database

SqlConnection conn = new SqlConnection(connStr);
string insertToDB = "";
try
{



conn.Open();

insertToDB = "INSERT INTO tblMiniprof ([SerialNo],[ProgramName],ProgramVer])" +

"VALUES @SerialNo,@ProgramName,@ProgramVer)";

SqlCommand cmd = new SqlCommand(insertToDB, conn);

cmd.Parameters.AddWithValue("@SerialNo", (string)list[2] + "-" + (string)list[3]);
cmd.Parameters.AddWithValue("@ProgramName", (string)list[0]);
cmd.Parameters.AddWithValue("@ProgramVer", (string)list[1]);

cmd.ExecuteNonQuery();

}
finally
{
conn.Close();
}


这篇关于如何读取目录中的多个文件,然后读取到MS SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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