Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

store file in DB

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 (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
        using (var sqlWrite = new SqlCommand("INSERT INTO Raporty (RaportPlik) Values(@File)", varConnection)) {
            sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
            sqlWrite.ExecuteNonQuery();
        }
    }
    public static void databaseFileRead(string varID, string varPathToNewLocation) {
        using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
        using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection)) {
            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);
                }
        }
    }
    public static MemoryStream databaseFileRead(string varID) {
        MemoryStream memoryStream = new MemoryStream();
        using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
        using (var sqlQuery = new SqlCommand(@"SELECT [RaportPlik] FROM [dbo].[Raporty] WHERE [RaportID] = @varID", varConnection)) {
            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 MemoryStream(memoryStream, FileMode.Create, FileAccess.Write)) {
                    memoryStream.Write(blob, 0, blob.Length);
                    //}
                }
        }
        return memoryStream;
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: my context class is in different project and i want migration in different project in asp.net mvc 
Csharp :: C# data base sql 
Csharp :: get c directory contains system windows c# 
Csharp :: asp.net core httpdelete with body 
Csharp :: how to detect when a gameobject has exited a trigger c# 
Csharp :: how to remove all controls from panel c# 
Csharp :: UPA Error 
Csharp :: IAuthorizationFilter OnAuthorization AuthorizationContext MyAuthorizeAttribute HttpUnauthorizedResult HttpContext 
Csharp :: how to make game restart when player touches a object unity 
Csharp :: Adding number of day remaining to future date from now 
Csharp :: dictionary and generic class c# 
Csharp :: c# KERNEL32.DLL recoverdeleted files 
Csharp :: c# void with nullable List argument 
Csharp :: SonarQube UnitTests 
Csharp :: c# dictionary key set 
Csharp :: c# arithmetic operators 
Csharp :: Showing a hidden WPF window 
Csharp :: C# return json data from File 
Csharp :: asp.net stop page jumping to top on click 
Csharp :: unity sprite blurry when far 
Csharp :: wpf user parent controller datacontext 
Csharp :: Find Number of Repetitions of Substring 
Csharp :: trigger enter with nav mesh 
Csharp :: my custom file watcher 
Csharp :: you have the following c# code. stringbuilder sb = new stringbuilder(really long string); the really long string variable is a string in which a very long string is stored. 
Csharp :: slider script unity 
Csharp :: soundplayer c# take uri 
Csharp :: c# if a new program is started 
Csharp :: unity insert variable into string 
Csharp :: c# change chart legend font size 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =