-
July 14th, 2009, 10:43 AM
#1
Using xls file as database
I have one excellsheet with one workspace (Sheet1) with two columns :
1) FileName
2) FileCode
This file has 120 records (rows).
I want to read from FileName column, append ".bak1" to it and save the result in Code column
e.g:
FileName : TestFile.txt
FileCode : TestFile.cs.bak1
The Code that I have written compiles with zero errors but does not updates the FileCode column values. Please suggest the best way of manipulating records in excel file.
What is missing from my code?
/// <summary>
/// Read from Excel Sheet
/// </summary>
private void UsingDataSet()
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Sales.xls;Extended Properties=Excel 8.0");
con.Open();
OleDbDataAdapter objAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", con);
DataSet objDataSet = new DataSet();
string strFileName;
int intCntr=0;
objAdapter.Fill(objDataSet, "MyTableName");
foreach (DataRow pRow in objDataSet.Tables["MyTableName"].Rows)
{
strFileName = pRow["FileName"].ToString();
pRow["FileCode"] = strFileName + ".bak1";
objDataSet.Tables["MyTableName"].Rows[intCntr++]["FileCode"] = pRow["FileCode", DataRowVersion.Current];
objDataSet.AcceptChanges();
}
con.Close();
}
/// <summary>
/// Read from Excel Sheet
/// </summary>
private void UsingDataSet()
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Sales.xls;Extended Properties=Excel 8.0");
con.Open();
OleDbDataAdapter objAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", con);
DataSet objDataSet = new DataSet();
string strFileName;
int intCntr=0;
objAdapter.Fill(objDataSet, "MyTableName");
foreach (DataRow pRow in objDataSet.Tables["MyTableName"].Rows)
{
strFileName = pRow["FileName"].ToString();
pRow["Code"] = strFileName + ".bak1";
objDataSet.Tables["MyTableName"].Rows[intCntr++]["Code"] = pRow["Code", DataRowVersion.Current];
objDataSet.AcceptChanges();
}
con.Close();
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|