[英]How to determine bitwise code value stored in SQL Server from C# code
I want to read bitwise value stored into SQL server from C# code.我想从 C# 代码读取存储到 SQL 服务器的按位值。
Assume that following code which stores bitwise values into database.假设以下代码将按位值存储到数据库中。
DECLARE @MedCopay VARCHAR(1);
DECLARE @MedDeductible VARCHAR(1);
DECLARE @MedCoins VARCHAR(1);
SET @MedCopay = 'Y';
SET @MedDeductible = 'N';
SET @MedCoins = 'Y';
SELECT CASE
WHEN @MedCopay = 'Y'
THEN 1
ELSE 0
END|CASE
WHEN @MedDeductible = 'Y'
THEN 10
ELSE 0
END|CASE
WHEN @MedCoins = 'Y'
THEN 100
ELSE 0
END;
Above query will store following value into DB以上查询将以下值存储到数据库中
101
So question is that how can I write c# code to determine that所以问题是我如何编写 c# 代码来确定
101 = MedCopay & MedCoins?
Are you looking for enum
?你在找
enum
吗?
[Flags]
public enum Med {
Copay = 1,
Deductable = 2,
Coins = 4
};
and so所以
// 101
Console.Write(Convert.ToString((int) (Med.Copay | Med.Coins), 2));
reverse:逆转:
String source = "101";
Med parsed = (Med) (Convert.ToInt32(source, 2));
// Copay, Coins
Console.Write(parsed);
you may want to implement an extension class for this:您可能想要为此实现一个扩展类:
public static class MedExtensions {
public static String Represent(this Med value) {
return Convert.ToString((int) value, 2);
}
}
And so the test will be所以测试将是
Console.Write((Med.Copay | Med.Coins).Represent());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.