jeudi 1 mars 2018

Data Doesn't Save In MSSQL

I have a .NET Web API (C#) where I can GET and POST data (Obviously). When I make a GET request, it returns all the data which it is supposed to. I have made my methods/functions to return a bool if a sql command worked. So when I make a PUT or POST request it returns true (which means it worked).

Then if I make a GET request or if I SELECT all in SQL Management Studio, the data hasn't saved. However Delete works.

This is my Add/POST method:

    public bool Add(string id, byte[] bytes)
    {
        SqlCommand Command = new SqlCommand("INSERT INTO [ag_Images] ([Id], [Data]) VALUES (@Id, @Data)");
        Command.Parameters.AddWithValue("@Id", id);
        Command.Parameters.AddWithValue("@Data", bytes);
        return _Sql.Execute(Command);
    }

Which passes a SqlCommand to:

    SqlTransaction _Transaction;

    public bool Execute(SqlCommand Command)
    {
        SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["AptestDB"].ConnectionString);
        using (conn)
        {
            try
            {
                conn.Open();
                _Transaction = conn.BeginTransaction("SQL_Execution");
                Command.CommandType = CommandType.Text;
                Command.Connection = conn;
                Command.Transaction = _Transaction;
                try
                {
                    Command.ExecuteNonQuery();
                    _Transaction.Commit();
                    return true;
                }
                catch
                {
                    _Transaction.Rollback();
                    return false;
                }
            }
            catch
            {
                return false;
            }

        }
    } 

The above doesn't work. However, this does (SQL Management Studio)

DECLARE @Id VARCHAR(50)
DECLARE @Data VARBINARY(MAX)

SET @Id = '00000000-0000-0000-0000-000000000000'
SET @Data = CAST('' AS VARBINARY)

UPDATE [ag_Images] SET [Data] = @Data WHERE [Id] = @Id




Aucun commentaire:

Enregistrer un commentaire