3

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.

I am trying the method to access Filestream data outlined on this page: Create Client Applications for FILESTREAM Data

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

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


Also, 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?

2 Answers 2

3

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.

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

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

Install SQLClient nuget.

Install-Package Microsoft.Data.SqlClient -Version 1.0.19249.1

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();

        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

I am afraid SqlFileStream is not supported on .NET Core. Because of that, I'm still using ASP.NET Core against Full .NET Framework instead of .NET Core.

If you'd like to see progress of this subject here are links:

https://github.com/dotnet/corefx/issues/15652

and more general Linux related:

https://github.com/Microsoft/mssql-docker/issues/47

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.