title: Convert byte[] array TO sqldbtype.Varbinary using Dapper
body: <p>I've found a code snipped on the Internet that inserts a document as a byte array in the database. It is as follows:</p>
  
  <pre><code>    public 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.slqDataConnectionDetails))
          using (SqlConnection connection = new SqlConnection(connectionString))
          using (var sqlWrite = new SqlCommand("INSERT INTO EXCEL_EM_BYTES (DOCUMENTO_BYTES) Values(@File)", connection))
          {
              sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
              connection.Open();
              sqlWrite.ExecuteNonQuery();
              connection.Close();
          }
      }
  </code></pre>
  
  <p>Now, I have to apply that to Dapper/Entity framework, but so far, without success. What I got so far is as follows:</p>
  
  <pre><code>      public void InsereRegistroEmail(string a, string b, string c, byte[] anexoBytes)
      {
          //var cn = _context.Database.Connection;
  
          //cn.Execute(string.Format(QueriesSAC.InsereRegistroEmailBanco, motivoEmail, nomeGestor, corpoEmail, anexoBytes));
  
          var cn = _context.Database.Connection;
          //var A = new SqlParameter("@A", SqlDbType.VarBinary, anexoBytes.Length);
          //A.Value = anexoBytes;
  
          var sql =(string.Format("INSERT INTO [LOG_EMAIL] ([GSTOR_DSTNA] ,[ASSNT],[DATA_ENVIO],[CORPO_EMAIL],[ANEXO]) VALUES('{0}','{1}', GETDATE(),'{2}', @A)", a, b, c));
          var A = new DynamicParameters();
          A.Add("@A", anexoBytes, dbType: DbType.Binary, direction: ParameterDirection.Input);
          A.Get&lt;DbType&gt;("@A");
          cn.Execute(sql);
      }
  </code></pre>
  
  <p>The key line here is:</p>
  
  <pre><code>         sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
  </code></pre>
  
  <p>As is the one that sets the data type to VarBinary. I really need some help here...</p>
  
  <p>The file array is anexoBytes.</p>