{"id":2266,"date":"2020-02-18T04:28:13","date_gmt":"2020-02-18T01:28:13","guid":{"rendered":"http:\/\/kusuaks7\/?p=1871"},"modified":"2024-01-08T20:22:00","modified_gmt":"2024-01-08T20:22:00","slug":"how-to-use-sql-server-filestream-to-store-unstructured-data","status":"publish","type":"post","link":"https:\/\/www.experfy.com\/blog\/bigdata-cloud\/how-to-use-sql-server-filestream-to-store-unstructured-data\/","title":{"rendered":"How to Use SQL Server FILESTREAM to Store Unstructured Data"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"2266\" class=\"elementor elementor-2266\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"has_eae_slider elementor-section elementor-top-section elementor-element elementor-element-312ce12 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"312ce12\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"has_eae_slider elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-1a9abbb8\" data-id=\"1a9abbb8\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-7db43a03 elementor-widget elementor-widget-text-editor\" data-id=\"7db43a03\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tIn this post, I decided to explain to you how to use SQL Server FILESTREAM to store unstructured data.\u00a0Here you can also read what are the positive and negative sides of FILESTREAM. Here are some other SQL queries to help you use it.\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7d7377a elementor-widget elementor-widget-text-editor\" data-id=\"7d7377a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tIn earlier versions of SQL Server, storage of unstructured data posed many challenges in terms of maintaining consistency between structured and unstructured data, managing backup\/restore procedure, performance issues, scalability, etc.\u2026\u00a0In earlier versions of MSSQL before SQL Server 2008, there were various mechanisms for storing unstructured data.\u00a0This information is usually stored in the form of files in a shared folder for which certain users are granted access rights.\u00a0The UNC path of these files is usually stored in the database as a column in the table (varchar (n)) so that application logic can access the specific files. The problem was the security of the files, administering access to them and maintaining them.\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9518bf6 elementor-widget elementor-widget-text-editor\" data-id=\"9518bf6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tThe later concept of Binary Large Objects (BLOB) helped to store unstructured data to some degree.\u00a0The main advantage of this concept is the integrated management and transactional consistency within the database. Security issues (the previous file solution) were resolved in this case.\u00a0There were problems though, which is a 2Gb limit and log file overload.\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a823ec8 elementor-widget elementor-widget-text-editor\" data-id=\"a823ec8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\nTo address these issues, SQL Server 2008 introduced for the first time an enhancement called FILESTREAM.\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e7e335b elementor-widget elementor-widget-text-editor\" data-id=\"e7e335b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tIt is not a data type such as VARBINARY (MAX) but an attribute\/property set via the VARBINARY column to indicate that the data should be stored directly on the file system.\u00a0Such data remains an integral part of the system and maintains transactional consistency.\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-acc6cbc elementor-widget elementor-widget-text-editor\" data-id=\"acc6cbc\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\nSome of the features of FILESTREAM are:\n<ul>\n \t<li>Binary data is stored as individual files outside the database,<\/li>\n \t<li>These individual files can be accessed through the WIN32 API for file operations,<\/li>\n \t<li>T-SQL statements are applicable,<\/li>\n \t<li>The 2GB file size limit for VARBINARY (MAX) columns has been removed for objects stored in the file system through FILESTREAM,<\/li>\n \t<li>FILESTREAM can also be used on compressed folders, disks, volumes.<\/li>\n<\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-bdecbde elementor-widget elementor-widget-text-editor\" data-id=\"bdecbde\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p style=\"margin-left: 11.25pt;\">In order to use FILESTREAM, it must be enabled.\u00a0FILESTREAM is enabled on two levels.\u00a0The first level is the SQL Server level.\u00a0This level was enabled when installing SQL Server.\u00a0If this is not done then enable can be done through SQL Server Configuration Manager by checking the checkboxes on the SQL Server FILESTREAM tab of Properties (Figure 1).<\/p>\nThe second level of enabling FILESTREAM is at the SQL Server Instance level. This enable is done by executing the T-SQL script provided in script 1.\n<div>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3aeade5 elementor-widget elementor-widget-text-editor\" data-id=\"3aeade5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre style=\"line-height: 15.0pt; background: white; border: none; mso-border-alt: solid #EAE9E9 .75pt; padding: 0cm; mso-padding-alt: 16.0pt 14.0pt 14.0pt 14.0pt; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-align: start; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; overflow: hidden; word-spacing: 0px;\" contenteditable=\"false\"><span style=\"font-size: 9.0pt; color: #080e14;\">EXEC sp_configure filestream_access_level<span class=\"token\">,<\/span> <span class=\"token\">2<\/span><\/span><\/pre>\n<pre>RECONFIGURE<\/pre>\n<\/div>\n<strong>Script 1. Enable FILESTREAM at Server Instance level<\/strong>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0174e71 elementor-widget elementor-widget-text-editor\" data-id=\"0174e71\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\nTable 1 lists the possible values for filestream_access_level.\n<table style=\"width: 584px;\" border=\"1\" width=\"584\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td style=\"width: 55px;\">Value<\/td>\n<td style=\"width: 564px;\">Description<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 55px;\">0<\/td>\n<td style=\"width: 564px;\">Disables FILESTREAM support for this instance.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 55px;\">1<\/td>\n<td style=\"width: 564px;\">Enables FILESTREAM for Transact-SQL access.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 55px;\">\u00a02<\/td>\n<td style=\"width: 564px;\">Enables FILESTREAM for Transact-SQL and Win32 streaming access.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Table 1. Possible values for filestream_access_level<\/strong>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8308f5e elementor-widget elementor-widget-text-editor\" data-id=\"8308f5e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tThe next step is to create a database where it is possible to create tables in which FILESTREAM will be used. An example of a T-SQL script for creating a database where FILESTREAM can be used is given in script 2.\n<div>\n<pre style=\"line-height: 15.0pt; background: white; border: none; mso-border-alt: solid #EAE9E9 .75pt; padding: 0cm; mso-padding-alt: 16.0pt 14.0pt 14.0pt 14.0pt; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-align: start; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; overflow: hidden; word-spacing: 0px; margin: 10.5pt 0cm 10.5pt 0cm;\" contenteditable=\"false\"><span style=\"font-size: 9.0pt; color: #080e14;\">CREATE DATABASE Test_FILESTREAM<\/span><\/pre>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">ON<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">PRIMARY(<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">NAME=Test_FILESTREAM_Prmary,<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">FILENAME=&#8217;d:sqlbaza2019mssqldataFILESTREAMTest_FILESTREAM.mdf&#8217;),<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">FILEGROUP FileStreamGroup CONTAINS FILESTREAM(<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">NAME=Test_FILESTREAM_FM,<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">FILENAME=&#8217;d:sqlbaza2019mssqldataFILESTREAMTest_FILESTREAM_FM&#8217;)<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">LOG ON (<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">NAME=Test_FILESTREAM_Log,<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">FILENAME=&#8217;d:sqlbaza2019mssqldataFILESTREAMTest_FILESTREAM.log&#8217;)<\/span><\/div>\n<\/div>\n&nbsp;\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-797e15e elementor-widget elementor-widget-text-editor\" data-id=\"797e15e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<strong>Script 2. An example of creating a database where it will be possible to use FILESTREAM<\/strong>\n\nExecuting script 2 on the file system will create a $ FSLOG folder and file filestream.hdr. The filestream.hdr file is a header file for the FILESTREAM container.\n\nThe next step is to create a table with column VARBINARY (MAX) with the FILESTREAM attribute (script 3).\n<div>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fc11190 elementor-widget elementor-widget-text-editor\" data-id=\"fc11190\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div>\n<pre style=\"line-height: 15.0pt; background: white; border: none; mso-border-alt: solid #EAE9E9 .75pt; padding: 0cm; mso-padding-alt: 16.0pt 14.0pt 14.0pt 14.0pt; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-align: start; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; overflow: hidden; word-spacing: 0px; margin: 10.5pt 0cm 10.5pt 0cm;\" contenteditable=\"false\"><span style=\"font-size: 9.0pt; color: #080e14;\">USE Test_FILESTREAM<\/span><\/pre>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\">CREATE TABLE [dbo].[Fajlovi]<\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\">(<\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\">ID UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL PRIMARY KEY,<\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\">Fajl VARBINARY(MAX) FILESTREAM DEFAULT NULL<\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\">)<\/div>\n<\/div>\n&nbsp;\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1c3a7f4 elementor-widget elementor-widget-text-editor\" data-id=\"1c3a7f4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<strong>Script 3. Creating a table with a VARBINARY (MAX) FILESTREAM column<\/strong>\n\nIt remains to insert the record into the newly created table (script 4).\n<div>\n<pre style=\"line-height: 15.0pt; background: white; border: none; mso-border-alt: solid #EAE9E9 .75pt; padding: 0cm; mso-padding-alt: 16.0pt 14.0pt 14.0pt 14.0pt; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-align: start; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; overflow: hidden; word-spacing: 0px; margin: 10.5pt 0cm 10.5pt 0cm;\" contenteditable=\"false\"><span style=\"font-size: 9.0pt; color: #080e14;\">INSERT INTO Fajlovi<\/span><\/pre>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\">VALUES (newid(), cast(&#8216;My test FIESTREAM-a&#8217; as varbinary(max)))<\/div>\n<\/div>\n&nbsp;\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b5ecfca elementor-widget elementor-widget-text-editor\" data-id=\"b5ecfca\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<strong>Script 4. Inserting a record into a table with a VARBINARY (MAX) FILESTREAM column<\/strong>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-894a0c8 elementor-widget elementor-widget-text-editor\" data-id=\"894a0c8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tInserting a record with script 4 will also create a new folder on the file system.\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7f93937 elementor-widget elementor-widget-text-editor\" data-id=\"7f93937\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tRecords from the table with the VARBINARY (MAX) FILESTREAM column can be obtained by executing script 5.\n<div>\n<pre style=\"line-height: 15.0pt; background: white; border: none; mso-border-alt: solid #EAE9E9 .75pt; padding: 0cm; mso-padding-alt: 16.0pt 14.0pt 14.0pt 14.0pt; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-align: start; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; overflow: hidden; word-spacing: 0px;\" contenteditable=\"false\"><span class=\"token\"><span style=\"font-size: 9.0pt; color: #080e14;\">select<\/span><\/span><span style=\"font-size: 9.0pt; color: #080e14;\"> <span class=\"token\">*<\/span><\/span><\/pre>\n<pre>from [Test_FILESTREAM].[dbo].[Fajlovi]<\/pre>\n<\/div>\n<strong>Script 5. Viewing records from a table with a VARBINARY (MAX) FILESTREAM column<\/strong>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5a12858 elementor-widget elementor-widget-text-editor\" data-id=\"5a12858\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tThe result of executing script 5 is given in Figure 2.\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-997811f elementor-widget elementor-widget-text-editor\" data-id=\"997811f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tEach cell in the FILESTREAM column is a file path on the file system associated with it. To read the path, it is necessary to use the PathName property of the varbinary (max) column in the T-SQL statement. Script 6 provides an example of how to read the varbinary (max) column file path.\n<div>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9ef22ab elementor-widget elementor-widget-text-editor\" data-id=\"9ef22ab\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre style=\"line-height: 15.0pt; background: white; border: none; mso-border-alt: solid #EAE9E9 .75pt; padding: 0cm; mso-padding-alt: 16.0pt 14.0pt 14.0pt 14.0pt; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-align: start; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; overflow: hidden; word-spacing: 0px;\" contenteditable=\"false\"><span style=\"font-size: 9.0pt; color: #080e14;\">DECLARE @filePath <span class=\"token\">varchar(<\/span>max<span class=\"token\">)<\/span><\/span><\/pre>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">SELECT @filePath = Fajl.PathName()<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">FROM dbo.Fajlovi<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">WHERE ID = &#8216;F9A149D0-F5F0-4FC5-9F59-1D27E4D10C1D&#8217;<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">PRINT @filepath<\/span><\/div>\n<\/div>\n&nbsp;\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b602ed6 elementor-widget elementor-widget-text-editor\" data-id=\"b602ed6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<strong>Script 6. Example of reading a FILESTREAM file path on a file system<\/strong>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c83627c elementor-widget elementor-widget-text-editor\" data-id=\"c83627c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tT-SQL can be used to work with FILESTREAM data, but it\u2019s a more natural MS Visual Studio environment.\u00a0The use of FILESTREAM functionality, in application logic, is made possible by implementing the System.Data.SqlTypes.SqlFileStream class.\u00a0To maintain consistency of data, each SQL FILESTREAM operation must be part of a transaction.\u00a0MARS (Multiple Active Result Sets) connections have special rules for batch transactions, which the T-SQL BEGIN TRANSACTION statement violates.\u00a0To avoid this problem, the application client should use the appropriate transaction management API to mean the class System.Data.SqlClient.SqlTransaction.\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5e02724 elementor-widget elementor-widget-text-editor\" data-id=\"5e02724\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tTo allow transactional access to a FILESTREAM data file system, it is necessary to use the T-SQL function GET_FILESTREAM_TRASACTION_CONTEXT () to provide a token that represents the current transaction within a particular session (C # code 1).\n<div>\n<pre style=\"line-height: 15.0pt; background: white; border: none; mso-border-alt: solid #EAE9E9 .75pt; padding: 0cm; mso-padding-alt: 16.0pt 14.0pt 14.0pt 14.0pt; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-align: start; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; overflow: hidden; word-spacing: 0px;\" contenteditable=\"false\"><span style=\"font-size: 9.0pt; color: #080e14;\">SqlConnection sqlConnection <span class=\"token\">=<\/span> <span class=\"token\">new<\/span> <span class=\"token\">SqlConnection(<\/span><\/span><\/pre>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">&#8220;Integrated Security=true;server=(local)&#8221;);<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">SqlCommand sqlCommand = new SqlCommand();<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">sqlCommand.Connection = sqlConnection;<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">SqlTransaction transaction = sqlConnection.BeginTransaction(&#8220;mainTranaction&#8221;);<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">sqlCommand.Transaction = transaction;<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">sqlCommand.CommandText =<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">&#8220;SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()&#8221;;<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">Object obj = sqlCommand.ExecuteScalar();<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">byte[] txContext = (byte[])obj;<\/span><\/div>\n<\/div>\n&nbsp;\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ba83805 elementor-widget elementor-widget-text-editor\" data-id=\"ba83805\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<strong>C # code 1. Using the GET_FILESTREAM_TRASACTION_CONTEXT () function<\/strong>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b93834b elementor-widget elementor-widget-text-editor\" data-id=\"b93834b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tThis way the transaction is initiated but not yet committed or rollbacked. Multiple operations with FILESTREAM data can be performed in one transaction. An example of a data entry code using the Win32 API is given in C # code 2 in which a sqlFileStream object is initially created based on the System.Data.SqlTypes.SqlFileStream class.\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fd5a1df elementor-widget elementor-widget-text-editor\" data-id=\"fd5a1df\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div>\n<pre style=\"line-height: 15.0pt; background: white; border: none; mso-border-alt: solid #EAE9E9 .75pt; padding: 0cm; mso-padding-alt: 16.0pt 14.0pt 14.0pt 14.0pt; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-align: start; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; overflow: hidden; word-spacing: 0px;\" contenteditable=\"false\"><span style=\"font-size: 9.0pt; color: #080e14;\">SqlFileStream sqlFileStream <span class=\"token\">=<\/span> <span class=\"token\">new<\/span> <span class=\"token\">SqlFileStream(<\/span>filePath<span class=\"token\">,<\/span> txContext<span class=\"token\">,<\/span> FileAccess<span class=\"token\">.<\/span>ReadWrite<span class=\"token\">);<\/span><\/span><\/pre>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">byte[] buffer = new byte[512];<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">int numBytes = 0;<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\"><span spellcheck=\"true\">\/\/Write string, &#8220;FILESTREAM test data&#8221; in FILESTREAM.<\/span><\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">string someData = &#8220;FILESTREAM test data&#8221;;<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">Encoding unicode = Encoding.GetEncoding(0);<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">sqlFileStream.Write(unicode.GetBytes(someData.ToCharArray()),0,someData.Length);<\/span><\/div>\n<\/div>\n&nbsp;\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3381c6e elementor-widget elementor-widget-text-editor\" data-id=\"3381c6e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<strong>C # code 2. Example code for entering data in FILESTREAM<\/strong>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5b5d7d2 elementor-widget elementor-widget-text-editor\" data-id=\"5b5d7d2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tAn example code to read FILESTREAM data, using the Win32 API, is given in C # code 3. The same sqlFileStream object that was created at the beginning of the C # code 1 example is used.\n<div>\n<pre style=\"line-height: 15.0pt; background: white; border: none; mso-border-alt: solid #EAE9E9 .75pt; padding: 0cm; mso-padding-alt: 16.0pt 14.0pt 14.0pt 14.0pt; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-align: start; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; overflow: hidden; word-spacing: 0px;\" contenteditable=\"false\"><span style=\"font-size: 9.0pt; color: #080e14;\">sqlFileStream<span class=\"token\">.Seek(<\/span>0L<span class=\"token\">,<\/span> SeekOrigin<span class=\"token\">.<\/span>Begin<span class=\"token\">);<\/span><\/span><\/pre>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">numBytes = sqlFileStream.Read(buffer, 0, buffer.Length);<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">string readData = unicode.GetString(buffer);<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">if (numBytes != 0)<\/span><\/div>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">Console.WriteLine(readData);<\/span><\/div>\n<\/div>\n&nbsp;\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-909bc3f elementor-widget elementor-widget-text-editor\" data-id=\"909bc3f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<strong>C # code 3. Example code to read FILESTREAM data<\/strong>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-69d7294 elementor-widget elementor-widget-text-editor\" data-id=\"69d7294\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tUpon completion of FILESTREAM enrollment transactions (C # code 1) and reading FILESTREAM data (C # code 2), the sqlFileStream object (created at the beginning of the C # code 1 example based on the System.Data.SqlTypes.SqlFileStream class) must be closed and commit transaction (C # code 4). The sqlCommand object was created at the beginning of example C # code 1.\n<div>\n<pre style=\"line-height: 15.0pt; background: white; border: none; mso-border-alt: solid #EAE9E9 .75pt; padding: 0cm; mso-padding-alt: 16.0pt 14.0pt 14.0pt 14.0pt; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-align: start; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; overflow: hidden; word-spacing: 0px;\" contenteditable=\"false\"><span style=\"font-size: 9.0pt; color: #080e14;\">sqlFileStream<span class=\"token\">.Close();<\/span><\/span><\/pre>\n<div style=\"background: #eee; border: 1px solid #ccc; padding: 5px 10px;\"><span style=\"font-family: courier new,courier,monospace;\">sqlCommand.Transaction.Commit();<\/span><\/div>\n<\/div>\n&nbsp;\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-712ecf1 elementor-widget elementor-widget-text-editor\" data-id=\"712ecf1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<strong>C # code 4. Close the sqlFileStream object and commit the transaction<\/strong>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d8b8d58 elementor-widget elementor-widget-text-editor\" data-id=\"d8b8d58\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tA graph of comparative values of data loading performance, when VARBINARY (MAX) and FILESTREAM are used to store unstructured data and T-SQL or Win32 is given in Figure 3 to access the data.\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3886292 elementor-widget elementor-widget-text-editor\" data-id=\"3886292\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tIt should also be noted that FILESTREAM is also used in other SQL Server technologies, such as FileTable and In-Memory OLTP.\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2e883d9 elementor-widget elementor-widget-text-editor\" data-id=\"2e883d9\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<strong>In the end:<\/strong>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5ac4229 elementor-widget elementor-widget-text-editor\" data-id=\"5ac4229\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\nLike any other technology, FILESTREAM has its advantages and disadvantages.\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4ec7607 elementor-widget elementor-widget-text-editor\" data-id=\"4ec7607\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\n<strong>Advantages:<\/strong>\n<ul>\n \t<li>Files created as part of FILESTREAM are managed by SQL Server itself in its own file groups from which backup and restore can be done together with other SQL Server data,<\/li>\n \t<li>Reading and writing these files is part of the database transaction,<\/li>\n \t<li>It is possible to store very large BLOB objects<\/li>\n<\/ul>\n<p style=\"margin-left: 11.25pt;\"><strong>Disadvantages:<\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8b6f633 elementor-widget elementor-widget-text-editor\" data-id=\"8b6f633\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<ul>\n \t<li>FILESTREAM data can only be stored on local disk volumes,<\/li>\n \t<li>Not supported in database snapshot,<\/li>\n \t<li>Not supported on database mirroring,<\/li>\n \t<li>Transparent Data Encryption is not supported,<\/li>\n \t<li>Cannot be used with table-valued parameters<\/li>\n<\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>In earlier versions of SQL Server, storage of unstructured data posed many challenges in terms of maintaining consistency between structured and unstructured data, managing backup\/restore procedure, performance issues, scalability, etc. This post explains to you how to use SQL Server FILESTREAM to store unstructured data.&nbsp;Here you can also read the positive and negative sides of FILESTREAM. Here are some other SQL queries to help you use it.<\/p>\n","protected":false},"author":685,"featured_media":23552,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-post-2.php","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[187],"tags":[95],"ppma_author":[3475],"class_list":["post-2266","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bigdata-cloud","tag-big-data-amp-technology"],"authors":[{"term_id":3475,"user_id":685,"is_guest":0,"slug":"alen-ibric","display_name":"Alen \u0130bri\u00e7","avatar_url":"https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2020\/04\/medium_7b9fc7fb-1f17-4554-9f19-b942b50e055c-150x150.jpg","user_url":"http:\/\/www.bilkent.edu.tr\/","last_name":"\u0130bri\u00e7","first_name":"Alen","job_title":"","description":"<a href=\"http:\/\/alenibric.com.tr\/\">Alen \u0130bri\u00e7<\/a> is Senior Computer Engineer at Bilkent University, Turkey."}],"_links":{"self":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/2266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/users\/685"}],"replies":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/comments?post=2266"}],"version-history":[{"count":7,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/2266\/revisions"}],"predecessor-version":[{"id":35423,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/2266\/revisions\/35423"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media\/23552"}],"wp:attachment":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media?parent=2266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/categories?post=2266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/tags?post=2266"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=2266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}