0

StreamWriter sw = new StreamWriter("D:\\sss.txt");

DataTable dt = new DataTable();
DataColumn dc = new DataColumn("ID", typeof(string));

dc.AllowDBNull = false;
dc.Unique = true;
dt.Columns.Add(dc);
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
dt.Rows.Add(1, "aaa", "bbb");
dt.Rows.Add(2, "ccc", "dddd");

int length = dt.Columns.Count;
int length1 = dt.Rows.Count;

for (int i = 0; i < length; i++) {
sw.Write(dt.Columns+";");
}
foreach (DataRow dr in dt.Rows) {
sw.Write("\r\n" + dr[0] + " " + dr[1] + " " + dr[2]);
}
sw.Close();

StreamReader sr = new StreamReader("D:\\sss.txt");


a = sr.ReadLine();
while (a != null) {

DataSet d = new DataSet();
Console.Write(a + "\n");
a = sr.ReadLine();


}

sr.Close();


as you can see I write from datatable to txt File ad it warks well, but now I want to load it back to datatable like this: In Colums must be ID, FirsName, LastName and in Rows must be 1, "aaa", "bbb"; 2, "ccc", "dddd"

what'll you advise me?

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by vizy
0

Hi,

Yes while reading the lines from the files
you can save the data into DataTable in this way

dt.Rows.Clear() // this will clear all the previous rows
while (a != null) {
string [] strValues = a.Split(new char[] {' '});
dt.Rows.Add(strValues[0], strValues[1], strValues[2]);
Console.Write(a + "\n");
a = sr.ReadLine();
}

try doing this...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.