且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何读取目录中的多个文件,然后读取到MS SQL数据库

更新时间:2022-01-07 10:23:52

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

您需要移动该行:

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();
}