X3

RedSky

导航

C#Sqlite插入/更新并返回数据

关键词:returning

返回所有列:returning *;

返回指定列:returning columnname;

删除命令不支持returning

参考代码:

string path = @"Data Source=D:\Data\data.sqlite;Version=3";
using (SQLiteConnection conn = new SQLiteConnection(path))
{
    conn.Open();
    string sql = "insert into Table1 (`Column1`,`Column2`) values ('test', '123') returning *";
    //sql = "select * from Table1";
    sql = "update Table1 set `Column2`='12345' where `Column1`='test' returning *";
    //sql = "delete from Table1 where `Column1`='test'"; //delete不支持returning
    using(SQLiteCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = sql;
        using (var reader = cmd.ExecuteReader())
        {
            while(reader.NextResult())
            { }
            if (reader.Read() && reader.FieldCount > 0)
            {
                var a = reader[0];
                DataTable dt = new DataTable();
                dt.Load(reader);
                var list = dt.ToListModel<ClassA>();
                ;
            }
            else
            {
                int records = reader.RecordsAffected;
            }
        }
    }
}

 

posted on 2024-02-20 15:32  HotSky  阅读(215)  评论(0编辑  收藏  举报