Unrecognized Database Format While trying to open access 97 database - c#

I created a mdb (Access Database 97) file with that code ,
string DBPath = #"C:\\Users\\Desktop\\test.mdb";
// create DB via ADOX if not exists
if (!File.Exists(DBPath))
{
ADOX.Catalog cat = new ADOX.Catalog();
cat.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DBPath);
cat = null;
}
// connect to DB
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DBPath);
con.Open();
When I am trying to open created database it gives an "unrecognized database format" error.
I am using Microsoft Access 97 , I guess it is 32 bit.
I tried all platform targets(x86,x64 & Any CPU) from C# but still same problem.
The both access 97 and Microsoft Office 2010 installed in my computer.
The error is like that;
Could you please help me ?

After conducting deep research in hours, I found 2 main issues regarding database connection:
Using backslash escape sequences in a string literal doesn't converting escape sequence character, instead you should use one backslash for file path:
string DBPath = #"C:\Users\Desktop\test.mdb";
The connection string provider to connect with Access database in question uses Microsoft ACE OLE DB 12.0, which only supported by Access 2007 and later with ACCDB format (use Microsoft Jet 4.0 provider instead).
Additionally, in database creation part which includes ADOX.Catalog.Create() method, it should include Jet OLEDB:Engine Type parameter to specify which Access version being used. By default it sets to Jet OLEDB:Engine Type=5 which means Access 2000 file format (will trigger unrecognized database format error in Access 97), hence to force ADOX creating MDB with Access 97 format it is necessary to set Engine Type=4 like below:
// database creation
if (!File.Exists(DBPath))
{
ADOX.Catalog cat = new ADOX.Catalog();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath + ";Jet OLEDB:Engine Type=4");
// other stuff
}
// connect to database using Jet OLE DB provider
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath);
Additional reference:
Unrecognized MDB created by ADOX

Related

Accessing legacy Visual FoxPro database from C#

