所以,这就是我想要做的事情:
我想将文件夹中的所有xml文件(比如C:\Bla\AllMyLittleXmlFiles
)导入到DataSet中,然后将其导入到SQL Server中。那可能吗?似乎DataSet在每次成功读取文件后都会自行清除,只留下第一个文件的数据。
这是我的代码,包括一些不必要的东西:
StringBuilder fileNames = new StringBuilder();
ArrayList filePaths = new ArrayList();
FolderBrowserDialog folder = new FolderBrowserDialog();
folder.ShowDialog();
int pathLength = folder.SelectedPath.Length;
foreach (string file in Directory.EnumerateFiles(folder.SelectedPath))
{
string fielname = file.ToString().Substring(pathLength + 1);
string filepath = file.ToString();
fileNames.AppendLine(fielname);
filePaths.Add(filepath);
}
// textBox1.Text = filePaths[0].ToString();
DataSet aDS = new DataSet();
StringBuilder uh = new StringBuilder();
int filesImported = 0;
foreach (object ob in filePaths)
{
string test = ob.ToString();
uh.Append(test);
aDS.ReadXml(ob.ToString());
filesImported++;
}
int tablesimported = 0;
foreach (DataTable table in aDS.Tables)
{
dataGridView1.DataSource = table.DefaultView;
tablesimported++;
}
MessageBox.Show("Files Imported:" + filesImported.ToString() + " Tables Imported : " + tablesimported.ToString());
textBox1.Text = uh.ToString();
修改 在尝试了一些答案之后我就离开了:
int filesImported = 0;
foreach (object ob in filePaths)
{
dsCollection[filesImported].ReadXml(ob.ToString());
filesImported++;
}
int tablesImported = 0;
foreach (DataSet ds in dsCollection)
{
foreach (DataTable table in ds.Tables)
{
mainDS.Merge(table);
tablesImported++;
}
}
然后我在dsCollection上调用Merge
方法。唯一的问题是dscollection中的数据集永远不会被实例化,所以......回到2号方。
答案 0 :(得分:3)
也许你可以创建主数据集,在读取xml到temp数据集后尝试Merge这些数据集如下:
mainDataSet.Merge(tempDataSet);
答案 1 :(得分:1)
这将解决你的问题,虽然不确定它是否有效......
DataSet[] aDS = new DataSet[filePaths.Count];
StringBuilder uh = new StringBuilder();
int filesImported = 0;
foreach (object ob in filePaths)
{
string test = ob.ToString();
uh.Append(test);
//every xml file gets its own dataset
//so that new read operation will not clear data
aDS[filesImported].ReadXml(ob.ToString());
filesImported++;
}
int tablesimported = 0;
foreach (DataSet ds in aDS)
{
foreach (DataTable table in ds.Tables)
{
dataGridView1.DataSource = table.DefaultView;
tablesimported++;
}
}
答案 2 :(得分:1)
我臃肿而低效的解决方案......我很失望。
感谢Niko和Reniuz指出我正确的方向。
private ArrayList GetFilePaths()
{
ArrayList filePaths = new ArrayList();
FolderBrowserDialog folder = new FolderBrowserDialog();
folder.ShowDialog();
foreach (string filePath in Directory.EnumerateFiles(folder.SelectedPath))
{
filePaths.Add(filePath.ToString());
}
return filePaths;
}
private void ImportXmls(ArrayList filePaths)
{
DataSet[] tempDSCollection = new DataSet[filePaths.Count];
int impFiles = 0;
foreach (object ob in filePaths)
{
DataSet impDS = new DataSet();
impDS.ReadXml(ob.ToString());
tempDSCollection[impFiles] = impDS;
impFiles++;
}
foreach (DataSet aDS in tempDSCollection)
{
foreach (DataTable table in aDS.Tables)
{
mainDS.Merge(table);
}
}
}
我会继续努力并更新,但现在必须要做
答案 3 :(得分:0)
试试这个:
foreach (object ob in filePaths)
{
string test = ob.ToString();
uh.Append(test);
DataSet tmpDS = new DataSet();
tmpDS.ReadXml(ob.ToString());
aDS.Merge(tmpDS);
filesImported++;
}
这将为每个文件留下带有DataSet表的DS,这似乎是您正在寻找的。 p>
编辑:[向@Reniuz道歉,他指出了完全相同的方向,但提前20分钟!]
答案 4 :(得分:-1)
试试这个:
DataSet ds = new Dataset();
for(int x=0;x<Filepath.Count;x++)
{
ds.ReadXml(Filepath[x].ToString());
}