SQLite.Interop unable to delete file on clean of solution - c#

I'm using the Nuget package System.Data.SQLite in an MVC web application. There seems to be lock issue when I try to clean the solution and rebuild. The error message I get is:
Unable to delete file "bin\x64\SQLite.Interop.dll". Access to the path '\bin\x64\SQLite.Interop.dll' is denied.
I'm thinking that the database is either still open or that the .dll is still in use, but I can't find any documentation or any reference to the same problem. This question seems like a similar issue but doesn't provide a resolution to my problem.
Here is a code snippet that I'm using to write to the SQLite database:
var conn = new SQLiteConnection("Data Source=" + connectionString);
conn.Open();
var debugEntriesTableQuery = "CREATE TABLE ...";
var cmd = conn.CreateCommand();
cmd.CommandText = debugEntriesTableQuery;
cmd.ExecuteNonQuery();
conn.Close();
Is there another step needed to properly close the connection to the database and inform the dll and the connection has closed?
I am using this code inside a data solution that I have added to a nuget package and am using in another solution. I am only having this problem when building/cleaning the solution that is using my nuget package.

Killing the IIS Express process solves this error for me as pointed out by the link Eric points to. I am using Visual Studio 2015 community. I exit the IIS Express process from the taskbar.

Related

Could not add "ADO.net Entity Data Model" with sqlite

I am trying to setup entity framework with sqlite in visual studio 2012. So, here is what I have done so far:
Installed System.Data.Sqlite with "Design-Time Component".
Entity Framework Tools for visual studio 2012.
System.Data.SQLite,System.Data.SQLite.EF6 and System.Data.SQLite.Linq from NuGet.
Now I am trying to add "ADO.net Entity Data Model",By following steps:
Add New Item.
ADO.net Entity Data Model.
"EF Designer from Database" from wizard and click "Next".
Add "New Connection", Data Source: System.Data.Sqlite Database File.
After choosing db file and provide password when I click on "Next" button, following error occurs:
I have checked my db file in "Sqlite Browser" also I have setup password for db file with the help of following code:
string conn = #"Data Source=pahtto\App_Data\app_data.sqlite;";
SQLiteConnection connection = new SQLiteConnection(conn);
connection.SetPassword("123456");
connection.Close();
What am I missing?
Please guide me.
Thank you.
Try looking in your App.config file and change the connection string and set your password there.
old connection string
source=X:projectsmeowdb.sqlite;
modified Connections string
source=X:projectsmeowdb.sqlite;password=this is my password
Look at this link for more information
http://www.dxsdata.com/2013/09/visual-studio-with-sqlite-solving-error-file-is-encrypted-or-not-a-database/
It seems that the problem was in my db file even though it was working fine in "Sqlite Browser".
So, I just deleted and created new file and every thing works.

Getting a working SpatiaLite + SQLite system for x64 c#

