zoukankan      html  css  js  c++  java
  • SQLite数据库的增删改查代码

    //搜集整理了一下网上的代码.找了半天居然找不到一条插入语句.好郁闷的

    //感觉速度还可以.小文件.很多小应用程序在用这个数据库

    //SQLite使用办法.直接COPYDLL文件System.Data.SQLite.DLL到应用程序DEBUG目录下。 然后在项目中添加引用,找到这个文件即可

    //添加引用

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    using System.IO;
    using System.Net;
    using System.Data.SQLite;

    private static string FavoriteDbPath = System.Environment.CurrentDirectory + "//Favorite.db";

    private bool CreateSQLiteDb()
            {
                try
                {
                    System.Data.SQLite.SQLiteConnection.CreateFile(FavoriteDbPath);
                    System.Data.SQLite.SQLiteConnection Conn = new System.Data.SQLite.SQLiteConnection();
                    System.Data.SQLite.SQLiteConnectionStringBuilder ConnStr = new System.Data.SQLite.SQLiteConnectionStringBuilder();
                    ConnStr.DataSource = FavoriteDbPath;
                    ConnStr.Password = "Admin";//设置密码,SQLite ADO.NET实现了数据库密码保护
                    Conn.ConnectionString = ConnStr.ToString();
                    Conn.Open();
                    System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
                    string Sql = "CREATE TABLE Admin(UserName varchar(20),UserPass varchar(20))";
                    cmd.CommandText = Sql;
                    cmd.Connection = Conn;
                    cmd.ExecuteNonQuery();
                    Conn.Dispose();
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return false;
                }
            }

            public bool CreateLinkDataTable()
            {
                try
                {
                    System.Data.SQLite.SQLiteConnection Conn = new System.Data.SQLite.SQLiteConnection();
                    System.Data.SQLite.SQLiteConnectionStringBuilder ConnStr = new System.Data.SQLite.SQLiteConnectionStringBuilder();
                    ConnStr.DataSource = FavoriteDbPath;
                    ConnStr.Password = "Admin";//设置密码,SQLite ADO.NET实现了数据库密码保护
                    Conn.ConnectionString = ConnStr.ToString();
                    Conn.Open();
                    System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
                    string Sql = "CREATE TABLE [FavoriteList] ([ID] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,[LinkText] NVARCHAR(256)  NULL,[LinkUrl]  NVARCHAR(1000),[AddTime] TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL)";
                    cmd.CommandText = Sql;
                    cmd.Connection = Conn;
                    cmd.ExecuteNonQuery();
                    Conn.Dispose();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            public bool InsertLinkDataTable(string strLinkText,string strLinkUrl,DateTime dtFileTime)
            {
                try
                {
                    System.Data.SQLite.SQLiteConnection Conn = new System.Data.SQLite.SQLiteConnection();
                    System.Data.SQLite.SQLiteConnectionStringBuilder ConnStr = new System.Data.SQLite.SQLiteConnectionStringBuilder();
                    ConnStr.DataSource = FavoriteDbPath;
                    ConnStr.Password = "Admin";//设置密码,SQLite ADO.NET实现了数据库密码保护
                    Conn.ConnectionString = ConnStr.ToString();
                    Conn.Open();
                    System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
                    string strInsertSQL = "INSERT INTO [FavoriteList] (LinkText,LinkUrl,AddTime) VALUES('" + strLinkText + "','" + strLinkUrl + "','" + dtFileTime + "');";
                    Console.WriteLine(strInsertSQL);
                    cmd.CommandText = strInsertSQL;
                    cmd.Connection = Conn;
                    cmd.ExecuteNonQuery();
                    Conn.Dispose();
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return false;
                }
            }
            public bool GetLinkDataTableRecordList()
            {
                try
                {
                    System.Data.SQLite.SQLiteConnection Conn = new System.Data.SQLite.SQLiteConnection();
                    System.Data.SQLite.SQLiteConnectionStringBuilder ConnStr = new System.Data.SQLite.SQLiteConnectionStringBuilder();
                    ConnStr.DataSource = FavoriteDbPath;
                    ConnStr.Password = "Admin";//设置密码,SQLite ADO.NET实现了数据库密码保护
                    Conn.ConnectionString = ConnStr.ToString();
                    Conn.Open();
                    System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
                    cmd.Connection = Conn;
                    cmd.CommandText = "select * from [FavoriteList]";
                    SQLiteDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        Console.WriteLine("编号 " + System.Convert.ToString(dr["id"]) + "           文本 " + (string)dr["Linktext"] + "           地址 " + (string)dr["LinkUrl"]);
                    }
                    Conn.Dispose();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }

            private void 创建数据库文件ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    if (System.IO.File.Exists(FavoriteDbPath))
                    {
                        System.IO.File.Delete(FavoriteDbPath);
                    }
                    if (CreateSQLiteDb())
                    {
                        MessageBox.Show("数据库创建成功");
                    }
                    else
                    {
                        MessageBox.Show("数据库创建失败");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("创建数据库文件出错" + ex.Message);
                }
            }

            private void 创建收藏夹数据表ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    if (CreateLinkDataTable())
                    {
                        MessageBox.Show("创建收藏夹数据表成功");
                    }
                    else
                    {
                        MessageBox.Show("创建收藏夹数据表失败");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("创建收藏夹数据表出错" + ex.Message);
                }
            }

            private void 查询收藏夹数据表ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    if (GetLinkDataTableRecordList())
                    {
                        MessageBox.Show("查询收藏夹数据表成功");
                    }
                    else
                    {
                        MessageBox.Show("查询收藏夹数据表失败");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("查询收藏夹数据表出错" + ex.Message);
                }
            }

            private void 插入新的数据记录ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    if (InsertLinkDataTable("GJKHKJHK","HLKJLJL",DateTime.Now))
                    {
                        MessageBox.Show("插入新的数据记录成功");
                    }
                    else
                    {
                        MessageBox.Show("插入新的数据记录失败");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("插入新的数据记录出错" + ex.Message);
                }
            }

  • 相关阅读:
    bzoj 3930: [CQOI2015]选数
    bzoj 2301: [HAOI2011]Problem b
    HDU 1695 GCD
    2017ACM/ICPC广西邀请赛-重现赛 1007.Duizi and Shunzi
    2017ACM/ICPC广西邀请赛-重现赛 1010.Query on A Tree
    2017ACM/ICPC广西邀请赛-重现赛 1004.Covering
    P3501 [POI2010]ANT-Antisymmetry
    P1171 售货员的难题
    P3385 【模板】负环
    P1659 [国家集训队]拉拉队排练
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306814.html
Copyright © 2011-2022 走看看