C# .Net Core 操作Sqlite--方法一

参考:
https://download.csdn.net/download/zrf139138/9314893?ops_request_misc=&request_id=&biz_id=103&utm_term=%E6%97%A0%E6%B3%95%E5%8A%A0%E8%BD%BD%20DLL%22SQLite.Interop.dll%22:%E6%89%BE&utm_medium=distribute.pc_search_result.none-task-download-2downloadsobaiduweb~default-3-9314893.pc_v2_rank_dl_default

dll:
在这里插入图片描述

值得注意的是:
SQLite.Interop.dll(路径:C:\Users\withyzu\.nuget\packages\stub.system.data.sqlite.core.netstandard\1.0.113.2\lib\netstandard2.1
在这里插入图片描述

复制到
C:\Users\withyzu\source\repos\ConsoleApp11\bin\Debug\netcoreapp3.1
在这里插入图片描述
一、Program.cs

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

namespace ConsoleApp11
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建数据库
            string DBname = "Terminator.sqlite";
            if (!File.Exists("Terminator.sqlite"))
                SQLiteConnection.CreateFile($"{DBname}");
            //连接数据库
            SQLiteConnection conn = new SQLiteConnection($"Data Source = {DBname}; Version = 3");
            conn.Open();
            //创建表
            string sql = null;
            sql = "select count(*) from sqlite_master where type='table' and name= 'robot';";
            if (Convert.ToInt32((new SQLiteCommand(sql, conn)).ExecuteScalar()) == 0)
            {
                sql = $"create table robot(name string , model string)";
                (new SQLiteCommand(sql, conn)).ExecuteNonQuery();
            }
            //插入数据
            for (int i = 79; i <= 82; i++)
            {
                sql = $"insert into robot(name, model) values('Anrold-{Convert.ToChar(i)}', 'T800{Convert.ToChar(i)}')";
                (new SQLiteCommand(sql, conn)).ExecuteNonQuery();
            }
            //查询
            sql = "select * from robot";
            SQLiteDataReader reader = (new SQLiteCommand(sql, conn)).ExecuteReader();
            while (reader.Read())
                Console.WriteLine("Name: " + reader["name"] + "\tModel: " + reader["model"]);
            Console.ReadLine();
            //关闭
            conn.Close();
        }
    }
}


二、运行结果
在这里插入图片描述
三、解决方案管理器
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值