How to retrieve file from database?

只愿长相守 提交于 2021-01-29 04:07:57

问题


I have saved a file in a database by converting it in bytes. It is saved successfully but I am unable to retrieve it.

Here is code which I used to convert and save:

public static void databaseFilePut(string varFilePath)
{
    byte[] file;

    using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read))
    {
        using (var reader = new BinaryReader(stream))
        {
            file = reader.ReadBytes((int)stream.Length);
        }
    }

    using (SqlConnection varConnection = new SqlConnection("Data Source=FARHANB;Initial Catalog=save;Integrated Security=True"))
    using (var sqlWrite = new SqlCommand("INSERT INTO Raporty (RaportPlik) Values(@File)", varConnection))
    {
        varConnection.Open();
        sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;

        sqlWrite.ExecuteNonQuery();
        varConnection.Close();
    }
}

I am using this code to retrieve it from the database:

public static void databaseFileRead(string varID, string varPathToNewLocation)
{
    using (SqlConnection varConnection = new SqlConnection("Data Source=FARHANB;Initial Catalog=save;Integrated Security=True"))
    using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection))
    {
        varConnection.Open();
        sqlQuery.Parameters.AddWithValue("@varID", varID);

        using (var sqlQueryResult = sqlQuery.ExecuteReader())
            if (sqlQueryResult != null)
            {
                sqlQueryResult.Read();
                var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
                sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);

                using (var fs = new FileStream(varPathToNewLocation, FileMode.Create, FileAccess.Write))
                    fs.Write(blob, 0, blob.Length);

                varConnection.Close();
            }
        }
    }

OR

public static void databaseFileRead(string varID, string varPathToNewLocation)
{
    using (SqlConnection varConnection = new SqlConnection("Data Source=FARHANB;Initial Catalog=save;Integrated Security=True"))
    using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection))
    {
        varConnection.Open();
        sqlQuery.Parameters.AddWithValue("@varID", varID);

        using (var sqlQueryResult = sqlQuery.ExecuteReader())
            if (sqlQueryResult != null)
            {
                sqlQueryResult.Read();
                var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
                sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);

                using (var fs = new FileStream(varPathToNewLocation, FileMode.Create, FileAccess.Write)) 
                    fs.Write(blob, 0, blob.Length);
            }
            varConnection.Close();
    }
}

I have used both code blocks one by one but none of them is working.

Link One(Original) Link Two(Modification)

First of all, I got the problem in

using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))

So I changed it into sqlconnection. It's saving the bytes successfully so I don't think problem is here.

Second problem I got was "Access denied". I was getting access denied error so I solved it by saving the file in the bin project.

Now there is no error but there is also no file in the specified folder.

Here are the references I passed in the method.

databaseFileRead("1",@"\here");

What should I do? Please guide me I'm new to the databases.

Thank you


回答1:


It was the problem of the path. I was doing it wrong here is the solution

databaseFileRead("1",@"here\filename");


来源:https://stackoverflow.com/questions/52048287/how-to-retrieve-file-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!