I have a legacy Visual FoxPro database from which I need to get the data from.
I have DBC, DCT, DCX & FPT files in the database folder.
I installed the Visual FoxPro provider & driver and wrote a short C# program to access the data.
I also made sure the program is running in 32bit mode, since I understood that the VFP driver is 32bit.
However I'm getting the following error:
Additional information: Connectivity error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I made sure that the driver name ("Microsoft Visual FoxPro Driver") as appears in the Windows ODBC 32bit administration tool is specified in the connection string, but it doesn't seem to help.
The code:
OleDbConnection yourConnectionHandler = new OleDbConnection(
#"Provider=VFPOLEDB.1;DRIVER=Microsoft Visual FoxPro Driver;Data Source=mydatabase.dbc;Mode=Read;Collating Sequence=machine");
// Open the connection, and if open successfully, you can try to query it
yourConnectionHandler.Open();
if (yourConnectionHandler.State == ConnectionState.Open)
{
string mySQL = "select * from tablename";
var command = yourConnectionHandler.CreateCommand();
command.CommandText = mySQL;
var res = command.ExecuteReader(); // throws the error
yourConnectionHandler.Close();
}
I've been stuck on this for a long time, and have tried everything I could find on the net.
Any idea?
You are saying provider and trying to use OleDbConnection (which is right) but defining an ODBC driver within the connection string. In other words, your connection string is wrong.
using (OleDbConnection cn = new OleDbConnection(
#"Provider=VFPOLEDB;Data Source=c:\MyPath\mydatabase.dbc;Mode=Read;Collating Sequence=machine"))
using (OleDbCommand cmd = new OleDbCommand("select * from tablename", cn))
{
cn.Open();
var reader = cmd.ExecuteReader();
// do something with the reader. ie:
// someDataTable.Load(reader);
cn.Close();
}
It works wonderfully well. In connectionstring you don't need to use the database name, you can just use the tables' path as the data source.

Cannot set Connection String to LocalDb using OLEDB

Good morning guys,
I feel quite silly to ask this question, but I have looked everywhere and possibly at all questions in this matter and could not find a solution that would work for me.
Long story short.
I am using a local database called TestDB.mdf in windows form application. The application is designed to do (as per current) two simple things.
1. Import data from excel document into the database - which I don't have any issues with.
and...
2. Clear all data stored in that database - this is where I am struggling
For the import data into the database (point 1) I am using two connection strings. One for excelConnectionString
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " +
"C:\\Users\\User.AR\\Desktop\\export.xls; " +
"Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;\"";
and second for sqlConnectionString
string sqlConnectionString = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\arkadiusz.rzepka\source\repos\Database_application\Database_application\TestDB.mdf;Integrated Security=True";
Then I use SqlBulkCopy to import all data and all is working like a charm.
Now the issue I can see is that I cannot open a connection to clear all data from the same database.
I have navigated to properties of my database to find connection string and this has been presented in the below format:
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User.AR\source\repos\Database_application\Database_application\TestDB.mdf;Integrated Security=True
I have had to amend the above connection string as I was getting errors such as missing provider, should be like Provider=SQLOLEDB, after adding a provider, I have had to change Integrated Security = SSPI, and now I am getting error such as SQL Server does not exist or access denied
My code just to check if the connection was opened is presented below and I would be grateful if you could advise of what I am doing wrong.
private void DeleteAllRecords()
{
string connectionString = #"Provider=Sqloledb;Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User.AR\source\repos\Database_application\Database_application\TestDB.mdf;Integrated Security=SSPI";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
try
{
connection.Open();
MessageBox.Show("Connection openned successfully!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
If you run this in the command prompt.
SqlLocalDB.exe i "MSSQLLocalDB"
You will get the instance pipe name. You should be able to use that in your OLEDB connection. This has worked for me when connecting Excel using OLEDB connection to my MSSQLLocalDB.
Name: MSSQLLocalDB
Version: 13.1.4001.0
Shared name:
Owner: Foo\Foo.Bar
Auto-create: Yes
State: Running
Last start time: 18/03/2021 09:00:34
Instance pipe name: np:\\.\pipe\LOCALDB#DA1FAFF6\tsql\query
So my final connection string in excel looked like this.
provider=SQLOLEDB;initial catalog=IpsosDC;data source=np:\\.\pipe\LOCALDB#DA1FAFF6\tsql\query
Try using double bar on the paths of your connection strings, instead of one. So it would be like this:
string connectionString = #"Provider=Sqloledb;Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\User.AR\\source\\repos\\Database_application\\Database_application\\TestDB.mdf;Integrated Security=SSPI";

connecting Access DB to VS using C#

This is the 1st time I am using C# connection with db, also I dont use access.
I just want to know the initial steps required to connect the access db to visual studio C# windows application.
I have searched though the internet, It helped a lot. I cant find my mistake in the code. I think I am missing some steps in establishing the connection with the db.
here is the error that appears when I try to run the program "'\f38910\Users\kainat.baig\Desktop\AAA\Database101' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides."
CODE:
OleDbConnection bookConn;
OleDbCommand oleDbCmd = new OleDbCommand();
String connParam = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= \\f38910\Users\kainat.baig\Desktop\AAA\Database101; Persist Security Info=False";
public Form1()
{
bookConn = new OleDbConnection(connParam);
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bookConn.Open(); //*ERROR LINE
oleDbCmd.Connection = bookConn;
bookConn.Close();
}
First your file name does not have the file extension name like .mdb
Database101\MyDB.mdb
Second, if it is in a network. Would it be better if you net use first in you DOS prompt to assign a drive letter for your network, like:
net use z: \\f38910\Users\kainat.baig\Desktop\AAA\Database101 your_password /USER:your_account
Then your connection string will be shorter then:
String connParam = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= z:\MyDB.mdb; Persist Security Info=False";
If the file extension by the way is .accdb instead of .mdb it should be:
String connParam = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=z:\MyDB.mdb;;Persist Security Info=False;"
Or simply your path is simply wrong.
Go to windows explorer and just clink on the path where your file is and copy paste it.
You forget to add .mdb after database name or .accdb if you are using Microsoft Office Access 2007 or higher database but, for that you have to change Provider also.
String connParam = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= \\f38910\Users\kainat.baig\Desktop\AAA\Database101.mdb; Persist Security Info=False";
Or you can use connection string like this
String connParam = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\f38910\Users\kainat.baig\Desktop\AAA\Database101.mdb;User Id=admin;Password=;"
EDITED:
for ACCESS 2010
#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\f38910\Users\kainat.baig\Desktop\AAA\Database101.accdb"

C# enctypted .accdb connection

I recently updated my database from .mdb (MS Access 2003) to .accdb (MS Access 2010).
With this update I also updated my provider from: Microsoft.Jet.OLEDB.4.0 to Microsoft.ACE.OLEDB.12.0
The connection works ok when I use a .accdb file without a password, but once I choose to
Encrypt with Password
I receive the following error when I try to open a connection.
Cannot open database ''. It may not be a database that your
application recognizes, or the file may be corrupt.
Used connection string:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\App\\Main\\bin\\Debug\\db.xxx;
Jet OLEDB:Database Password=MyPass;
Note: I use a custom extension for my .accdb file, this was used for .mdb files without problems and I assume this should not be a problem (tested).
I don't see a problem with your connection string. Still I would try it from VBA to see whether that effort sheds any light on the problem.
This one worked from Access 2007 whether I named the db file with "accdb" or "xxx" file extension. The single quotes are not required around my password; the code succeeded whether or not I included the single quotes.
Public Sub OleDbToEncryptedAccdb()
'Const cstrDb As String = "encryptd.accdb" '
Const cstrDb As String = "encryptd.xxx"
Const cstrFolder As String = "C:\share\Access"
Const cstrPassWord As String = "letmein"
Dim cn As Object
Dim strConnect As String
strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
cstrFolder & Chr(92) & cstrDb & _
";Jet OLEDB:Database Password='" & cstrPassWord & "';"
Debug.Print strConnect
Set cn = CreateObject("ADODB.Connection")
cn.ConnectionString = strConnect
cn.Open
cn.Close
Set cn = Nothing
End Sub
Edit: Apparently Access 2010 provides a stronger encryption method than earlier Access versions. With db.xxx open in Access 2010, check which ACE version is used as the Provider.
? CurrentProject.Connection.Provider
If it replies something like Microsoft.ACE.OLEDB.14.0, use that in your c# connection string.

How do I insert data into a dbf format database using sql in Visual C# 2008?

How do I insert data into a .dbf format database using SQL in Visual c#?
Is it same as when using MS Access?
OleDbConnection dbConn = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\wz\Desktop\UBS\onetimecapture\onetimecapture\onetimecapture\bin\Debug\;Extended Properties=dBase IV;");
try
{
// Open connection.
dbConn.Open();
//string queryCutting = "INSERT INTO cuttingstatus.dbf ([Status]) VALUES(adddate)";
string queryCutting = "INSERT INTO cuttingstatus.dbf VALUES(adddate)";
OleDbCommand command_cutting = new OleDbCommand(queryCutting, dbConn);
command_cutting.Parameters.AddWithValue("adddate", "123");
command_cutting.ExecuteNonQuery();
dbConn.Close();
}
catch
{
MessageBox.Show("Error", "SCADA system", MessageBoxButtons.OK);
}
but it return an error say that
The Microsoft Jet database engine could not find the object
'cuttings'.
Make sure the object exists and that you spell its name and the path name correctly.
The database is called cuttingstatus.dbf, and it consist of only a single column Status.
Thanks for the help =)
What is the name of the table in the cuttingstatus.dbf?
The statement should be something like:
INSERT INTO TABLE_NAME
VALUES(adddate)
as the value of the variable queryCutting.
When making an OleDbConnection to use Database files, you want your connection source to point to the logical PATH WHERE the table is... not the actual table.
Connection dbConn = new OleDbConnection(#"Data ource=C:\SomePath\WhereAreAllTables;Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=dBase IV;");
Then, your insert query does not reference the .dbf extension... it is implied by the connection. Also, even though it may be a single column, it would be better to explicitly show the column and values such as :
string queryCutting = "INSERT INTO cuttingstatus ( YourColumnName ) VALUES ( #adddate )";
Next... Is it really a DBase IV system? FoxPro table? Clipper? I would ensure proper provider. If the data is actually from Foxpro origin, I would go to Microsoft and download the Visual Foxpro OleDb provider.
One final thought... is that the error is referring to a truncated table name of up to 8 characters "cuttings" which implies old DOS 8.3 file naming convensions. Don't know if that is what you are also running into for your problem.

Categories