1

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
uniqueSolutions
  • 143
  • 2
  • 8
  • 1
    `but none of them is working.` What specifically isn't working? `First of all, I got the problem in` What problem specifically? `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.` What line of code threw that exception? `Now there is no error but there is also no file in the specified folder.` What specified folder? – mjwills Aug 27 '18 at 23:48
  • I'd suggest ensuring that `varPathToNewLocation` contains a **fully qualified path**. – mjwills Aug 27 '18 at 23:51
  • @mjwills I posted two methods which are almost same i got them from two different sources and both of them are not working. – uniqueSolutions Aug 27 '18 at 23:54
  • access denied: Access to writing the file in any folder was denied by using path something like this @"D:\a\b\c" So right now i am saving the file in a folder which is present in the bin folder of project i am specifying it like this @"here" – uniqueSolutions Aug 27 '18 at 23:56
  • 1
    Doesn't sound like a sql problem - purely a stream writer/access/path problem. Think the title of the question is wrong. – TomC Aug 28 '18 at 00:00
  • 1
    @mjwills Thanks you were right it's the problem of the path. I solved it :) – uniqueSolutions Aug 28 '18 at 00:16

1 Answers1

1

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

databaseFileRead("1",@"here\filename");
uniqueSolutions
  • 143
  • 2
  • 8