OrmLite与Filestream [英] OrmLite With Filestream

查看:95
本文介绍了OrmLite与Filestream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些搜索,但在OrmLite中利用文件流却找不到很多.我认为这是可能的,但我不确定该朝哪个方向前进.

I did some searching and I could not find very much on utilizing filestream with OrmLite. I think it is possible but I am not sure which direction to take.

理想情况下,我希望能够基于具有二进制字段的模型来创建或删除/创建表,然后执行 something 将数据库中的该列映射到文件流.我知道必须先在sql server上设置文件流(我认为您不能从Management Studio/Configuration Manager外部进行所有文件流设置)

Ideally I would like to be able to create or drop/create a table based on a model with a binary field and then do something to make that column in the database mapped to the filestream. I know that the filestream has to be setup on sql server ahead of time (I don't think you can do ALL the filestream setup from outside of the Management Studio / Configuration Manager)

是否有一种使用OrmLite做到这一点的方法?我确实找到了这部分:

Is there a way to do this using OrmLite? I did find this part:

db.ExecuteNonQuery("UPDATE Person SET LastName = @ name WHERE ID = @ id",new {name ="WaterHouse",id = 7});

db.ExecuteNonQuery("UPDATE Person SET LastName=@name WHERE Id=@id", new { name = "WaterHouse", id = 7 });

并且:

创建表Archive.dbo.Records ( [Id] [uniqueidentifier] ROWGUIDCOL NOT NULL UNIQUE, [SerialNumber]整数唯一, [图表] VARBINARY(MAX)文件流为NULL ) 开始

CREATE TABLE Archive.dbo.Records ( [Id] [uniqueidentifier] ROWGUIDCOL NOT NULL UNIQUE, [SerialNumber] INTEGER UNIQUE, [Chart] VARBINARY(MAX) FILESTREAM NULL ) GO

ALTER TABLE可能有效,但我不知道如何结合使用SQL在OrmLite或Ormlite中单独修改一列.

ALTER TABLE might work but I can't figure out how to combine modifying just one column using SQL in OrmLite or Ormlite on its own.

推荐答案

使用OrmLite不太可能做到这一点,但是您可以编写一个扩展方法,该扩展方法使用OrmLite基础执行您想要的操作(下面的代码)是v3).此方法只是删除所有varbinary列,然后将它们重新添加为文件流:

This isn't quite possible out of the box with OrmLite, but you can write an extension method that uses the OrmLite base to do what you want (code below is v3). This method simply drops all of the varbinary columns and re-adds them as filestreams:

public static class OrmLiteBinaryCreateExtensions
{
    /// <summary>
    /// WARNING: this will drop all of the existing varbinary columns!
    /// </summary>
    public static void UpdateByteToBinary<T>(this IDbConnection dbConn)
    {
        var modelDef = ModelDefinition<T>.Definition;
        var tableName = OrmLiteConfig.DialectProvider.GetQuotedTableName(modelDef);
        var definitions = modelDef.FieldDefinitions.Where<FieldDefinition>(f => f.FieldType == typeof(byte[]));

        foreach (var def in definitions)
        {
            var columnName = OrmLiteConfig.DialectProvider.GetQuotedColumnName(def.FieldName);
            dbConn.ExecuteNonQuery(string.Format("ALTER TABLE {0} DROP COLUMN {1}", tableName, columnName));
            dbConn.ExecuteNonQuery(string.Format("ALTER TABLE {0} ADD {1} [varbinary](max) filestream NULL", tableName, columnName));
        }
    }
}

用法:

using (IDbConnection db = dbFactory.OpenDbConnection())
{
    // create temp file, read bytes, delete it
    var tmp = Path.GetTempFileName();
    using (var fs = new FileStream(tmp, FileMode.OpenOrCreate))
    {
        var textBytes = System.Text.Encoding.UTF8.GetBytes("some text");
        fs.Write(textBytes, 0, textBytes.Length);
    }
    byte[] bytes = File.ReadAllBytes(tmp);
    File.Delete(tmp);


    // stand up data in orm lite
    db.CreateTableIfNotExists<FileExample>();

    // here is our extension method - note that this will drop the entire file column
    // and lose all existing data
    db.UpdateByteToBinary<FileExample>();

    db.Insert<FileExample>(new FileExample { Name = "my new file", FileBytes = bytes  });

    // read data back to ensure it saved properly
    var files = db.Select<FileExample>();

    using (var ms = new MemoryStream(files[0].FileBytes))
    {
        var someText = System.Text.Encoding.UTF8.GetString(files[0].FileBytes);

        Console.WriteLine(someText);
    }

    db.DropTable<FileExample>();
}

这篇关于OrmLite与Filestream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