Hello, I created a new accdb file by method : File.Create, after that I try to connect that data base like that DataBaseManager.conn.Open();
and it throws me this exception, what should I do?
Thanks.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Jaish MathewsRank #225 · 11K reputationPosted Feb 5, 2011, 9:07 PM EST
You need to close the stream after file creation. Make sure to use "using" block like below.
using (File.Create("<<>>"))
{
}
//From here only open the created file. Make that also in another "using" block.
Marat Beiner20 reputationPosted Feb 5, 2011, 9:17 PM EST
I've tried it, didn't help.
Liutauras Medziunas25 reputationPosted Feb 5, 2011, 9:38 PM EST
System.Data.DbManager DbMgr = new System.Data.DbManager("System.Data.OleDb", connstring);
If you want to operate with File.Create() created file you could do it like in my sample.
Maybe could you write more details about problem for better understanding
public static void Main()
{
string path = @"accdb";
// Delete the file if it exists.
if (File.Exists(path))
{
File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
Marat Beiner20 reputationPosted Feb 5, 2011, 9:50 PM EST
I wont to create for every user his private data base, thus after I've received his user name I create an accdb file for him, connect to this data base and create tables.
The problem:
after I create the file it won't connect to it, it throws an exception at line :DataBaseManager.conn.Open();, says that the file already open.
Jaish MathewsRank #225 · 11K reputationPosted Feb 6, 2011, 12:05 AM EST
I am focussing on "file already in use" problem. On creating a new file, it will be locked by a stream object, which you need to close manually. A "using" block will do it automatically and the file you created is then free to use. Tried that?
Marat Beiner20 reputationPosted Feb 6, 2011, 12:15 AM EST
public DataBaseManager(String userName)
{
this.userName = userName;
if (!File.Exists(Environment.CurrentDirectory + "\\" + userName + ".accdb"))
{
using (fs = File.Create(Environment.CurrentDirectory + "\\" + userName + ".accdb"))
{
}
}
}
public void connectToDatabase()
{
DataBaseManager.conn = new OleDbConnection(this.dbConnPath);
if (DataBaseManager.conn == null)
{
MessageBox.Show("Can't connect to database", "WorkManager", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
String state = DataBaseManager.conn.State.ToString();
using (File.Open(Environment.CurrentDirectory + "\\" + userName + ".accdb", FileMode.Open))
{
if (state.Equals("Closed"))
{
DataBaseManager.conn.Open();
}
}
}
}
Suthish NairRank #92 · 22.8K reputationPosted Feb 6, 2011, 12:30 AM EST
Make sure your file exists amd not opened in memory. Also, where is your FileStream declaration method?
Marat Beiner20 reputationPosted Feb 6, 2011, 12:35 AM EST
The declaration of "fs" is above as private.
Suthish NairRank #92 · 22.8K reputationPosted Feb 6, 2011, 1:12 AM EST
FileStream fs; //still in memory, didnt disposed. change your code to..
Liutauras Medziunas25 reputationPosted Feb 6, 2011, 1:40 AM EST
- Open a new Visual C# .NET console application.
- In Solution Explorer, right-click the
References node and select Add
Reference.
- On the COM tab, select Microsoft
ADO Ext. 2.7 for DDL and Security, click Select to
add it to the Selected Components, and then click
OK.
public static void Main(){
DataBaseManager dataBaseManager = new DataBaseManager("Marat_Beiner");
OleDbConnection connection = dataBaseManager.ConnectToDatabase();
Console.ReadKey();
}
public class DataBaseManager
{
private readonly string userName = "anonymous";
private string dbFilePath = String.Empty;
private string dbCnnectionString = String.Empty;
ADOX.Catalog catalog;
public DataBaseManager(string userName)
{
this.userName = userName;
this.dbFilePath = String.Format("{0}\\{1}.accdb", Environment.CurrentDirectory, this.userName);
this.catalog = new ADOX.Catalog();
this.dbCnnectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};", this.dbFilePath);
if (!File.Exists(this.dbFilePath))
{
this.catalog.Create(this.dbCnnectionString);
Console.WriteLine("Database Created Successfully");
}
else
{
Console.WriteLine("Database Inicialized Successfully");
}
}
public OleDbConnection ConnectToDatabase()
{
OleDbConnection oleDbConnection = new OleDbConnection(this.dbCnnectionString);
Console.WriteLine("Database Connected Successfully");
return oleDbConnection;
}
}
In this sample the DataBaseManager creates Access database with file name equals user name.
Your solution is bad if you create a binary file for using with OleDbConnection class.
You did one mistake in your code:
DataBaseManager.conn = new OleDbConnection(this.dbConnPath);
if (DataBaseManager.conn == null)
{
MessageBox.Show("Can't connect to database", "WorkManager", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
The statement is incorrect and this will never happen.
If this post helped you, then tick the CheckBox above "Do you like this Answer"...
Marat Beiner20 reputationPosted Feb 6, 2011, 8:24 AM EST