I am trying to create a database with SQLite with c# then create a table insert data then close the connection. I have just downloaded System.Data.SQLite.dll lybrary and I am not sure how to use it. There are a lot of examples in the internet but all of them seem to have a database already. Or maybe I am doing something wrong.
It will be nice If I can have a short example to just create a database, table and basic query.
EDIT
I have tried the examples provided by the comments but I don't understand why I do get errors. Maybe I downloaded the wrong library?
The error was because I was using .NET Framework 4.0 . I downgraded to 2.0 and it worked. Sorry for the question. It will be nice to use it with .NET Framework 4.0 though.
Edit:
It actually works with .NET Framework 4.0 I had to add this lines of code to my app.config file:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
also if you plan to use ado.net in your solution I experience a lot of problems when deploying. Everything worked great under development. If you use ado.net and you plan on deploying your app then include also:
<!--Sqlite configuration so that it works with ado.net-->
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
if you include that last part in your app.config file then you will have to make sure that:
those dll's have to be in your output directory.
if you deploy make sure that you copy those files to the working directory
shouldnt it be {"FailIfMissing", "False"} instead of {"FailIfMissing=False", "False"} ?
Please change the Platform Target for your C# project to Project Settings > Build > Platform Target: Any CPU.
I had a similar error occurred when i tried to run in a x64 machine with SQLite x64 binaries. But worked fine after these settings were changed
Related
I thought that sqlite was simple but it is giving me a hard time. I just want to create an application where I can connect to a sqlite database using the ado.net entity data classes.
I am having this problem when testing the application on a virtual computer running windows xp. the application works fine on my current computer and also on my laptop when I deploy them.
Here is what happens on the virtual computer :
The application is able to launch.
The application is able to interact with the database using System.Data.SQLite
The application is not able to connect to the database using The ADO.NET Entity data models
when I try to connect I get the following exception:
I know there are a lot of post that talk about this and most of them say that you need to download the .NET provider for Sqlite.
I have already installed the sqlite-netFx40-setup-bundle-x86-2010-1.0.79.0.exe and I get the same problem. What should I do?
Edit
I managed to establish a connection by adding:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
to my app.config file.
The problem is that now I cannot select data nor insert records to the database. The exception that I get when I try to insert a new record now is:
A null was returned after calling the 'GetService' method on a store provider instance of type 'System.Data.SQLite.SQLiteFactory'. The store provider might not be functioning correctly.
had to add:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
to my app config file. and it now looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
</configuration>
In the location where sqlite was installed I had to copy
to my output directory where my program exe is located
Install SQLite Toolbox through extensions
Download the latest setup file sqlite-netFx46-setup-bundle-x86-2015-1.0.xxx.0.exe
Install the correct x86 installation, installing VS designer components is a option in the MSI, there is bold text detailing this underneath the correct installation.
Restart and check SQLite in GAC was installed correctly by clicking the blue question mark above your connected databases underneath SQLite toolbox.
At this point you should see "SQLite EF6 DbProvider in GAC - Yes."
Elsewise, you most likely configured your machine.config manually in the past. Delete the changes you made underneath system.data > DbProviderFactories.
Restart VS and you should see "SQLite EF6 DbProvider in GAC - Yes."
Install System.Data.SQLite under the NuGet manager.
You can now connect to SQLite as a binding source and directly select your database file through "SQLite Database" vs "SQLite Provider (Simple for EF6 by ErikEJ)."
refer to this link, this solution was taken from a detailed github post, step by step
I read this post which apparently solves the issue with WPF app publishing which has SQL Server Compact dependency. I went through all the steps prescribed by the author of this post but it just doesn't work for me. Once I add all the DLLs from SQL Server Compact "private" folder my app crashes on startup. The difference is that I use .NET 4.5 while the example deals with 4.0 I am not sure if that is the core of the problem but it just doesn't work with those settings.
My app is crashing with the following exception :
System.Windows.Markup.XamlParseException
Here is my app.config (including add on from the article ) :
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="TypeAppRelease.Properties.Settings.MyDatabase_1ConnectionString"
connectionString="Data Source=D:\XXXXXX\XXXXX\XXXXXXX\externals\MyDatabase#1.sdf;Password=XXXXXXX;Persist Security Info=True"
providerName="Microsoft.SqlServerCe.Client.4.0" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0"/>
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.1,
Culture=neutral,
PublicKeyToken=89845dcd8080cc91"/>
</DbProviderFactories>
</system.data>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
One more thing. I came to this because doing just ClickOnce publishing produced installation file which didn't work complaining that
The application is missing required files ....
So that lead me to the solution I linked above which also seems not to work.
Update:
I would like to extend on this problem because until not I haven't resolved it.
I decided to put aside ClickOnce deployment and just test Debug/Release builds on 2 machines:1) With SQL Compact Server installed ;2) Has only NET framework installed.
The App works ok on the first machine and fails on the second.That is pretty logical because the first one has Compact SQL installed and running.
Next I removed the server installation from the first machine and ran the app again.It still works , but not for the second PC .The error I am getting is
System.IO.FileLoadException
I can't figure out why I am getting it and why I am not getting the same on the first machine.
Maybe it is 64 /32 bit OS thing? PC1 runs Windows 7 64bit while PC2 x86.
I you use ADO.NET objects (SqlCeCommand, DataSet) instead of Entity Framework (as described in my blog post) you must reference System.Data.SqlServerCe.dll in your project folder (assembly version 4.0.0.1) and NOT the dll in your GAC (assembly version 4.0.0.0). I have updated my blog post with that information.
I developed a windows application using Entity Framework and as a database, I used SQLite. I have no problem developing it until I add a setup project to my solution to make an installer file for my application and I add .net Framework 3.5 and .net Framework 4.0 and Windows Installer as prerequisite to install.
In other computers, installation is done successfully but when users try to execute the application, they receive this exception
Unable to find the requested .Net Framework Data Provider
I googled this exception and most of the answers talked about provider name tag in web.config. Can anyone help me?
You have to include the System.Data.SQLite.dll provider assembly in your installer package (or make them required in a click-once download app) and then you have to register the provider by adding it to the providers section in the config file.
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
</DbProviderFactories>
I'm using System.Data.SQLite to access a SQLite database via c#/linq. I had no problems until I tried to use the Take()/Skip() functionality which caused an exception. The error message in case of Take() is "SQLite error near ".": syntax error".
Any ideas what I could do to get around this?
perhaps you need DBLinq http://code.google.com/p/dblinq2007/
I've had the same issue using VS2008 SP1, System.Data.Sqlite.Linq v2.0.38.0 and System.Data.Sqlite v1.0.66.0.
Solved a similar error (with LINQ's Skip) uncommenting DbProviderFactories section in app.config, don't ask me why it works.
That section shows this way:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
(I'm working with and old VS2008 project after an OS reinstall and I had to reinstall all dependencies, I think I've not installed the same exact Sqlite versions I had. It always worked with that app.config's section commented)
Obviously I'd like to not have to install an Oracle client (other than simply a DLL), because that would prohibit my ClickOnce goal. I tried System.Data.OracleClient, but it threw an exception, "System.Data.OracleClient requires Oracle client software version 8.1.7 or greater."
Update:
It seems that System.Data.OracleClient is not the way to go. So I downloaded the xcopy-able version of the Oracle ODP.NET provider. I then ran "install.bat odp.net20 c:\oracle odac" to install only ODP.NET. Then I added a reference in my project to C:\oracle\odp.net\bin\2.x\Oracle.DataAccess.dll. This allowed me to hit the Oracle DB. But when I take all my files to a clean machine (including the oracle DLL), I get an exception: "The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception." So it seems that it is still depending on some other files. But I don't know what files. And how does it know where the files are? A registry entry? Not looking good for ClickOnce.
Update:
It works as long as I include the files mentioned by mcauthorn: oci.dll, oracle.dataaccess.dll, oraociei11.dll, & oraops11w.dll. Unfortunately, oraociei11.dll is very large -- 114MB.
I would say to go get the ODP.Net Dlls here. You will have to grab the XCopy version, and then add the necessary DLLs to your project and you then should be good to go. The System.Data.OracleClient has been discontinued and is not recommended by Microsoft.
You can use third-party ADO.NET provider. Try this one. It does not require Oracle Client and runs on Mono.
Update, since the original answers are 4 years old.
Just use Oracle's Managed driver. You need only that, nothing else. You can then connect directly with a simple connection string to the database.
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client" />
<!-- If any should be in the machine.config -->
<add name="Oracle Data Provider for .NET" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<clear />
<add name="OracleContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="DATA SOURCE=<IP_ADDRESS>:1521/XE;PASSWORD=<PASSWORD>;USER ID=<USER_ID>;Connection Timeout=600;Validate Connection=true" />
</connectionStrings>
I used ServiceStack's OrmLite as ORM but it should work perfectly fine also directly if you don't want to use an ORM, or with any other. All you need to make sure is that your ORM library is using the managed driver as well and not the old one. I've written a blog post regarding a hack, where I had to change ServiceStack.OrmLite.Oracle to use the new managed driver. This solved all my problems.
Blog post: http://peter.grman.at/how-to-connect-mono-in-linux-to-oracle/
ServiceStack.OrmLite.Oracle.Managed NuGet package: https://www.nuget.org/packages/ServiceStack.OrmLite.Oracle.Managed/
The changes in GitHub: https://github.com/Grman-IT-Solutions/ServiceStack.OrmLite/tree/OracleManagedDataAccess