I need to create and access a spatialite-extended SQLite database under x64 windows.
I have downloaded the latest version 1.0.92.0 called sqlite-netFx45-static-binary-bundle-x64-2012-1.0.92.0.zip of System.Data.SQLite. It is referenced from my Visual Studio (2012) project, and seems to work just fine by itself.
I also have the latest precompiled x64 spatiaLite version 4.1.1 called spatialite-4.1.1-DLL-win-amd64.zip
All the dlls from spatialite are present in the executing directory.
When I try to load the extension:
using (var conn = new SQLiteConnection("Data Source=\"" + _sqLiteFullName + "\""))
{
conn.Open();
conn.EnableExtensions(true);
conn.LoadExtension("libspatialite-4.dll");
...
}
I get an AccessViolationException (Attempted to read protected memory. This is often an indication that other memory is corrupt) on the LoadExtension() line.
I notice when looked at with PE Deconstructor (software that determines the bitnewss of dll/exe), it says that my copy of System.Data.SQLite.dll (from the x64 package) is actually 32bits. Is that the problem?
How to I remedy this?
How has anyone else got spatiaLite working on x64?
Actually, the problem could be in spatialite-4.dll this autmun I spent a week trying to fix the same issue without success. It looks like that there problems in spatialite-4.dll (I mean this one downloaded form gaia-sins (official spatialite site) )
You can try to build a Spatialite from sources (like a nightmare (: ) or try to look for another build of .dll. Second option helped me.
Btw, there a couple of .dlls you need to use Spatialite extension:
libsqlite3-0.dll
libgeos-3-0-2.dll
libgeos-c-1.dll
libiconv2.dll
libproj-0.dll
libvirtualtext-2.dll
libspatialite-2.dll
<----- Spatialite v.2 completely suits my project. As I told, if you need v.4 you can try to build it or look for a another build.
Hope, this helps
download mod_spatialite from the site, choose mod_spatialite-4.2.0-win-amd64.7z. unzip and copy all dll to the bin folder of your program.
sample code:
//SELECT load_extension("mod_spatialite") // doesn't need the '.dll' suffix.
using (var cnn = new SQLiteConnection(connStr))
{
//connStr = "FullUri=file::memory:?cache=shared;Pooling=True;Max Pool Size=200;";
cnn.Open();
//cnn.EnableExtensions(true);
using (SQLiteCommand mycommand = new SQLiteCommand("SELECT load_extension(\"mod_spatialite\")", cnn))
{
mycommand.ExecuteNonQuery();
}
Have a look in this Google Groups discussion here:
https://groups.google.com/forum/#!topic/spatialite-users/u2QZpQL_6ek
The latest solution is by Dominik:
I just found out, that the hack described at
http://blog.jrg.com.br/2016/04/25/Fixing-spatialite-loading-problem/
only works with the dlls from the second most recent version of
mingw64 mingw-w64-bin_x86_64-linux_20131228.tar.bz2 from
http://netassist.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win64/Automated%20Builds/mingw-w64-bin_x86_64-linux_20131228.tar.bz2.
Any attempt to do the same with the most recent version available at
sourcefourge
http://sourceforge.net/projects/mingw-w64/files/latest/download
failed on my system.
However, I can definitely confirm, that I can load mod_spatialite with
MyConnection.LoadExtension("mod_spatilite");

C# SQL Server CE not inserting

SqlCeConnection sqlCnn =
new SqlCeConnection(Properties.Settings.Default.mainDBConnectionString);
SqlCeCommand sqlCmd = new SqlCeCommand(
"INSERT INTO desktopItems (Location,Label) VALUES (#Location, #Label)",
sqlCnn);
sqlCnn.Open();
sqlCmd.Parameters.Add("#Location", openExe.FileName.ToString());
sqlCmd.Parameters.Add("#Label", openExe.SafeFileName.ToString());
sqlCmd.ExecuteNonQuery();
sqlCnn.Close();
I have this code but when I run the program, the database is not updating ...
Usually this scenario is caused by a simple error in visualizing the database.
Your INSERT works as expected, but you check if the insert succeded looking at a database in the wrong directory.
Using the DATADIRECTORY substitution string with a WinForms application means that, at debug time, your database is expected to be located in the directory BIN\DEBUG from your base project folder.
Visual Studio make sure that this is the case because in your project, the database file, is marked with the property Copy To The Output Directory set to Copy Always or Copy If Newer.
And it is here that the insert happens when you run your code inside a debug session of Visual Studio.
Then you check the result of the execution using the SERVER EXPLORER connection. But this connection points to the original database in the Project Folder and, of course, the new record is not present.
Usually the database in the project folder is kept up to date for the deployement, with the correct schema and initial data, but without any records that are inserted just for debug purpose.
So you could simply add a new connection to the SERVER EXPLORER pointing to the database in the BIN\DEBUG, rename it (like 'DEBUG-DB') and keep your original connection in case you need to change something in the schema of the database before releasing your application.

ADO.Net - Why isn't the insert happening?

I went through MSDN pages to learn ADO.Net using Commands. I am able to read using the sample code posted there.
But when I tried to use the modification code below, the insert is not happening. I am not ale to figure out why. Can someone please tell me what is wrong with this code?
string connectionString = "A_VALID_CONNECTION_STRING";
string commandText =
"INSERT INTO Contacts (FullName, Mobile) VALUES ('Pierce Brosnan', '1800-007')";
SqlConnection connection = new SqlConnection(connectionString);
try
{
connection.Open();
SqlCommand command = new SqlCommand(commandText, connection);
Console.WriteLine(command.ExecuteNonQuery());
connection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Edit
No exception is thrown.
The ExecuteNonQuery() which is supposed to return the no. of rows affected is returning 1.
Environment: Visual C# 2010 Express | SQL Server 2008 Express | Windows 7 Ultimate 32-bit.
Update
Previously I was using a MDF file present in the project. It was, I guess, automatically attached to the SQL server instance each time the project ran. This is when I had the problem. The connection string had some info about attaching a database file.
I removed the SQL Server 2008 Express that I installed along with Visual C# 2010 Express. Also removed the MDF file from the project.
I Separately downloaded and installed SQL Server 2008 Express along with Management Studio Express.
Created a new database in management studio.
Used a different type of connection string to use the database in the server.
Now INSERT is working!
P.S. I guess I should have mentioned that I had an attach database file scenario. Really sorry for that.
My suspicion is that you had the following scenario:
Database.mdf file was present in the project with the table structure created in it
Your connection string looked something like this Server=.\SQLExpress;AttachDbFilename=database.mdf;Database=dbname; Trusted_Connection=Yes;, i.e. loading the database in the connection string.
What was happening was, when you built/ran your project, your application was compiled and the database.mdf file was copied along with it to ApplicationProjectFolder\bin\Debug, so that when the application was run, the file database.mdf was present. This means that everytime you ran your project, the "empty" database.mdf file was copied from ApplicationProjectFolder\database.mdf to ApplicationProjectFolder\bin\Debug\database.mdf, hence the data "disappearing". Also, the file database.mdf probably had "Copy Always" set on its properties in the project.
So, the "INSERT" was working, it was just being "reset" everytime you ran your application.

sqlite3.dll and system.data.sqlite.dll

Hello people I've been struggling to use sqlite in my C#2.0 application and I have finally decided to get rid of assumptions and ask really basic questions.
When I created a database say iagency with table users, from external tools like firefox plugging and another sqladmin tool I can't query it from sqlicommand inside vs2005 it displays System.Data.SQLite.SQLiteException:Sqlite Error no such table users, please be assured that I've made reference to system.data.sqlite installed with SQLite-1.0.61.0-setup
When I do the opposite like create a database and a table from VS server explorer and VS database gui tools it can't be queried neither but can be seen by other tools, but tables created through query from VS using stringbuilder eg create table bla bla. it can be display in a datagrid but none of the tools can see and display that table.
WHAT DO I NEED EXACTLY TO MAKE SQLITE WORK IN MY APPLICATION?
I've tried to add sqlite3.dll of sqlitedll-3_6_14.zip downloaded from sqlite site under section precompiled binaries for windows as reference to my application but it fails with make sure it's accessible an it's a valid assembly or com component.
I downloaded this SQLite-1.0.61.0-setup.exe Ran the installation then I wrote this to access the firefox favorites sqlite db.
using System.Data.SQLite; // Dont forget to add this to your project references
// If the installation worked you should find it under
// the .Net tab of the "Add Reference"-dialog
namespace sqlite_test
{
class Program
{
static void Main(string[] args)
{
var path_to_db = #"C:\places.sqlite"; // copied here to avoid long path
SQLiteConnection sqlite_connection = new SQLiteConnection("Data Source=" + path_to_db + ";Version=3;New=True;Compress=True;");
SQLiteCommand sqlite_command = sqlite_connection.CreateCommand();
sqlite_connection.Open();
sqlite_command.CommandText = "select * from moz_places";
SQLiteDataReader sqlite_datareader = sqlite_command.ExecuteReader();
while (sqlite_datareader.Read())
{
// Prints out the url field from the table:
System.Console.WriteLine(sqlite_datareader["url"]);
}
}
}
}
Try opening up the database in the command line SQLite tool (from SQLite.org), and check the schema.
You can check the schema in this way:
.schema
This will dump out all the SQL necessary to create the tables in the database. Make sure the table is there, with the name you assume it should have.
You do not need the .dll file from SQLite.org, all you need is the assemblies from System.Data.SQLite.
For me - this link helped a lot at start.
Was harder to get subsonic work, to make database accessible through web application -
but that's another story.
You might try adding the location of the assembly and the db to the Path environment variable. The SQLite assembly contains both .Net and native code merged together, so you do not need the C dll. (the mergebin tool they include to do this is pretty interesting)
I also tried adding the location to Path environment variable but without success.
Finally I copied System.Data.SQLite.dll and System.Data.SQLite.lib into the bin folder of the Web application where other assemblies are located, and application worked.

Categories

Resources