Risehuxyc头像

C#里的SQLite动态库的坑

不管是System.Data.SQLite还是Microsoft.Data.SQLite ,都出问题!动不动就是缺失DLL或依赖项缺失!

怎么解决
一定要把System.Data.SQLite.Core装到项目里!
运行环境里一定要有由SQLite.Interop.dll这个动态库!

//测试代码
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;


namespace TestSQLite
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SQLiteConnection conn = new SQLiteConnection("Data Source=test.db");
                conn.Open();
                Console.WriteLine("Connection opened successfully.");

                string sql = @"CREATE TABLE IF NOT EXISTS RelaySessionData (
                session_id INTEGER PRIMARY KEY AUTOINCREMENT,  -- 自增主键
                timestamp INTEGER NOT NULL,                    -- 时间戳(如 Unix 时间戳)
                From_ID TEXT NOT NULL,                        -- 发送方 ID(假设为文本类型)
                To_ID TEXT NOT NULL,                          -- 接收方 ID
                DataLen INTEGER CHECK (DataLen >= 0),         -- 数据长度(非负整数)
                BinData BLOB                                  -- 二进制数据
            );";
                SQLiteCommand command = new SQLiteCommand();
                command.Connection = conn;
                command.CommandText = sql;
                //应该不会出错,不用catchexception
                command.ExecuteNonQuery();

                sql = @"INSERT INTO RelaySessionData 
            (timestamp, From_ID, To_ID, DataLen, BinData)
            VALUES (
                strftime('%s','now') * 1000 + substr(datetime('now', 'localtime'), 21, 3),  -- 毫秒级时间戳
                @from, @to, @len, @data
            );";



                command.CommandText = sql;

                // 参数绑定
                command.Parameters.Clear();
                //this.command.Parameters.AddWithValue("@ts", timestamp);
                command.Parameters.AddWithValue("@from", 1001);
                command.Parameters.AddWithValue("@to", 1002);
                command.Parameters.AddWithValue("@len", 3);
                command.Parameters.AddWithValue("@data", new byte[] { 1, 2, 3 });

                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

转载自 CSDN-专业IT技术社区

原文链接:https://blog.csdn.net/huxyc/article/details/170201593

文章来源转载

评论

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--