well I have some problem
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[i]+";");
}
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?
Vimal KandasamyPosted Mar 23, 2009, 6:28 AM
try the following code, it may help you.
StreamWriter sw = new StreamWriter("c:\\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[i] + ";");
}
foreach (DataRow dr in dt.Rows)
{
sw.Write("\r\n" + dr[0] + " " + dr[1] + " " + dr[2]);
}
sw.Close();
StreamReader sr = new StreamReader("c:\\sss.txt");
String a = sr.ReadLine();
String[] col = a.Split(';');
//col String array contains column headers
//write your code here, to create columns
Console.Write(a + "\n");
while ((a=sr.ReadLine()) != null)
{
DataSet dd = new DataSet();
Console.Write(a + "\n");
String[] rows = a.Split(' ');
// rows string array contains rows of the table
//write your code here, to add rows into table
}
sr.Close();