Click here to Skip to main content
15,888,286 members
Home / Discussions / C#
   

C#

 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
Marat Beiner5-Feb-11 23:30
Marat Beiner5-Feb-11 23:30 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
OriginalGriff5-Feb-11 23:32
mveOriginalGriff5-Feb-11 23:32 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
Marat Beiner5-Feb-11 23:37
Marat Beiner5-Feb-11 23:37 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
OriginalGriff5-Feb-11 23:42
mveOriginalGriff5-Feb-11 23:42 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
Marat Beiner5-Feb-11 23:57
Marat Beiner5-Feb-11 23:57 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
OriginalGriff6-Feb-11 0:24
mveOriginalGriff6-Feb-11 0:24 
AnswerRe: Error: "Could not use"file name"; file already in use." Pin
Eddy Vluggen6-Feb-11 1:09
professionalEddy Vluggen6-Feb-11 1:09 
AnswerRe: Error: "Could not use"file name"; file already in use." Pin
Henry Minute6-Feb-11 7:09
Henry Minute6-Feb-11 7:09 
Amongst the things that others have mentioned your main problem is that the file you create is not an Access Database file.

The suggestion that you have an empty database file and copy it then rename it (from OriginalGriff, I think) will work but you can actually create an access database file programmatically, if you want to.

Here's how.

1) Create a new Windows Forms project. (Call it CreateAccessACCDB, or whatever takes your fancy)
2) Add two TextBoxes and one Button. In order to match the code below name the TextBoxes txtDbName and txtDBPath, otherwise modify the code to match what you name them.
3) Add a reference to the project. Select the 'Com' tab, in the References Dialog, and scroll down till you find 'Microsoft ADO Ext. 2.8 for DDL and Security' and add a reference to it. If you don't find it on your system, you will have to download and install it from Microsoft[^].
4) Double-Click the Button and add the code below into the blank handler that you get.
C#
// delete file if it exists
if (File.Exists(this.txtDBPath.Text + this.txtDbName.Text + ".accdb"))
{
    File.Delete(this.txtDBPath.Text + this.txtDbName.Text + ".accdb");
}

Catalog cat = new Catalog();
try
{
    string creationString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
        " Data Source=" + this.txtDBPath.Text + this.txtDbName.Text + ".accdb";
    cat.Create(creationString);

    MessageBox.Show("Database Created Successfully");
}
catch (Exception ex)
{
    // Hopefully
    MessageBox.Show("Oh deary, deary me." + Environment.NewLine + ex.ToString(), "Oooops");
    return;
}

// Make sure that 'cat' gets disposed
cat = null;

// Create an OleDbConnectionString to use from now on.
OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
connBuilder.DataSource = this.txtDBPath.Text + this.txtDbName.Text + ".accdb";
connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
OleDbConnection tablesConn = new OleDbConnection(connBuilder.ConnectionString);
// Create the Tables that you want
this.CreateTables(tablesConn);

Note that the code uses the run-time driver for Access 2010. If you don't have it, download and install it from Microsoft[^], or edit the code to use the version that you have.

All you have to do then is run it, enter a path into the path box (make sure it ends with a '\') and the name you want for the database (without the '.accdb' extension) in the name textbox, then click the button.

An example of a simple CreateTables() method
C#
private void CreateTables(OleDbConnection conn)
{
    string tableSql = @"CREATE TABLE FoodTable(FoodID SHORT NOT NULL , FoodName VARCHAR(30) NOT NULL , FoodType VARCHAR(15) NOT NULL , FoodPrice CURRENCY NOT NULL )";
    OleDbCommand tableCommand = new OleDbCommand(tableSql, conn);
    conn.Open();
    tableCommand.ExecuteNonQuery();
    conn.Close();
}

this is cut down a bit, as in my app I use a table definition file which allows for Primary Keys and Indexes etc., but you can look up the syntax for that on MSDN or elsewhere.

This was tested on a machine running XP, so if you are on Vista or Windows 7 you might get security errors. I cannot help with those. Also this was done for just goofing around so there is very little error checking.

Hope it helps. Smile | :)
Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
I wouldn't let CG touch my Abacus!
When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is.

AnswerRe: Error: "Could not use"file name"; file already in use." Pin
Marat Beiner6-Feb-11 7:22
Marat Beiner6-Feb-11 7:22 
GeneralRe: Error: "Could not use"file name"; file already in use." Pin
Henry Minute6-Feb-11 9:39
Henry Minute6-Feb-11 9:39 
QuestionUnity DI config implementation question Pin
Member 39190495-Feb-11 18:40
Member 39190495-Feb-11 18:40 
AnswerRe: Unity DI config implementation question Pin
Ravi Sant6-Feb-11 23:41
Ravi Sant6-Feb-11 23:41 
GeneralRe: Unity DI config implementation question Pin
Member 391904927-Feb-11 8:02
Member 391904927-Feb-11 8:02 
Questionquestion about delegates c# Pin
Gilbertu5-Feb-11 9:11
Gilbertu5-Feb-11 9:11 
AnswerRe: question about delegates c# PinPopular
OriginalGriff5-Feb-11 9:30
mveOriginalGriff5-Feb-11 9:30 
GeneralRe: question about delegates c# Pin
Gilbertu5-Feb-11 9:59
Gilbertu5-Feb-11 9:59 
GeneralRe: question about delegates c# Pin
OriginalGriff5-Feb-11 20:13
mveOriginalGriff5-Feb-11 20:13 
GeneralRe: question about delegates c# Pin
Richard MacCutchan5-Feb-11 21:26
mveRichard MacCutchan5-Feb-11 21:26 
GeneralRe: question about delegates c# Pin
OriginalGriff5-Feb-11 21:29
mveOriginalGriff5-Feb-11 21:29 
GeneralRe: question about delegates c# Pin
Gilbertu6-Feb-11 4:01
Gilbertu6-Feb-11 4:01 
GeneralRe: question about delegates c# Pin
OriginalGriff6-Feb-11 4:36
mveOriginalGriff6-Feb-11 4:36 
QuestionExtracting table from pdf in C# Pin
puneeth894-Feb-11 22:23
puneeth894-Feb-11 22:23 
AnswerRe: Extracting table from pdf in C# Pin
Henry Minute5-Feb-11 4:31
Henry Minute5-Feb-11 4:31 
AnswerRe: Extracting table from pdf in C# Pin
RaviRanjanKr6-Feb-11 23:37
professionalRaviRanjanKr6-Feb-11 23:37 
QuestionCaputre Unicode Keyboard Hook Pin
Anubhava Dimri4-Feb-11 18:14
Anubhava Dimri4-Feb-11 18:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.