WinForm Read and Write SQLite database example (RPM)

Source: Internet
Author: User
Tags sqlite sqlite database

. Net SQLite Database driver and System.Data.SQLite.dll download the latest address:

Http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

SQLite Management tools:

Http://www.cr173.com/soft/94247.html

App. Config file modification:

[HTML]View Plaincopyprint?
  1. <? XML version="1.0"?>
  2. <configuration>
  3. <appSettings>
  4. <!--failifmissing=false means that the database is automatically created when it does not exist
  5. <add key= "dbsqlite" value= "data source=| datadirectory| DB.DB3; Pooling=true; Failifmissing=false "/>
  6. </appSettings>
  7. </configuration>

Note: If the development environment is 4.0, and System.Data.Sqlite is the lower version, the error message "Mixed-mode assemblies are generated for the v2.0.50727 version of the runtime and cannot be configured with no additional information in 4.0 Load the assembly in the runtime, the workaround is to add:

<startup uselegacyv2runtimeactivationpolicy= "true" >
<supportedruntime version= "v4.0"/>

</startup>

Database read-write assistant SqliteHelper.cs

[CSharp]View Plaincopyprint?
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Using System.Data;
  6. Using System.Data.Common;
  7. Using System.Data.SQLite;
  8. Public class Sqlitehelper
  9. {
  10. Public sqlitehelper ()
  11. {
  12. //  
  13. //todo: Add constructor logic here
  14. //  
  15. }
  16. private static sqliteconnection getconnection ()
  17. {
  18. string connstr = system.configuration.configurationmanager.appsettings["Dbsqlite"].   ToString ();
  19. Sqliteconnection conn = new Sqliteconnection (CONNSTR);
  20. Conn. Open ();
  21. return conn;
  22. }
  23. public static int executesql (string sql)
  24. {
  25. using (Sqliteconnection conn = getconnection ())
  26. {
  27. var cmd = new Sqlitecommand (SQL, conn);
  28. return cmd.  ExecuteNonQuery ();
  29. }
  30. }
  31. public static int executescalar (string sql)
  32. {
  33. using (Sqliteconnection conn = getconnection ())
  34. {
  35. var cmd = new Sqlitecommand (SQL, conn);
  36. object o = cmd.  ExecuteScalar ();
  37. return Int.  Parse (O.tostring ());
  38. }
  39. }
  40. public static Sqlitedatareader ExecuteReader (String sql)
  41. {
  42. Sqliteconnection conn = getconnection ();
  43. var cmd = new Sqlitecommand (SQL, conn);
  44. Sqlitedatareader myreader = cmd. ExecuteReader (commandbehavior.closeconnection);
  45. return myreader;
  46. }
  47. public static DataSet execdataset (String sql)
  48. {
  49. using (Sqliteconnection conn = getconnection ())
  50. {
  51. var cmd = new Sqlitecommand (SQL, conn);
  52. Sqlitedataadapter da = new Sqlitedataadapter (CMD);
  53. DataSet ds = new DataSet ();
  54. Da. Fill (DS);
  55. return DS;
  56. }
  57. }
  58. }

Apply Form1.cs in a form

[CSharp]View Plaincopyprint?
    1. //Determines whether the table exists, does not exist, and generates
    2. int result = Sqlitehelper.executescalar ("Select COUNT (*) from sqlite_master where type= ' table ' and name= ' TB '");
    3. if (result = = 0)
    4. {
    5. //Create a table
    6. Sqlitehelper.executesql ("CREATE TABLE [TB] (ID integer PRIMARY KEY autoincrement, [name] varchar (), [CreateDate] da  Tetime Default (DateTime (' Now ', ' localtime ')));
    7. }
    8. Insert a row of data
    9. result = Sqlitehelper.executesql ("INSERT INTO [TB] (name) VALUES (' Luck ')");
    10. if (Result > 0)
    11. {
    12. string msg = "";
    13. //Read Data
    14. Sqlitedatareader dr = Sqlitehelper.executereader ("SELECT * from [TB]");
    15. if (Dr. Read ())
    16. {
    17. msg + = Dr[0] + "," + dr[1] + "," + dr[2 ";
    18. }
    19. MessageBox.Show (msg);
    20. }

WinForm Read and Write SQLite database example (RPM)

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.