在.Net Core 2中使用SQL Server Filestream

编程入门 行业动态 更新时间:2024-10-14 20:21:21
本文介绍了在.Net Core 2中使用SQL Server Filestream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要访问存储在SQL Server中的pdf文件作为Filestream数据.我正在开发.Net Core 2(Razor页面)应用程序.

I need to access pdf files stored in a SQL Server as Filestream data. I am working on a .Net Core 2 (Razor pages) application.

我正在尝试访问此页面上概述的Filestream数据的方法: docs.microsoft/zh-CN/sql/relational-databases/blob/create-client-applications-for-filestream-data

I am trying the method to access Filestream data outlined on this page: docs.microsoft/en-us/sql/relational-databases/blob/create-client-applications-for-filestream-data

但是,SqlFileStream类型似乎在System.Data.SqlTypes的.Net Core 2版本中不可用.

However, it appears that the SqlFileStream type is not available in the .Net Core 2 version of System.Data.SqlTypes.

从SQL Server .Net Core 2访问Filestream数据的最佳方法是什么?

What is the best way to access Filestream data from SQL Server .Net Core 2?

还有一个相关的问题,是否有一种方法可以以更简化"的方式使用Linq和Entity Framework,而不必通过设置SqlCommand,SqlConnection来设置经典" SQL查询等等

And, related question, is there an approach that can make use of Linq and Entity Framework in a more "streamlined" manner, rather than having to set up a "classic" SQL query with setting SqlCommand, SqlConnection, etc.

推荐答案

.NET Core 3中的 Microsoft.Data.SqlClient .您可以尝试预览,但官方版本即将发布.

This will be available in .NET Core 3 as part of Microsoft.Data.SqlClient. You can try the preview, but the official release is right around the corner.

以前曾在这里进行过跟踪: github/dotnet/SqlClient/issues /15

It was previously tracked here: github/dotnet/SqlClient/issues/15

首先创建数据库.可以找到一个很好的例子在这里.

Start by creating your database. A good example can be found here.

安装SQLClient nuget.

Install SQLClient nuget.

Install-Package Microsoft.Data.SqlClient -Version 1.0.19249.1

请注意,不再使用System.Data.新的名称空间是Microsoft.Data.

Please note that System.Data is not used anymore. The new namespace is Microsoft.Data.

这是一个简单的应用程序(来自上述示例):

Here's an simple app (from above mentioned example):

class Program { const string cs =@"Data Source=<your server>;Initial Catalog=MyFsDb;Integrated Security=TRUE"; static void Main(string[] args) { Save(); Open(); } private static void Save() { var path = @"C:\Files1\testfile.txt"; FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader rdr = new BinaryReader(fs); byte[] fileData = rdr.ReadBytes((int)fs.Length); rdr.Close(); fs.Close(); using (SqlConnection con = new SqlConnection(cs)) { con.Open(); string sql = "INSERT INTO MyFsTable VALUES (@fData, @fName, default)"; SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.Add("@fData", SqlDbType.Image, fileData.Length).Value = fileData; cmd.Parameters.Add("@fName", SqlDbType.NVarChar).Value = "Some Name"; cmd.ExecuteNonQuery(); con.Close(); } } private static void Open() { using (SqlConnection con = new SqlConnection(cs)) { con.Open(); SqlTransaction txn = con.BeginTransaction(); string sql = "SELECT fData.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT(), fName FROM MyFsTable"; SqlCommand cmd = new SqlCommand(sql, con, txn); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { string filePath = rdr[0].ToString(); byte[] objContext = (byte[])rdr[1]; SqlFileStream sfs = new SqlFileStream(filePath, objContext, System.IO.FileAccess.Read); byte[] buffer = new byte[(int)sfs.Length]; sfs.Read(buffer, 0, buffer.Length); sfs.Close(); string fileContents = System.Text.Encoding.UTF8.GetString(buffer); Console.WriteLine(fileContents); } rdr.Close(); txn.Commit(); con.Close(); } } }

更多推荐

在.Net Core 2中使用SQL Server Filestream

本文发布于:2023-11-16 15:20:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Core   Net   SQL   Filestream   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!