C # automatically register the SQLite ado.net database driver and custom connection string

Source: Internet
Author: User

Vista or later systems must have System Administrator privileges to execute the following code. Sqlite.net uses 1.0.66.0, supports the ado.net EF data model,: http://sourceforge.net/projects/sqlite-dotnet2/files/SQLite%20for%20ADO.NET%202.0/

During deployment, make sure that system. Data. SQLite. dll and system. Data. SQLite. LINQ. dll are in the input directory of the project. That is, the directory where your program EXE file is located.

Using system. configuration;
Using system. xml. LINQ;
Using system. kerberiseservices. Internal;

VaR d = configurationmanager. openmachineconfiguration (). filepath;
Xelement Xe = xelement. Load (d );
VaR dd = Xe. element ("system. Data"). element ("dbproviderfactories"). Elements ("add ");
If (DD. Where (MD => Md. Attribute ("name"). value. Equals ("SQLite data provider"). Count () = 0)
{
Publish objpub = new publish ();
Objpub. gacremove ("system. Data. SQLite. dll ");
Objpub. gacremove ("system. Data. SQLite. LINQ. dll ");
Objpub. gacinstall ("system. Data. SQLite. dll ");
Objpub. gacinstall ("system. Data. SQLite. LINQ. dll ");

Xe. element ("system. Data"). element ("dbproviderfactories"). Add (New xelement ("add ",
New xattribute ("name", "SQLite data provider"), new xattribute ("invariant ",
"System. Data. SQLite"), new xattribute ("Description", ". NET Framework data provider for SQLite "),
New xattribute ("type", "system. Data. SQLite. sqlitefactory, system. Data. SQLite, version = 1.0.66.0, culture = neutral, publickeytoken = db937bc2d44ff139 ")
));
Xe. Save (d );
}

The following provides a custom database connection method for the User-Defined ado.net EF:

Public entityconnection getentityconnection ()
{
Entityconnectionstringbuilder ecsb = new entityconnectionstringbuilder ();
Ecsb. metadata = string. format (@ "res: // */{0 }. CSDL | res: // */{0 }. SSDL | res: // */{0 }. MSL "," model1 ");
Ecsb. provider = "system. Data. SQLite ";
Ecsb. providerconnectionstring = @ "Data Source = data. DB; Password = admin ";
Entityconnection EC = new entityconnection (ecsb. tostring ());
Return EC;
}

Use Time:

Dataentities de = new dataentities (getentityconnection ());
Datagrid1.itemssource = de. usertable;

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.