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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Marat BeinerPosted Feb 6, 2011, 1:24 PM
Liutauras MedziunasPosted Feb 6, 2011, 6:40 AM
- 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"...
Suthish NairPosted Feb 6, 2011, 6:12 AM
FileStream fs; //still in memory, didnt disposed. change your code to..
using (FileStream fs = File.Create(Environment.CurrentDirectory + "\\" + userName + ".accdb"))
Marat BeinerPosted Feb 6, 2011, 5:35 AM
The declaration of "fs" is above as private.
Suthish NairPosted Feb 6, 2011, 5:30 AM
Make sure your file exists amd not opened in memory. Also, where is your FileStream declaration method?
using (FileStream fs = File.Create(Environment.CurrentDirectory + "\\" + userName + ".accdb"))
{
}
Marat BeinerPosted Feb 6, 2011, 5:15 AM
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();
}
}
}
}
Jaish MathewsPosted Feb 6, 2011, 5:05 AM
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 BeinerPosted Feb 6, 2011, 2:50 AM
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.
Liutauras MedziunasPosted Feb 6, 2011, 2:38 AM
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 BeinerPosted Feb 6, 2011, 2:17 AM
I've tried it, didn't help.
Jaish MathewsPosted Feb 6, 2011, 2:07 AM
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.