 |
|
 |
Smooth solution! Works like a charm, much cleaner...
=====================
Lars [Large] Werner
lars@werner.no
http://lars.werner.no
Have you tried the ultimate tool for filling your CD/DVDs? http://lars.werner.no/sizeme/
=====================
|
|
|
|
 |
|
 |
the old mdb has a password... but after compacting and repaired the password has gone...
why?
JoeAdrianBlack
|
|
|
|
 |
|
 |
joeblack wrote: the old mdb has a password... but after compacting and repaired the password has gone...
Same for me,
does anyone have a clue how to set an access password programmatically?
THX
Sebastian Lorenz
|
|
|
|
 |
|
 |
Hi,
you won't believe how easy it is...
Just set the OLEDB:Database Password=yourpass in the oParams array, and there you are!
Hope this helps you out, it did for me.
Sebastian Lorenz
|
|
|
|
 |
|
 |
Hi,
thanks for this example
I just have a little question : is it REALLY safe to use this code, I mean, in production environment ? Because it delete the original file... maybe there are some RARE situations where the database could be completely swept ?
|
|
|
|
 |
|
|
 |
|
 |
Hi
I need to repair access file in c++ program, so I should write this function in c++. Because I do not know c# so someone help me with this function written in c++. Tha nks.
|
|
|
|
 |
|
 |
you just type "www.codeproject.com/c++" the web page will show you the C++ Source.
|
|
|
|
 |
|
 |
If the JRO is not registered .... it will not work....
but you can modify the code to automatically register the com-object
Type typJRO=Type.GetTypeFromProgID("JRO.JetEngine");
if (typJRO==null)
{
//phps. msjro is not registered
string strMsjrodll=Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"),@"Common Files\System\ado\msjro.dll");
if (File.Exists(strMsjrodll))
{
//start a process to register the dll
Process procRegisterMsjro=Process.Start("regsvr32.exe",string.Concat("/s \"",strMsjrodll,"\""));
procRegisterMsjro.WaitForExit();
typJRO=Type.GetTypeFromProgID("JRO.JetEngine");
}
}
if (typJRO==null)
{
throw new InvalidOperationException("JRO.JetEngine can not be created... please check if it is installed");
}
//create an inctance of a Jet Replication Object
object objJRO =
Activator.CreateInstance(typJRO);
|
|
|
|
 |
|
 |
Very helpful article, but when I run my program I get an exception. The error message says "Format of the initialization string does not conform to the OLE DB specification". I have no idea why it is not working. I have tried messing around with the connection string but nothing seems to help. Most of my code is exactly how you have it:
object[] oParams;
object objJRO = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
string conn="C:\\";
oParams = new object[] {conn, "Provider=Microsoft.Jet.OLEDB.4.0;Data" +
"Source=C:\\tempdb.mdb;Jet OLEDB:Engine Type=5"};
\\ This is where it is crashing on me
objJRO.GetType().InvokeMember("CompactDatabase",
System.Reflection.BindingFlags.InvokeMethod,null,objJRO,oParams);
Thank you so much,
Ben
|
|
|
|
 |
|
 |
You killed a space in the string
use "Data Source=C:\\..." instead of "DataSource=C:\\..."
|
|
|
|
 |
|
 |
Your code was great. I made what I think are some enhancements to the cod. Thanks!!!
Public Sub CompactAccessDB(ByVal mdbToCompact As String) 'Boilerplate for connection strings Const JET_CONNECTION_STRING As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data" + " Source={0};Jet OLEDB:Engine Type=5" Dim objJRO As Object Dim tempFile As String Dim oParams As Object() Try 'Get temp filename for compacted MDB 'HACK: To prevent "Database Already Exists" error we add "FOO" to the end of system generated filename. The CompactDB fuction will then create a new compcated DB with the system generateed name with "FOO" appended. Function Path.GetTempFileName() gets filename AND creates file which causes a problem here tempFile = Path.GetTempFileName() & "FOO" 'Setup object arrary to hold connection strings for CompactDatabase meathod on JRO.JetEngine object oParams = New Object() {String.Format(JET_CONNECTION_STRING, mdbToCompact), String.Format(JET_CONNECTION_STRING, tempFile)} 'Get instanct of JRO.JetEngine object which will do the actual work of compacted the MDB objJRO = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine")) 'Compact the database... objJRO.GetType.InvokeMember("CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, Nothing, objJRO, oParams) 'Delete the old database System.IO.File.Delete(mdbToCompact) 'Replace it with the new compacted mdb System.IO.File.Move(tempFile, mdbToCompact)
Catch ex As Exception 'Just send the exception up to the caller Throw ex Finally 'Release memory System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO) objJRO = Nothing End Try
End Sub "I don't know were I'm goin' but I'm on the way" - Me
|
|
|
|
 |
|
 |
I'm new to VB, Thanks for the VB syntax conversion
|
|
|
|
 |
 | Thanks  |  | Anonymous | 8:37 31 Jan '05 |
|
 |
Excellent code, easy to learn!!!!
|
|
|
|
 |
|
 |
Hi, I'm new to c# but can't see how exactly to call the CompactAccessDB method and pass in the parameters. How do I run this code for a database located at c:\compact.mdb
Thanks
|
|
|
|
 |
|
 |
if the initial dataase has a password in the connection string, the replacement database doesnt have the database... do you know how to set a password programaticly?
|
|
|
|
 |
|
 |
Hmmm, can’t actually believe I wrote that!!! Must have been real late… what I mean to illustrate is that the method you uses would actually remove the password from the database.. do you know how to set it programmatically?
|
|
|
|
 |
|
 |
I get the expetion when i use try - catch. It is :[System.Runtime.InteropServices.COMExeption(0x80004005). Invalid Argument]
I use access 2000 database and my connString is:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\database\\mydb.mdb;Jet OLEDB:Database Password=mypass;";
I tried a lot of things... like use the Database password=mypass to destination db... and still get tge same message.
Milan Ristic
|
|
|
|
 |
|
 |
Works great, saved me some late working hours!!
Magnus Hauge
|
|
|
|
 |
|
 |
Great article, but I keep getting an exception on the "InvokeMember" call. The error message simply says "Exception has been thrown by the target of an invocation". Could it have something to do with the connection string? My Access MDB has no security, so all I am putting in the connection string "FIL=MS Access;DSN=AcclaroDB" which our C# programs use to open the MDB file. It is in Access 2000 format.
Any ideas? Any help would be appreciated.
Michael Greene
|
|
|
|
 |
|
 |
Hi Michael.
Catch your exception and look at the InnerException. You will get a COM-exception, which will explain, what is wrong.
Like this:
try
{
}
catch(Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}
|
|
|
|
 |
|
 |
Hello,
I get a program exception as well. The inner exception I am getting is "Unrecognized database format" This is the exception that I use to know that the database needs a repair.
The database is in this state due to a loss of connection when writing previously. I want my application to recognize that the database is in this state and perform a repair.
Any idea why the compact will not work?
Regards,
Mike Gardiner
|
|
|
|
 |
|
 |
After applying try...catch as you guided, I got this message:
"System.Runtime.InteropServices.COMException (0x80040E4D): Authentication failed"
How can I fix this error?
Pham Toan Thang
|
|
|
|
 |
|
 |
Shooted. Exactly what I was looking for. You've got my 5.
|
|
|
|
 |
 | Great  |  | Dubant | 4:47 29 Jul '04 |
|
 |
No I will create a scheduled task that will call a webservice that will compact & repair, en then email me my DBase.
( My Host was defaced once )
Any Idea on how to kill all my connections?
|
|
|
|
 |