Using SQLite in C #

Source: Internet
Author: User
Tags password protection

(1) Download the SQLite version (sqlite-netfx40-setup-bundle-x64-2010-1.0.83.0) from the following URL:
Http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

using:using System.Data.SQLite used in Ann. cs files;

Add code: Sqliteconnection.createfile (DataSource); Run the Times wrong (the first time using C #, please help to analyze the cause of the error):

System.badimageformatexception not processed
Message= failed to load file or assembly "System.Data.SQLite, version=1.0.83.0, Culture=neutral,

Publickeytoken=db937bc2d44ff139 "or one of its dependencies. An attempt was made to load a program that is not properly formatted.
Source=usesqlite
Filename=system.data.sqlite, version=1.0.83.0, Culture=neutral, publickeytoken=db937bc2d44ff139
fusionlog==== Pre-bind status information = = =
LOG: User = Yonghang-pc\yonghang
LOG: DisplayName = System.Data.SQLite, version=1.0.83.0, Culture=neutral, publickeytoken=db937bc2d44ff139
(fully-

Specified
LOG: Appbase = file:///E:/Source/PC/UseSQLite/bin/Debug/
LOG: initial PrivatePath = NULL
Calling the assembly: Usesqlite, version=1.0.0.0, Culture=neutral, Publickeytoken=null.
===
LOG: This binding starts from the default load context.
LOG: The application configuration file was not found.
LOG: Use host profile:
LOG: Use C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config's computer configuration file.
Logs: Post-policy references: System.Data.SQLite, version=1.0.83.0, Culture=neutral, publickeytoken=db937bc2d44ff139
LOG: Try to download the new URL File:///E:/Source/PC/UseSQLite/bin/Debug/System.Data.SQLite.DLL.
Error: Failed to complete assembly installation (hr = 0x8007000b). The probe terminates.

StackTrace:
In UseSQLite.Form1.CreateSqliteDatabase ()
In Usesqlite.form1. ctor () position E:\Source\PC\UseSQLite\Form1.cs: line number 22
In UseSQLite.Program.Main () position E:\Source\PC\UseSQLite\Program.cs: line number 18
In system.appdomain._nexecuteassembly (runtimeassembly Assembly, string[] args)
In System.AppDomain.ExecuteAssembly (String assemblyfile, Evidence assemblysecurity, string[] args)
In Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()
In System.Threading.ThreadHelper.ThreadStart_Context (Object state)
In System.Threading.ExecutionContext.Run (ExecutionContext ExecutionContext, ContextCallback callback, Object

State, Boolean Ignoresyncctx)
In System.Threading.ExecutionContext.Run (ExecutionContext ExecutionContext, ContextCallback callback, Object

State
In System.Threading.ThreadHelper.ThreadStart ()
InnerException:

Code:

1 usingSystem; 2 usingSystem.Collections.Generic; 3 usingSystem.ComponentModel; 4 usingSystem.Data; 5 usingSystem.Drawing; 6 usingSystem.Linq; 7 usingSystem.Text; 8 usingSystem.Windows.Forms; 9    Ten usingSystem.Data.SQLite;  One     A namespaceUsesqlite - {   -      Public Partial classForm1:form the     {   -         Private stringDataSource ="Usesqlite.sqlite";  -         //private String DataSource = "E:/source/pc/sqlitedb.db";  -     +          PublicForm1 () -         {   + InitializeComponent ();  A     at createsqlitedatabase ();  -         }   -     -         voidcreatesqlitedatabase () -         {   -System.Diagnostics.Debug.WriteLine ("Use sqlite:start CREATE Database ...");  in Sqliteconnection.createfile (DataSource);  - //Sqliteconnection conn = new sqliteconnection ();  to //Sqliteconnectionstringbuilder connstr = new Sqliteconnectionstringbuilder ();  + //ConnStr.  DataSource = DataSource;  - //ConnStr. Password = "admin"; //set Password, SQLite ADO. NET implements the database password protection the //Conn. ConnectionString = ConnStr.  ToString ();  * //Conn.  Open ();  $System.Diagnostics.Debug.WriteLine ("Use sqlite:create database end."); Panax Notoginseng         }   -     }   the}

CSDN reply may be for the following reasons:
It is possible that the framework is not configured correctly, SQLite is. NET 4.0, and your console is. NET 4.0 Client profile;
. NET 4.0 is larger than the. NET 4.0 Client profile to view the project properties and change the test.
. NET 4.0 Client profile consumes less space than. NET 4.0, but the supported types are also smaller than those supported by. NET 4.0.
SQLite's dynamic library is not supported by. NET 4.0 Client profile.
Not verified If this is the case.

(2) Downloaded: Sqlite-1.0.66.0-setup.exe, after installation in the VS2010 menu "Add new Data Source" in the "New connection ..." Finally found the option of SQLite.
Then you need to change the project's target framework from 4.0 to 3.5, or compile an error.
Finally, in the project "Add Reference", select the \sqlite.net\bin directory: System.Data.SQLite.dll and System.Data.SQLite.dll (optional)

(3) Create a database

......

------------------------------------------------------------------------------------------------

* Downloaded from Http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki Sqlite-netfx35-setup-bundle-x86-2008-1.0.83.0.exe
* For VS2008 version, install. During the installation, some errors were prompted during the console interface, and the final installation was completed.
* Create a new C # project in VS2008, "Add References ..." Browse to the installation directory for Sqlite-netfx35-setup-bundle-x86-2008-1.0.83.0.exe
* For example, my installation directory: D:\Program Files\system.data.sqlite\2008\bin
* Choose System.Data.SQLite.dll, in the References of the project can see one more: System.Data.SQLite
* Added in the Form1.cs of the project: using System.Data.SQLite;
* Add code as follows:

* Compile and debug through, cause the cause of the problem, as Csdner said, the. Net version is not selected, resulting in the test the day before.

1 usingSystem.Data.SQLite; 2   3 namespaceTestusesqlite4 {  5      Public Partial classForm1:form6     {  7         Private stringDataSource ="Contactbookdb.sqlite"; 8   9          PublicForm1 ()Ten         {   One InitializeComponent ();  A    - Sqliteconnection.createfile (DataSource);  -    theSqliteconnection dbconn =NewSqliteconnection ("Data source="+DataSource);  -    - Dbconn.open ();  -    +Sqlitecommand dbcmd =Dbconn.createcommand ();  -    +Dbcmd.commandtext ="CREATE TABLE telephonebook (personID varchar), telephone varchar ((), type varchar )";  A Dbcmd.executenonquery ();  at    -Dbcmd.commandtext ="INSERT into Telephonebook VALUES (' MTB ', ' 1234567890 ', ' not mobile ')";  - Dbcmd.executenonquery ();  -    -Dbcmd.commandtext ="SELECT * from Telephonebook";  -Sqlitedatareader DataReader =Dbcmd.executereader ();  in    -DataTable datatable =NewDataTable ();  to             if(datareader.hasrows) +             {   - datatable.load (DataReader);  the             }   *    $Datagridview1.datasource =dataTable; Panax Notoginseng    - Datareader.close ();  the Dbconn.close ();  +         }   A     }   the}

Using SQLite in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.