Cannot encrypt / decrypt SQLite database in .NET4 - c#

I am running Visual Studio 2010 on a Windows 7 x64. The app I'm writing is supposed to run on all platforms (AnyCPU). I'm able to encrypt/decrypt the database file if I'm using the System.Data.SQLite.dll either x86 version (sqlite-netFx40-static-binary-bundle-Win32-2010-1.0.77.0.zip) or x64 one (sqlite-netFx40-static-binary-bundle-x64-2010-1.0.77.0.zip).
I need my app to run on both platforms x86 and x64 (Any CPU project build setting). I've tried installing the ADO.NET 4.0 Provider (SQLite-1.0.67.1-vs2010-net4-setup.exe). I added the reference (right-click on project, Add Reference, .NET tab -> System.Data.SQLite) and ran the program. If I decrypt the file and try to encrypt by calling ChangePassword("myPass"), I get the following exception:
System.EntryPointNotFoundException was caught
Message=Unable to find an entry point named 'sqlite3_rekey' in DLL 'System.Data.SQLite.DLL'.
Source=System.Data.SQLite TypeName=""
StackTrace:
at System.Data.SQLite.UnsafeNativeMethods.sqlite3_rekey(IntPtr db, Byte[ key, Int32 keylen)
at System.Data.SQLite.SQLite3.ChangePassword(Byte[ newPasswordBytes)
at System.Data.SQLite.SQLiteConnection.ChangePassword(Byte[ newPassword)
at System.Data.SQLite.SQLiteConnection.ChangePassword(String newPassword)
at SQLiteTest.Database.Encrypt() in C:\SQLiteTest\Database.cs:line 166
Also, I've tried to open a connection using the SQLiteConnection object and I get two different exceptions in two different cases.
First, if the file is encrypted and I don't specify a password in the connection string I get this:
System.Data.SQLite.SQLiteException was caught
Message=File opened
that is not a database file file is encrypted or is not a database
Source=System.Data.SQLite ErrorCode=-2147467259
StackTrace:
at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous,
UInt32 timeoutMS, String& strRemain)
at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior
behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
at System.Data.SQLite.SQLiteConnection.Open()
at SQLiteTest.Database.GetAllLanguages() in C:\SQLiteTest\Database.cs:line 216
Second, if I add the password parameter to the connection string, I get the System.EntryPointNotFoundException like the one above.
So, does anyone know a sure-fire way to work with encrypted SQLite database in a C# app on AnyCPU platform?
Thanks in advance!

Following Solution is a little bit dirty, but it may work, we didn't use encrypted database so i'm not 100% if that will work for you.
We downloaded both versions of sqlite dlls for x64 and x86 and placed them in different folders. We load them manually dependent on what platform application is currently running ( we check IntPtr.Size , from .net 4 there is a property in Environment class called Is64BitOperatingSystem) when we loaded assembly manually we get connection instance with
var sqliteConnectionType = assembly.GetType("System.Data.SQLite.SQLiteConnection");
(DbConnection)Activator.CreateInstance(sqliteConnectionType);
we don't use reference to any version of sqlite in our project.

Related

EnsureLibraryPresent error in huysentruitw's SapNwRfc connector with .net5

I'd like to use huysentruitw's SapNwRfc connector to connect to SAP. I have copied all the necessary files to project root (sapnwrfc.dll, icuuc50.dll, icuin50.dll, icudt50.dll) but I still got error when I try to call this:
SapLibrary.EnsureLibraryPresent();
the error I got:
System.BadImageFormatException
HResult=0x8007000B
Message=An attempt was made to load a program with an incorrect format. (0x8007000B)
Source=SapNwRfcDotNet
StackTrace:
at SapNwRfc.Internal.Interop.RfcInterop.RfcGetVersion(UInt32& majorVersion, UInt32& minorVersion, UInt32& patchLevel)
at SapNwRfc.Internal.Interop.RfcInterop.GetVersion(UInt32& majorVersion, UInt32& minorVersion, UInt32& patchLevel)
at SapNwRfc.SapLibrary.GetVersion()
at SapNwRfc.SapLibrary.EnsureLibraryPresent()
I have tried to changed platform from any CPU to x86 (then I got error from IIS Express), and x64 (the same behavior as any CPU)
Do you have any tips how to handle it?
Thank you.
EDIT
Tested with .NET Framework 4.7.2 and works fine. With Core 2.1, 3.1 and .NET5 does not work.
I have also tried it without copying the files into project folder and result is the same (files are present in System32 and SysWOW64 folders)

.Net code first migration reflection issues

When running Update-Database in the package manager console of my solution, I receive the following error:
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindType[TBase](String typeName, Func`2 filter, Func`2 noType, Func`3 multipleTypes, Func`3 noTypeWithName, Func`3 multipleTypesWithName)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
I read in other similar stack overflow discussions that sometimes more information can be gained by running the update programatically and catching that exception, so I have tried this:
static void Main(string[] args)
{
//https://stackoverflow.com/questions/15774247/how-to-debug-entity-framework-migration-loaderexceptions
DbMigrator migrator = new DbMigrator(new Configuration());
migrator.Update();
}
The above code actually runs the update successfully without issues (as do the migrations when run by the web browser on application initialization) but the package manager still fails on the same Update-Database command with the same error. I've checked startup projects, default projects, reference copy-local settings, regenerating the ibmmx file, database restores, and run multiple visual studio/OS restarts. I've also tried accessing the exception information through powershell but haven't found anything. Does anyone know of a way to get more information about why the package manager operations are failing?
This same setup with the same underlying database states (different database servers, both on localhost) works without issues on another development machine, so it appears to be something specific to my setup or installation. Are there any other "gotchas" I should be aware of?
Thanks.
Update: I might have spoken too soon about the migrations working outside of the package manager. I just made one on the working dev machine, pulled it to my local machine, and found it doesn't work through the application initialization in the web browser or the package manager.

ODAC 12c Release 2 (12.1.0.1.2) Xcopy compatibility with Oracle 11g R2 database

I'm trying to upgrade my C# projects to use to use the latest ODAC 12c release for which I downloaded the ODAC 12.1 xcopy for Windows. The installation was fine but the problem started when I tried executing a unit test.
My test case fails on connection.Open(). The sample is below:
OracleConnection con = new OracleConnection();
con.ConnectionString = #"User Id = test, Password = test; Data Source = test";
con.Open();
Unfortunately, there is no exception message and the Stack Trace isn't of much help either:
Result StackTrace:
at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure, Boolean bCheck, Int32 isRecoverable)
at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, Object src)
at Oracle.DataAccess.Client.OracleConnection.Open()
at MyProgram.Program.GetDetails()
When I tried debugging, an error message box popped up at the start of execution stating: The entry of procedure ons_init_wconfig_ctx point is not found in the dynamic link library oraons.dll.
I wonder if there's something missing.
Oracle DB version: 11.2.0.3.0
Oracle Client version: 11.2.0.3.0
ODAC version: 12.1.0.1.2
I had the same problem of "The entry of procedure ons_init_wconfig_ctx point is not found in the dynamic link library oraons.dll" with our desktop application.
After spending many days finally found the solution to make working. Steps are given below:
Remove unwanted oracle homes from PATH variable, for example i had installed ODAC 12 both 32 bit and 64 bits.
The oraons.dll was not present in my C:\app\client\mdas0004\product\12.1.0\client_1\bin so i copied the dll from C:\app\client\mdas0004\product\12.1.0\client_1 to the bin folder.
Bingo started working.
If this is with the unmanaged client, try setting a dllpath:
<configuration>
<oracle.dataaccess.client>
<add key="DllPath" value="C:\<your_xcopy_dir>\bin"/>
</oracle.dataaccess.client>
</configuration>
You might be picking up the unmanaged dlls from a previous oracle home. Normally I'd expect a "failed to load assembly" message but maybe things have changed with 12. If that does fix it, and you're tired of dealing with "oracle homes" then consider the new Oracle.ManagedDataAccess.dll instead.
In anycase, I would not expect this to be a client/server compatibility issue.

Could not load file or assembly 'Oracle.DataAccess' (x64)

I am trying to create a WCF service to host in IIS.
My production and development servers are 64 bit.
The WCF service needs to be able to connect to a several different Oracle databases based on the client request.
I am using Entity Framework 5 with ODAC, and with the DbContext Initialiser, I have the option of providing a database connection to use.
private void ExampleSelect(string dataSource, string user, string password)
{
var connection =
string.Format("DATA SOURCE={0};PASSWORD={1};USER ID={2}",
dataSource, password, user);
using (var context = new OracleDbContext(new OracleConnection(connection)))
{
// do stuff here
}
}
The problem I have lies with new OracleConnection(connection). To use this line, I need to reference the x64 version of Oracle.DataAccess.dll
When I come to publish, I get the warning:
and the error:
The platform target for my project is currently 'Any CPU'. If I change this to target x64, I get the same error, but this time it refers to my project and not Oracle.DataAccess
Any of the above builds successfully on my development machine, I only get the errors when I come to publish.
What am I doing wrong here?
These solutions appear to be related, but they all refer to 32 bit Oracle Dlls:
Could not load file or assembly Oracle.DataAccess
Could not load file or assembly 'Oracle.DataAccess' 64 bit ODP.NET
Could not load file or assembly 'Oracle.DataAccess error
To rule it out, I have also tried installing both the 64bit and 32bit Oracle Clients on the development server, but I still don't get as far as even publishing so I'm not sure that the server is the cause of the problem.
The Enable 32 bit Application property on the Application pool in IIS does not make any difference, but I tried it anyway.
To eliminate your problem you must meet the following conditions. Since you want everything run in x64:
1.Make sure that you have installed 64-bit Oracle client software
2.Make sure that you using 64-bit Oracle.DataAccess.dll (use corflags to verify)
3.Make sure you build your application with x64 configuration
4.Disable Enable 32 bit Application on IIS pool
This is a common mistake by many people. They install 32-bit client and try to run x64 Oracle.DataAccess

Oracle oci.dll missing when running on MSTest agent

I'm trying to get some database related integration tests running on a MSTest test agent. We use c# and an oracle db with devart dotconnect.
Since we need to use indirect mode, I installed devart and an oracle client on the test agent.
The tests run smoothly when I execute them on my local machine. But when executed on the test agent (remotely), it can't find the client's oci.dll, which is a dependency somewhere:
System.IO.FileNotFoundException: Can not load Oracle client library oci.dll from home ..
Now, I have no idea why that happens. I checked the PATH environment variables already, but they seem fine. Any ideas why this works on my machine, but not when deployed to the test agent?
edit: added stacktrace within devart:
Devart.Data.Oracle.OracleHome.get_ClientVersion()
Devart.Data.Oracle.aw.a(Boolean A_0, Boolean A_1, OracleHome A_2, Boolean A_3)
Devart.Data.Oracle.OracleInternalConnection..ctor(aa connectionOptions, OracleInternalConnection proxyConnection)
Devart.Data.Oracle.cs.a(k A_0, Object A_1, DbConnectionBase A_2)
Devart.Common.DbConnectionFactory.a(DbConnectionPool A_0, k A_1, DbConnectionBase A_2)
Devart.Common.DbConnectionPool.a(DbConnectionBase A_0)
Devart.Common.DbConnectionPool.GetObject(DbConnectionBase owningConnection)
Devart.Common.DbConnectionFactory.a(DbConnectionBase A_0)
Devart.Common.DbConnectionClosed.Open(DbConnectionBase outerConnection)
Devart.Common.DbConnectionBase.Open()
Devart.Data.Oracle.OracleConnection.Open()
Thanks for the replies, got the right pointers. Apparently the problem occurs when a 64bit thread tries to load a 32bit OCI.DLL and vice versa. However, I played around with the test settings and build settings related to 32/64 things, but to no avail.
Anyway, I was able to solve the problem by additionally installing a 64-bit Oracle Client on the test agent. For some reason it seems that then the QTAgent thread finds the right OCI.DLL.
Faced the same problem in a console project. I fixed it by changing the compilation platform setting by going to
project properties -> Compilation -> Destination of the platform and selecting x86.

Categories