Best way to represent a color in a SQL Database?

前端 未结 6 1177
情歌与酒
情歌与酒 2021-01-12 04:14

If I am using .Net and SQL Server 2008, what is the best way for me to store a color in the database, should I use ToString or convert it to an integer, or something else?

6条回答
  •  温柔的废话
    2021-01-12 04:18

    If you are storing the System.Drawing.Color, you need to store 4 bytes that represent the alpha and the 3 color channels. You can use a int data type.

    If you are storing the System.Windows.Media.Color (from WPF), there are two possibilities depending on the usage you are using:

    1. If you are only using ARGB colors, store as a int data type.
    2. If you are using the ScRGB format, you need to store 4 float (single) values. I suggest two way of storing it:
      1. A user-defined type with 4 float fields.
      2. A varchar (length depend on the precision) and storing
        color.ToString(CultureInfo.InvariantCulture)

提交回复
热议问题