C# 有没有办法将数据从文本文件插入数据集?
我的文本文件如下所示:C# 有没有办法将数据从文本文件插入数据集?,c#,winforms,C#,Winforms,我的文本文件如下所示: 1 \t a 2 \t b 3 \t c 4 \t d string data = System.IO.File.ReadAllText("myfile.txt"); DataRow row = null; DataSet ds = new DataSet(); DataTable tab = new DataTable(); tab.Columns.Add("First"); tab.Columns.Add("Second"); string[] rows =
1 \t a
2 \t b
3 \t c
4 \t d
string data = System.IO.File.ReadAllText("myfile.txt");
DataRow row = null;
DataSet ds = new DataSet();
DataTable tab = new DataTable();
tab.Columns.Add("First");
tab.Columns.Add("Second");
string[] rows = data.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string r in rows)
{
string[] columns = r.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (columns.Length <= tab.Columns.Count)
{
row = tab.NewRow();
for (int i = 0; i < columns.Length; i++)
row[i] = columns[i];
tab.Rows.Add(row);
}
}
ds.Tables.Add(tab);
我有数据集:dataset ZX=newdataset()代码>
有没有办法将文本文件值插入此数据集
提前谢谢当然有
定义一个DataTable
,添加带有所需数据类型的DataColumn
ReadLine
打开文件,按选项卡拆分值,并通过调用NewRow
将每个值作为DataRow
添加到DataTable中
上有一个很好的示例代码,请看一看并按照步骤进行操作当然有
定义一个DataTable
,添加带有所需数据类型的DataColumn
ReadLine
打开文件,按选项卡拆分值,并通过调用NewRow
将每个值作为DataRow
添加到DataTable中
上有一个很好的示例代码,请看一看并按照步骤进行操作是的,动态创建数据选项卡,请参阅操作指南
逐行读取文件并将这些值添加到数据表中,请参阅如何读取文本文件是的,动态创建数据选项卡,请参阅如何读取
逐行读取文件并将这些值添加到数据表中,请参阅如何读取文本文件,您必须手动解析文件。也许是这样:
1 \t a
2 \t b
3 \t c
4 \t d
string data = System.IO.File.ReadAllText("myfile.txt");
DataRow row = null;
DataSet ds = new DataSet();
DataTable tab = new DataTable();
tab.Columns.Add("First");
tab.Columns.Add("Second");
string[] rows = data.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string r in rows)
{
string[] columns = r.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (columns.Length <= tab.Columns.Count)
{
row = tab.NewRow();
for (int i = 0; i < columns.Length; i++)
row[i] = columns[i];
tab.Rows.Add(row);
}
}
ds.Tables.Add(tab);
--Pavel您必须手动解析该文件。也许是这样:
1 \t a
2 \t b
3 \t c
4 \t d
string data = System.IO.File.ReadAllText("myfile.txt");
DataRow row = null;
DataSet ds = new DataSet();
DataTable tab = new DataTable();
tab.Columns.Add("First");
tab.Columns.Add("Second");
string[] rows = data.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string r in rows)
{
string[] columns = r.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (columns.Length <= tab.Columns.Count)
{
row = tab.NewRow();
for (int i = 0; i < columns.Length; i++)
row[i] = columns[i];
tab.Rows.Add(row);
}
}
ds.Tables.Add(tab);
--帕维尔试试这个
private DataTable GetTextToTable(string path)
{
try
{
DataTable dataTable = new DataTable
{
Columns = {
{"MyID", typeof(int)},
"MyData"
},
TableName="MyTable"
};
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(path))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
string[] words = line.Split(new string[] { "\\t" }, StringSplitOptions.RemoveEmptyEntries);
dataTable.Rows.Add(words[0], words[1]);
}
}
return dataTable;
}
catch (Exception e)
{
// Let the user know what went wrong.
throw new Exception(e.Message);
}
}
就像
GetTextToTable(Path.Combine(Server.MapPath("."), "TextFile.txt"));
您也可以查看试试这个
private DataTable GetTextToTable(string path)
{
try
{
DataTable dataTable = new DataTable
{
Columns = {
{"MyID", typeof(int)},
"MyData"
},
TableName="MyTable"
};
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(path))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
string[] words = line.Split(new string[] { "\\t" }, StringSplitOptions.RemoveEmptyEntries);
dataTable.Rows.Add(words[0], words[1]);
}
}
return dataTable;
}
catch (Exception e)
{
// Let the user know what went wrong.
throw new Exception(e.Message);
}
}
就像
GetTextToTable(Path.Combine(Server.MapPath("."), "TextFile.txt"));
您也可以查看我还想在“volpan”代码中添加以下内容:
String _source = System.IO.File.ReadAllText(FilePath, Encoding.GetEncoding(1253));
最好添加文本文件的编码,这样您就可以读取数据,在我的情况下,修改后可以将数据导出到另一个文件。我还想在“volpan”代码中添加以下内容:
String _source = System.IO.File.ReadAllText(FilePath, Encoding.GetEncoding(1253));
添加文本文件的编码很好,这样您就可以读取数据,在我的情况下,修改后将数据导出到另一个文件。谢谢帮助,但是当我不知道文本文件中有多少列时我能做什么?谢谢帮助,但是当我不知道文本文件中有多少列时我能做什么?