简体   繁体   English

如何使用 asp.net webapi 将图像保存在数据库中

[英]How Can I save image in database usoing asp.net webapi

I am working on an API.我正在研究 API。 Everything is getting done smoothly so far, but I have to save images to the database in the API.到目前为止一切都很顺利,但我必须将图像保存到 API 中的数据库中。 Scenario, let's say I have a class of Person with three properties {Name, Age, and (byte[])Image} .场景,假设我有一个人的class具有三个属性{Name, Age, and (byte[])Image} How can add the Person's image in the database through API?如何通过 API 将 Person 的图像添加到数据库中?

Thanks.谢谢。

public IHttpActionResult Create([FromBody] Person person) { }

To achieve that you will need to use VarBinary in order to pass the image parameter, like in this example:为此,您需要使用VarBinary来传递图像参数,如下例所示:

public static void InsertByteArrayintoSqlDatabase()
{
    string sampleText = "Hello World!";

    byte[] byteData = Encoding.UTF8.GetBytes(sampleText);

    using (SqlConnection sqlconnection = new SqlConnection(@"Data Source=.SQLExpress; 
Initial Catalog=MorganDB; Integrated Security=SSPI;"))
    {
        sqlconnection.Open();

        // create table if not exists 
        string createTableQuery = @"Create Table [MyTable](ID int, [BinData] varbinary(max))";
        SqlCommand command = new SqlCommand(createTableQuery, sqlconnection);
        command.ExecuteNonQuery();

        string insertXmlQuery = @"Insert Into [MyTable] (ID,[BinData]) Values(1,@BinData)";

        // Insert Byte [] Value into Sql Table by SqlParameter
        SqlCommand insertCommand = new SqlCommand(insertXmlQuery, sqlconnection);
        SqlParameter sqlParam = insertCommand.Parameters.AddWithValue("@BinData", byteData);
        sqlParam.DbType = DbType.Binary;
        insertCommand.ExecuteNonQuery();
    }

Taken from https://morgantechspace.com/2014/05/how-to-store-and-read-byte-array-in-sql-server-using-csharp.html取自https://morgantechspace.com/2014/05/how-to-store-and-read-byte-array-in-sql-server-using-csharp.html

I do not recommend storing images as they are in the database.我不建议将图像存储在数据库中。 Instead, it makes much more sense to store them as files and store their name/path in the database.相反,将它们存储为文件并将它们的名称/路径存储在数据库中会更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使运行在Azure实例中的ASP.NET WebAPI程序每分钟更新一次数据库? - How can I make an ASP.NET WebAPI program running in an Azure instance update a database every minute? 如何从Asp.Net WebAPI接收消息 - How can I receive message from Asp.Net WebAPI 如何将表单数据发布到ASP.NET WebAPI 2? - How can I POST form data to ASP.NET WebAPI 2? ASP.NET MVC - 如何上传图像并在数据库中保存URL - ASP.NET MVC - How to upload an image and save URL in the database 使用ASP.NET MVC将图像保存到数据库 - Save image to database with ASP.NET MVC 如何在 wwwroot For ASP.NET MVC 中保存输入图像? - How can i save an input image in the wwwroot For ASP.NET MVC? 我如何调用rabbitMq 从asp.net Webapi 获取消息? - How can i call rabbitMq get message from asp.net Webapi? 如何转换将JSON返回列表的ASP.NET WebAPI调用? - How can I convert a ASP.NET WebAPI calls that returns JSON into a list? 如何对从C#控制台应用程序使用表单身份验证的ASP.NET WebAPI进行身份验证? - How can I authenticate to an ASP.NET WebAPI that is using Forms Authentication From a C# Console Application? 如何在返回对象的ASP.NET Core WebAPI控制器中抛出异常? - How can I throw an exception in an ASP.NET Core WebAPI controller that returns an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM