Objective
In recent days, several articles on the content of non-content, such as the series of lectures, these days are my project in the problems and re-learning knowledge, so share with you, the article about SQLite abound, but some of the most outdated, why say outdated, Before all in the website of sqlite download dll or EXE to achieve, now we can directly install the SQLite package, but also convenient for us, at the same time, the author is not used for SQLite, which is also the project needs to learn the use of SQLite, welcome and we discuss together.
Installing SQLite
Install the SQLite package via NuGet as follows:
In this case, you will naturally have the following assemblies, such as SQLite, Sqlitecore, Sqliteef, etc.
Then is to write our SQLite statement, is simply so easy! in the study we can play, just play, in the actual project, we put these assemblies directly in the Pacakages, it is certainly not so, we just need the assembly. The use of SQLite must be " System.Data.SQLite.dll " and " SQLite.Interop.dll " is no longer described, the other EF and LINQ assemblies are used in conjunction with EF, We can depend on the situation of the project, but we do not add it here. We removed these assemblies in the package at the same time in order to prevent the rebuild of the solution when NuGet was re-installed in the package configuration "Packages.config" , and we removed the few in the package configuration. At this point we'll build the solution again with the following error:
According to experience, in general, if the project is named or other modifications to build the solution, sorta is modified to modify the content of the success but in the project with the extension " csproj " The project master file does not make corresponding changes caused by, we go to look for.
We see that the file will be reloaded when the solution is built, the file will be imported into SQLite but we have deleted the following error when we delete its import and error content to build the solution successfully. We place the assembly as follows:
(1) Non-empty MVC project
Let's write a statement inserted into the SQLite database, and we're ready to insert the SQLite statement:
/// <summary>
/// Perform addition, deletion, and modification operations on the SQLite database, and return the number of affected rows.
/// </ summary>
/// <param name = "sql"> Add, delete, modify SQL statements to be executed </ param>
/// <param name = "parameters"> Parameters required to execute additions, deletions, and modification statements. The parameters must be in the order they appear in the SQL statement. </ param>
/// <returns> </ returns>
public static int ExecuteNonQuery (string sql)
{
int affectedRows = 0;
if (IsDBInitialized ())
{
try
{
using (SQLiteConnection connection = new SQLiteConnection (s_ConnectionString))
{
connection.Open ();
using (DbTransaction transaction = connection.BeginTransaction ())
{
using (SQLiteCommand command = new SQLiteCommand (connection))
{
command.CommandText = sql;
affectedRows = command.ExecuteNonQuery ();
}
transaction.Commit ();
}
}
}
catch (Exception ex)
{
}
}
return affectedRows;
}
We give the INSERT test statement:
Sqlitehelper.executenonquery ("insertintovalues(1,2)");
Let's look at the results of the demo:
As a result, as we expected, keep in mind that this demonstration is based on the premise that our project is a "non-empty MVC project, the basic MVC project", while "SQLite.Interop.dll" and "System.Data.SQLite.dll" The assembly is also placed in the same folder.
(2) An empty MVC project and SQLite is in the same project
Now let's show the second case, we set up an empty MVC project to put the above two SQLite assemblies in the same folder we'll try to insert the data.
The result is the following error:
At this point we put "SQLite.Interop.dll" in the x86 or x64 folder according to its own operating system to take this assembly. Look at the results of the demo:
At this point, if the "SQLite.Interop.dll" assembly is successful, let's look at whether x64 or x86 can be named any other name:
As we modified the name of the "SQLite.Interop.dll" Assembly to test, the entry point is still not found. From here you can see that the "SQLite.Interop.dll" assembly folder name is either x86 or x64. Well, it's over here, isn't it? We've been demonstrating that SQLite is placed in a project, what if it's a different project? Let's take a look.
(3) An empty MVC project but SQLite is in another project
About putting "SQLite.Interop.dll" and "System.Data.SQLite.dll" in the same folder is no longer demonstrated, obviously not in the same project, not to mention in another project, let's look at the structure placed in another project:
As we did with the above empty MVC project, let's look at the results:
Let's drop the glasses, put SQLite alone in a project the error occurs, when we set the "SQLite.Interop.dll" assembly Properties "copy to Output directory" to "Always copy " Look, we look at the results of the demo:
Well, to the end of the complete, these situations you are confused or understand.
Summarize
(1) About establishing a non-empty MVC project in the building SQLite you love how to play on the play, but learning only, speculation is probably MVC built to do the processing.
(2) about establishing an empty MVC project built in SQLite, you cannot put "SQLite.Interop.dll" and "System.Data.SQLite.dll" in the same folder, you need to "SQLite.Interop.dll" The assembly is placed in the x86 or x64 folder and the folder name cannot be changed.
(3) Regarding the establishment of an empty MVC project but when you build SQLite into a single project, "SQLite.Interop.dll" and "System.Data.SQLite.dll" cannot be placed in the same folder, you need to " SQLite.Interop.dll "in the x86 or x64 folder and the folder name cannot be changed, the" Copy to output directory "of the" SQLite.Interop.dll "property must be set to" Copy always ".
All of the above attention is a place to be aware of with the latest SQLite installed by NuGet, rather than seeking another method.
NuGet package after installing SQLite completely out of the introduction of SQLite and notes, do you really understand?