sql – 在数据库中存储不同图像的最佳方式是什么?
用于存储不同目的图像的最佳方式(数据库设计)是什么?
我有一堆用户的照片,另有5套不同的照片(如用户照片,但没有连接到用户的照片). 将所有照片存储在单个数据库表中是最好的事情,并尝试从该表中引用它们,或者最适合为每组照片创建不同的表格? 我可以看到创建多个表的一个好处,这是删除主对象时删除照片的级联删除功能. 任何其他方面要考虑? 另一个例子可能是地址.用户可以拥有地址,但公司或位置也可以. 解决方法如何在sql server中存储大的blob在SQL Server中存储大量二进制数据不是一个好办法.它使您的数据库非常庞大的备份和性能一般不是很大.存储文件通常在文件系统上完成. Sql Server 2008具有对FILESTREAM的即时支持. >正在存储的对象平均大于1 MB. 在你的情况下,我认为所有的积分都是有效的. 在服务器上启用 要在服务器上启用FILESTREAM支持,请使用以下语句. EXEC sp_configure filestream_access_level,2 RECONFIGURE 配置数据库 获取一个与您的数据库链接的filestream文件组创建 ALTER DATABASE ImageDB ADD FILEGROUP ImageGroup CONTAINS FILESTREAM ALTER DATABASE ImageDB ADD FILE ( NAME = 'ImageStream',FILENAME = 'C:DataImagesImageStream.ndf') TO FILEGROUP TodaysPhotoShoot 创建表 下一步是使用filestream存储将您的数据存储在数据库中: CREATE TABLE Images ( [Id] [uniqueidentifier] ROWGUIDCOL NOT NULL PRIMARY KEY,[CreationDate] DATETIME NOT NULL,[ImageFile] VARBINARY(MAX) FILESTREAM NULL ) 对于Filestream工作,您不仅需要表中字段上的FILESTREAM属性,还需要具有ROWGUIDCOL属性的字段. 用TSQL插入数据 现在要在这个表中插入数据,你可以使用TSQL: using(var conn = new SqlConnection(connString)) using(var cmd = new SqlCommand("INSERT INTO Images VALUES (@id,@date,cast(@image as varbinary(max))",conn)) { cmd.Parameters.AddRange(new { new SqlParameter("id",SqlDbType.UniqueIdentifier).Value = uId,new SqlParameter("date",SqlDbType.DateTime).Value = creationDate,new SqlParameter("image",SqlDbType.varbinary).Value = imageFile,}); conn.Open cmd.ExecuteScalar(); } 使用SqlFileStream插入数据 还有一种使用Win32直接获取磁盘上的文件数据的方法.这为您提供流式访问SqlFileStream从IO.Stream继承. 使用win32插入数据可以通过以下代码来完成: public void InsertImage(string connString,Guid uId,DateTime creationDate,byte[] fileContent) { using (var conn = new SqlConnection(connString)) using (var cmd = new SqlCommand(@"INSERT INTO Images VALUES (@id,cast(@image as varbinary(max)) output INSERTED.Image.PathName()",conn)) { conn.Open(); using (var transaction = conn.BeginTransaction()) { cmd.Transaction = transaction; cmd.Parameters.AddRange( new[] { new SqlParameter("id",SqlDbType.VarBinary).Value = null } ); var path = (string)cmd.ExecuteScalar(); cmd.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()"; var context = (byte[])cmd.ExecuteScalar(); using (var stream = new SqlFileStream(path,context,FileAccess.ReadWrite)) { stream.Write(fileContent,fileContent.Length); } transaction.Commit(); } } 如何建模照片存储数据库 使用filestream方法存储图像,表格非常窄,这对于性能有好处,因为每个8K数据页可以存储许多记录.我会使用以下型号: CREATE TABLE Images ( Id uniqueidentifier ROWGUIDCOL NOT NULL PRIMARY KEY,ImageSet INTEGER NOT NULL REFERENCES ImageSets,ImageFile VARBINARY(MAX) FILESTREAM NULL ) CREATE TABLE ImageSets ( ImageSet INTEGER NOT NULL PRIMARY KEY,SetName nvarchar(500) NOT NULL,Author INTEGER NOT NULL REFERENCES Users(USerId) ) CREATE TABLE Users ( UserId integer not null primary key,UserName nvarchar(500),AddressId integer not null REFERENCES Addresses ) CREATE TABLE Organsations ( OrganisationId integer not null primary key OrganisationName nvarchar(500),AddressId integer not null REFERENCES Addresses ) CREATE TABLE Addresses ( AddressId integer not null primary key,Type nvarchar(10),Street nvarchar(500),ZipCode nvarchar(50),City nvarchar(500),) CREATE TABLE OrganisationMembers ( OrganisationId integer not null REFERENCES Organisations,UserId integer not null REFERENCES Users,PRIMARY KEY (UserId,OrganisationId) ) CREATE NONCLUSTERED INDEX ixOrganisationMembers on OrganisationMembers(OrganisationId) 这转换为以下实体关系图: >性能方面,窄图表非常好,因为它每个记录只包含几个字节的数据. 参考文献: > Enable and configure Filestream (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |