I'm writing an app in C# that connects to a SQL Server using Entity Framework. Every instance of the app shares the tables and some variables (ints, strings and bools).
What's the best way to share that variables (int string and bools) via tables in SQL Server?
Since tables have fixed type columns, one table would not do it without loosing type-safe in C#, because every type should be converted to string or boxed to object.
The two solutions I came up with are, one table with 3 columns ( int
, varchar
, bool
), with the data writing in the appropriately typed column, or 3 tables with one column.
Or maybe I am totally missing the point here..
The question would be: what's the most elegant way to accomplish saving typed data to a SQL Server?
You can use SQL_Variant
for that purpose
https://docs.microsoft.com/en-us/sql/t-sql/data-types/sql-variant-transact-sql?view=sql-server-2017
CREATE TABLE tableA(colA sql_variant, colB int)
INSERT INTO tableA values ( cast (46279.1 as decimal(8,2)), 1689)
SELECT SQL_VARIANT_PROPERTY(colA,'BaseType') AS 'Base Type',
SQL_VARIANT_PROPERTY(colA,'Precision') AS 'Precision',
SQL_VARIANT_PROPERTY(colA,'Scale') AS 'Scale'
FROM tableA
WHERE colB = 1689
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.