Ejemplo n.º 1
0
        internal static int UpdateRecord(ImageStoreFile file)
        {
            var connection = DatabaseConnection.Current;

            using (var command = new SqlCommand("Update [File] Set [ImageHash]=@ImageHash, [Sha1Hash]=@Sha1Hash, [FileSize]=@FileSize, [FileState]=@FileState, [ImageComparedThreshold]=@ImageComparedThreshold where [Id]=@Id"))
            {
                command.Connection = connection;
                command.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier)
                {
                    Value = file.Id
                });
                command.Parameters.Add(new SqlParameter("@ImageHash", System.Data.SqlDbType.Binary, 40)
                {
                    Value = DBNullableReader.NullCheck(file.ImageHash)
                });
                command.Parameters.Add(new SqlParameter("@Sha1Hash", System.Data.SqlDbType.Binary, 20)
                {
                    Value = DBNullableReader.NullCheck(file.Sha1Hash)
                });
                command.Parameters.Add(new SqlParameter("@FileSize", System.Data.SqlDbType.Int)
                {
                    Value = file.FileSize
                });
                command.Parameters.Add(new SqlParameter("@FileState", System.Data.SqlDbType.Int)
                {
                    Value = file.FileStateCode
                });
                command.Parameters.Add(new SqlParameter("@ImageComparedThreshold", System.Data.SqlDbType.Real)
                {
                    Value = file.ImageComparedThreshold
                });

                return(command.ExecuteNonQuery());
            }
        }
Ejemplo n.º 2
0
        void WriteDatabase()
        {
            var connection = DatabaseConnection.Current;

            using (var commandToDeleteSame = new SqlCommand("Delete from [SameFile] Where [FileId]=@Id"))
                using (var commandToDeleteSimilar = new SqlCommand("Delete from [SimilarFile] Where [File1Id]=@Id or [File2Id]=@Id"))
                    using (var command = new SqlCommand("Update [File] Set [ImageHash]=@ImageHash, [Sha1Hash]=@Sha1Hash, [FileSize]=@FileSize, [FileState]=@FileState, [ImageComparedThreshold]=0 where [Id]=@Id"))
                    {
                        commandToDeleteSame.Connection = connection;
                        commandToDeleteSame.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier));
                        commandToDeleteSame.CommandTimeout = 180;
                        commandToDeleteSimilar.Connection  = connection;
                        commandToDeleteSimilar.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier));
                        commandToDeleteSimilar.CommandTimeout = 180;
                        command.Connection = connection;
                        command.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier));
                        command.Parameters.Add(new SqlParameter("@ImageHash", System.Data.SqlDbType.Binary, 40));
                        command.Parameters.Add(new SqlParameter("@Sha1Hash", System.Data.SqlDbType.Binary, 20));
                        command.Parameters.Add(new SqlParameter("@FileSize", System.Data.SqlDbType.Int));
                        command.Parameters.Add(new SqlParameter("@FileState", System.Data.SqlDbType.Int));

                        Tuple <Guid, byte[], byte[], int, FileState, bool> record;

                        while (true)
                        {
                            try
                            {
                                record = toWrite.Take();

                                command.Parameters[0].Value = record.Item1;
                                command.Parameters[1].Value = DBNullableReader.NullCheck(record.Item2);
                                command.Parameters[2].Value = DBNullableReader.NullCheck(record.Item3);
                                command.Parameters[3].Value = record.Item4;
                                command.Parameters[4].Value = (int)record.Item5;

                                if (command.ExecuteNonQuery() == 0)
                                {
                                    var text = "Cannot update file record. Id: " + record.Item1.ToString();
                                    outputs.Add(new Tuple <int, string>(2, text));
                                    exceptions.Add(new ErrorRecord(new InvalidOperationException(text), "ImageStore Measuring - Update database", ErrorCategory.WriteError, record.Item1));
                                }

                                if (!record.Item6)
                                {
                                    commandToDeleteSame.Parameters[0].Value = record.Item1;
                                    commandToDeleteSame.ExecuteNonQuery();
                                    commandToDeleteSimilar.Parameters[0].Value = record.Item1;
                                    commandToDeleteSimilar.ExecuteNonQuery();
                                }
                            }
                            catch (InvalidOperationException)
                            {
                                outputs.CompleteAdding();
                                return;
                            }
                            catch (Exception ex)
                            {
                                outputs.Add(new Tuple <int, string>(2, ex.ToString()));
                                exceptions.Add(new ErrorRecord(ex, "ImageStore Measuring - Update database", ErrorCategory.ResourceUnavailable, null));
                            }
                        }
                    }
        }