Search code examples
javascriptasp.netwebpackvarcharvarbinary

storing javascript source file in sql server database


I am developping an asp.net web application where an user could upload a javascript source code file and store it into a sqlserver database.

The file he uploads is a javascript file developped by him and then "minified" thanks to webpack. The final file could looks like something like this :

window["integration/user"]=function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(

This is only an extract of the file. Those files have usually a size of 10 000 bytes (size given by my windows explorer).

So my question was the following :

As varchar and varbinary have the same maximum capacity (8000 chars equivalent to 8000 bytes for varchar and 8000 bytes for varbinary), what would be the best between :

-storing the javascript as plain text in a varchar column

-converting in c# to binary data and then store it into varbinary column

Also, I have done a simple test by trying the second solution, and it's working (I've managed to store it into a varbinary column and then read back javascript from it with c# conversion code), but what I can't understand is how a file of a size of 10000bytes(size given by windows) could be successfully stored in varbinary column of a maximum capacity of 8000 bytes (so smaller than the file) ?

this is my column declaration :

[JavascriptBundle] [varbinary](max) NOT NULL

Solution

  • I think the confusion is that if you are explicitly defining the lengths of varchar or varbinary then 8000 is the limit.

    However varchar(max) and varbinary(max) both have the maximum lengths of 2^31-1, or 2,147,483,647 bytes. Which are the way you would want to define your database column.

    Which of the two you use would depend on your preferred design, varchar having the advantage of not having to convert to binary and back.

    References:

    varchar - https://learn.microsoft.com/en-us/sql/t-sql/data-types/char-and-varchar-transact-sql?view=sql-server-2017

    varbinary - https://learn.microsoft.com/en-us/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-2017