Creating a Database Table? [C#]

Shaitan00

Well-known member
Joined
Aug 11, 2003
Messages
343
Location
Hell
My goal is to create a .XLS file (Excel) without the need for the Excel COM Object (and corresponding office .dlls), so this is how I am trying to create my Table (worksheet), by treating it like any other Database.
My connection string is as follows "provider=Microsoft.Jet.OLEDB.4.0; data source=" + sDB + "; Extended Properties=Excel 8.0;" (where sDB=ClientTaskTimer.xls) and the code I use is posted below:

Code:
System.IO.File.Create("ClientTaskTimer.xls");
oDB = new objDB("ClientTaskTimer.xls");
oDB.Write("CREATE TABLE ClientTaskTimer ([CLIENTS] varchar, [ASSIGNMENTS] varchar)");

oDB is a class that creates the Database connection (pass in the Database name, in this case the excel .xls file) and also reads/writes (Adapter/Command).
Sadly this doesnt seem to work perfectly, specifically if the .XLS file already exists and was created BY Excel then my table (worksheet) is added perfectly and everyone is happy, HOWEVER given the code posted above (which is supposed to CREATE the Excel file AND Table - meaning it makes the Template for the Database if this is the first time you run the application) I keep getting the following ERROR MESSAGE:
System.Data.OleDb.OleDbException: The Microsoft Jet database engine cannot open the file . It is already opened exclusively by another user, or you need permission to view its data.

Is there something wrong with the way I am creating my Database File (Excel .xls file)?
The only reason I am making it a .XLS is because a) I cant use a real DB b) a Text file is to nasty and c) it allows the users to open the excel file and view it seperatly
Any help/hints would be greatly appreciated - or if you can propose another way to go about this problem...? Thanks
 
First of all, close the newly opened file, and the message will disapear.

Secondly, why dont you use XML?
DataSet1.WriteXml() is easy to use.
 
Back
Top