C# 插入和检索模型对象作为 BLOB to/fro 数据库
C# Insert and Retrieve a model object as BLOB to/fro database
无论我在哪里问这个关于 C# 的问题,我都只得到图像和文件的答案。我想将对象作为 BLOB 而不是图像从一个 asp.net 应用程序存储到数据库中,然后从另一个应用程序检索它。
假设我有一个模型 Person
:
public class Person
{
public int userID { set; get; }
public string userName { get; set; }
public string lastName { get; set; }
public string firstName { get; set; }
public string email { get; set; }
public List<int> someAttr { get; set; }
public List<int> otherAttr { get; set; }
public List<SomeModel> modelAttr { get; set; }
public List<AnotherModel> modelAttr2 { get; set; }
}
该模型不仅有常规数据类型值,还有一些列表(数组)以及其他特定模型类型数据(SomeModel
和 AnotherModel
)。出于这个原因,我需要将该模型的对象存储到数据库中并从另一个应用程序中检索它,因为会话变量在不同 asp.net 应用程序之间来回导航时会丢失。
现在我的目标是:
Person p1 = *Retrieve data from database, and store it to p1*
我正在尝试将 p1 存储在数据库中,它的所有值都完好无损,这样当我从我的第二个 asp.net mvc 应用程序中检索它时,我可以像 p1.userName
一样使用它,p1.email
,然后是循环中的列表,如:
for(int i=0; i<p1.someAttr.Count(); i++)
{
*use i.someAttr[i] in some way*
}
我找到了各种资源来执行此操作,但使用的是图像文件。它们与我的场景不符,所以我发布了这个问题。
http://www.c-sharpcorner.com/uploadfile/Ashush/working-with-binary-large-objects-blobs/
http://www.aspsnippets.com/Articles/Read-and-Write-BLOB-Data-to-SQL-Server-database-using-C-and-VBNet.aspx
谢谢。
您可以通过使用此 PersonRepository class 来实现此目的,它将使用 JSON.
中的序列化版本将您的对象存储在 SQL 数据库中
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace Whosebug
{
#region Models
public class Person
{
public int userID { set; get; }
public string userName { get; set; }
public string lastName { get; set; }
public string firstName { get; set; }
public string email { get; set; }
public List<int> someAttr { get; set; }
public List<int> otherAttr { get; set; }
public List<SomeModel> modelAttr { get; set; }
public List<AnotherModel> modelAttr2 { get; set; }
}
public class SomeModel
{
public int SomeProperty { get; set; }
}
public class AnotherModel
{
public string AnotherProperty { get; set; }
}
#endregion
public class PersonRepository
{
// Before you can use this repository you need to setup a Person table to store your objects
// CREATE TABLE Person (UserID int primary key, PersonObject text)
private string _dbConnectionString;
public PersonRepository(string dbConnectionString)
{
this._dbConnectionString = dbConnectionString;
}
public void WriteToDatabase(Person p)
{
using (var conn = new SqlConnection(_dbConnectionString))
{
conn.Open();
using (var command = conn.CreateCommand())
{
// Serialize the person object to JSON to store in the database
var personJson = JsonConvert.SerializeObject(p);
command.CommandText = "INSERT INTO Person (UserID, PersonObject) values (@UserId, @PersonObject)";
command.Parameters.Add(new SqlParameter("@UserID", p.userID));
command.Parameters.Add(new SqlParameter("@PersonObject", personJson));
// Execute the SQL command to insert the record
command.ExecuteNonQuery();
}
}
}
public Person ReadFromDatabase(int userId)
{
using (var conn = new SqlConnection(_dbConnectionString))
{
conn.Open();
using (var command = conn.CreateCommand())
{
command.CommandText = "SELECT PersonObject from Person where UserID = @UserID";
command.Parameters.AddWithValue("@UserID", userId);
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
// Read out the JSON respresentation of the Person object
var personJson = reader.GetString(0);
// Deserialize it back into a Person object. Note you will have to deal with versioning issues.
return JsonConvert.DeserializeObject<Person>(personJson);
}
else
throw new ApplicationException($"No person found with user ID {userId}");
}
}
}
}
}
}
简而言之,您可以将它们保存为 Json 或 Xml 格式,甚至可以序列化为二进制 Json/Xml 但是处理 CRUD 操作并不简单。
但是
BLOB 是像 image/audio 一样存储在数据库中的二进制数据类型,目前,它们以文件流的形式保存在 MS SQL 服务器中。
List someAttr、List modelAttr 等数据被视为集合,可以通过它们的外键访问(不能被视为 Blob)
在 Entity Framework 中,您可以使用 "Include" 检索它们,例如:
var modelAttr = MyEntitiy.Person.Include(r=>r.modelAttr)
在 OData 中,您可以通过 "Expand" 关键字获得
因此,可以在许多 ORM 框架、OData 和 Rest 服务的 CRUD 操作中轻松处理将集合保存在单独的实体中。
无论我在哪里问这个关于 C# 的问题,我都只得到图像和文件的答案。我想将对象作为 BLOB 而不是图像从一个 asp.net 应用程序存储到数据库中,然后从另一个应用程序检索它。
假设我有一个模型 Person
:
public class Person
{
public int userID { set; get; }
public string userName { get; set; }
public string lastName { get; set; }
public string firstName { get; set; }
public string email { get; set; }
public List<int> someAttr { get; set; }
public List<int> otherAttr { get; set; }
public List<SomeModel> modelAttr { get; set; }
public List<AnotherModel> modelAttr2 { get; set; }
}
该模型不仅有常规数据类型值,还有一些列表(数组)以及其他特定模型类型数据(SomeModel
和 AnotherModel
)。出于这个原因,我需要将该模型的对象存储到数据库中并从另一个应用程序中检索它,因为会话变量在不同 asp.net 应用程序之间来回导航时会丢失。
现在我的目标是:
Person p1 = *Retrieve data from database, and store it to p1*
我正在尝试将 p1 存储在数据库中,它的所有值都完好无损,这样当我从我的第二个 asp.net mvc 应用程序中检索它时,我可以像 p1.userName
一样使用它,p1.email
,然后是循环中的列表,如:
for(int i=0; i<p1.someAttr.Count(); i++)
{
*use i.someAttr[i] in some way*
}
我找到了各种资源来执行此操作,但使用的是图像文件。它们与我的场景不符,所以我发布了这个问题。
http://www.c-sharpcorner.com/uploadfile/Ashush/working-with-binary-large-objects-blobs/ http://www.aspsnippets.com/Articles/Read-and-Write-BLOB-Data-to-SQL-Server-database-using-C-and-VBNet.aspx
谢谢。
您可以通过使用此 PersonRepository class 来实现此目的,它将使用 JSON.
中的序列化版本将您的对象存储在 SQL 数据库中using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace Whosebug
{
#region Models
public class Person
{
public int userID { set; get; }
public string userName { get; set; }
public string lastName { get; set; }
public string firstName { get; set; }
public string email { get; set; }
public List<int> someAttr { get; set; }
public List<int> otherAttr { get; set; }
public List<SomeModel> modelAttr { get; set; }
public List<AnotherModel> modelAttr2 { get; set; }
}
public class SomeModel
{
public int SomeProperty { get; set; }
}
public class AnotherModel
{
public string AnotherProperty { get; set; }
}
#endregion
public class PersonRepository
{
// Before you can use this repository you need to setup a Person table to store your objects
// CREATE TABLE Person (UserID int primary key, PersonObject text)
private string _dbConnectionString;
public PersonRepository(string dbConnectionString)
{
this._dbConnectionString = dbConnectionString;
}
public void WriteToDatabase(Person p)
{
using (var conn = new SqlConnection(_dbConnectionString))
{
conn.Open();
using (var command = conn.CreateCommand())
{
// Serialize the person object to JSON to store in the database
var personJson = JsonConvert.SerializeObject(p);
command.CommandText = "INSERT INTO Person (UserID, PersonObject) values (@UserId, @PersonObject)";
command.Parameters.Add(new SqlParameter("@UserID", p.userID));
command.Parameters.Add(new SqlParameter("@PersonObject", personJson));
// Execute the SQL command to insert the record
command.ExecuteNonQuery();
}
}
}
public Person ReadFromDatabase(int userId)
{
using (var conn = new SqlConnection(_dbConnectionString))
{
conn.Open();
using (var command = conn.CreateCommand())
{
command.CommandText = "SELECT PersonObject from Person where UserID = @UserID";
command.Parameters.AddWithValue("@UserID", userId);
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
// Read out the JSON respresentation of the Person object
var personJson = reader.GetString(0);
// Deserialize it back into a Person object. Note you will have to deal with versioning issues.
return JsonConvert.DeserializeObject<Person>(personJson);
}
else
throw new ApplicationException($"No person found with user ID {userId}");
}
}
}
}
}
}
简而言之,您可以将它们保存为 Json 或 Xml 格式,甚至可以序列化为二进制 Json/Xml 但是处理 CRUD 操作并不简单。
但是
BLOB 是像 image/audio 一样存储在数据库中的二进制数据类型,目前,它们以文件流的形式保存在 MS SQL 服务器中。
List someAttr、List modelAttr 等数据被视为集合,可以通过它们的外键访问(不能被视为 Blob) 在 Entity Framework 中,您可以使用 "Include" 检索它们,例如:
var modelAttr = MyEntitiy.Person.Include(r=>r.modelAttr)
在 OData 中,您可以通过 "Expand" 关键字获得 因此,可以在许多 ORM 框架、OData 和 Rest 服务的 CRUD 操作中轻松处理将集合保存在单独的实体中。