winform中读写SQLite数据库例子

前端之家收集整理的这篇文章主要介绍了winform中读写SQLite数据库例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

App.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <!--FailIfMissing=false表示数据库不存在时就会自动创建-->
    <add key="Dbsqlite" value="data source=|DataDirectory|DB.db3;Pooling=true;FailIfMissing=false"/>
  </appSettings>
</configuration>

备注:如果开发环境是4.0,而System.Data.sqlite是比较低的版本,则可能会弹出错误信息“混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集”,解决方法是在上面加上

<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>

</startup>


数据库读写助手sqliteHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.sqlite;


public class sqliteHelper
{
    public sqliteHelper()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    private static sqliteConnection GetConnection()
    {
        string connStr = System.Configuration.ConfigurationManager.AppSettings["Dbsqlite"].ToString(); 
        sqliteConnection conn = new sqliteConnection(connStr);
        conn.Open();
        return conn;
    }

    public static int Executesql(string sql)
    {
        using (sqliteConnection conn = GetConnection())
        {
            var cmd = new sqliteCommand(sql,conn);
            return cmd.ExecuteNonQuery();
        }
    }

    public static int ExecuteScalar(string sql)
    {
        using (sqliteConnection conn = GetConnection())
        {
            var cmd = new sqliteCommand(sql,conn);
            object o = cmd.ExecuteScalar();
            return int.Parse(o.ToString());
        }
    }
    public static sqliteDataReader ExecuteReader(string sql)
    {
        sqliteConnection conn = GetConnection();
        var cmd = new sqliteCommand(sql,conn);
        sqliteDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return myReader;
    }
    public static DataSet ExecDataSet(string sql)
    {
        using (sqliteConnection conn = GetConnection())
        {
            var cmd = new sqliteCommand(sql,conn);
            sqliteDataAdapter da = new sqliteDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds;
        }
    }
}

窗体中应用Form1.cs

             //判断表是否存在,不存在则生成
            int result = sqliteHelper.ExecuteScalar("SELECT COUNT(*) FROM sqlite_master where type='table' and name='tb'");
            if (result == 0)
            {
                //创建表
                sqliteHelper.Executesql("create table [tb] (id integer PRIMARY KEY autoincrement,[name] varchar(20),[createDate] datetime default (datetime('now','localtime')))");
            }
            //插入一行数据
            result = sqliteHelper.Executesql("insert into [tb](name) values ('Luck')");
            if(result > 0)
            {
                string msg = "";
                //读取数据
                sqliteDataReader dr = sqliteHelper.ExecuteReader("select * from [tb]");
                if (dr.Read())
                {
                    msg += dr[0] + "," + dr[1] + "," + dr[2];
                }
                MessageBox.Show(msg);
            }
原文链接:https://www.f2er.com/sqlite/201565.html

猜你在找的Sqlite相关文章