c# 连接sqlite

因为之前安装的sqlite的版本是3.23.0

但是在网上安装c#对应的驱动,发现并没有,只能是3.24.0版本的,但是又想到3.23和3.24,还有3.25也只是差几个月而已,所以,直接安装最新版本的应该问题很小。

PM> Install-Package System.Data.SQLite -Version 1.0.109.1

在PM中选择安装sqlite版本,安装之后,直接编写代码测试运行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;


/**
 create table company(
id int primary key,
name varchar(20),
age short,
address varchar(50),
salary decimal(10,3)
);

insert into company values(1,'Paul', 32, 'California', 20000);
insert into company values(2,'Allen', 25, 'Texas', 15000);
insert into company values(3,'Teddy', 23, 'Norway', 20000);
insert into company values(4,'Mark', 25, 'Rich-Mond', 65000);
insert into company values(5,'David', 27, 'Texas', 85000);
insert into company values(6,'Kim', 22, 'South-Hal', 45000);
insert into company values(7,'James', 24, 'Houston', 10000);
insert into company values(8,'Paul', 24, 'Houston', 20000);
insert into company values(9,'James', 44, 'Norway', 5000);
 */

namespace 数据库连接.sqlite
{
    class Sqlite
    {
        private static string connStr = //"Data Source=" + @"C:\Users\tdht\Desktop\test.db" + ";Version=3;";
            "Data Source=" + @"C:\Users\tdht\Desktop\test.db;Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";
        private static SQLiteConnection conn = conn = new SQLiteConnection(connStr);

        //连接测试
        static void f0()
        {
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                Console.WriteLine("连接成功!");
            }
            conn.Close();
        }


        //查询sqlite数据库
        static void f1()
        {
            string sql = "select *from company";
            SQLiteCommand cmd = new SQLiteCommand(sql, conn);
            SQLiteDataReader reader = cmd.ExecuteReader();
            while (reader.Read())//初始索引是-1,执行读取下一行数据,返回值是bool
            {
                Console.WriteLine("id:"+ (long)reader["id"] + ", name:" + (string)reader["name"]);
            }
            
        }

        static void f2()
        {
            string sql = "insert into company(name, age, address, salary) values('土豆', 24, 'BeiJing', 21000)";
            SQLiteCommand cmd = new SQLiteCommand(sql, conn);
            int row = cmd.ExecuteNonQuery();
        }

        static void f3()
        {
            string sql = "update company set salary=26000 where id=10";
            SQLiteCommand cmd = new SQLiteCommand(sql, conn);
            int row = cmd.ExecuteNonQuery();
        }

        static void f4()
        {
            string sql = "delete from company where name='土豆'";
            SQLiteCommand cmd = new SQLiteCommand(sql, conn);
            int row = cmd.ExecuteNonQuery();
        }

        public static void TestSqlite()
        {
            try
            {
                //测试连接是否成功。
                f0();
                conn.Open();
                //查询。
                f1();
                //增加。
                f2();
                //修改
                f3();
                //删除
                //f4();

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            Console.ReadKey();
        }
    }
}

这样就能直接运行起来。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值