private static bool WriteFileDataToDb(Guid fileid, string filename, System.IO.Stream stream, long length) { bool success = true; using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(Config.ConnectionString)) { SqlCommand command = null; try { connection.Open(); command = connection.CreateCommand(); command.CommandTimeout = Config.SQLTimeout; command.CommandText = "UPDATE [File] SET FileData.WRITE(@Bytes,@Index,@Length) WHERE FileId = @Id"; SqlParameter param_bytes = command.CreateParameter(); SqlParameter param_index = command.CreateParameter(); SqlParameter param_length = command.CreateParameter(); SqlParameter param_id = command.CreateParameter(); param_bytes.DbType = DbType.Binary; param_bytes.ParameterName = "@Bytes"; param_index.ParameterName = "@Index"; param_length.ParameterName = "@Length"; param_id.ParameterName = "@Id"; command.Parameters.Add(param_bytes); command.Parameters.Add(param_index); command.Parameters.Add(param_length); command.Parameters.Add(param_id); int bufflength = 1024; int index = 0; byte[] buff = new byte[bufflength]; using (System.IO.BinaryReader br = new System.IO.BinaryReader(stream, Encoding.GetEncoding(Config.Encode))) { param_id.Value = fileid; int offset = 0; while ((buff = br.ReadBytes(buff.Length)).Length > 0) { offset = index * bufflength; param_bytes.Value = buff; param_index.Value = index * bufflength; param_length.Value = buff.Length; try { command.ExecuteNonQuery(); } catch (Exception ex) { var msg = ex.Message; var stack = ex.StackTrace; success = false; break; } index++; } } } catch (Exception ex) { success = false; } finally { if (command != null) command.Dispose(); if (connection.State == ConnectionState.Open) connection.Close(); if (connection != null) connection.Dispose(); } } return success; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)