我在数据库上有一个 SQL 表,其中有一列使用 Bit 数据类型。我试图在我的 C# 应用程序中定义一个方法,它从表中获取两列并将它们用作参数。
这是代码
public void Edit_NAA_ApplicationsFirm(int ApplicationId, string UniversityOffer, bit Firm)
{
//This line declares a variable in which we store the actual values from NAA_Applications based on the associating ID
NAA_Applications AppFirm = Get_Applicant_Application(ApplicationId);
//Here we tell the application that the values edited are equal to the values within the table
AppFirm.Firm = Firm;
//Any of these changes are then saved.
_context.SaveChanges();
}
唯一的问题是程序不断尝试将位转换为 BitConverter。当我将其更改为 bit 时,将其作为数据类型接受时会出现问题。
值得注意的是,我正在 ASP.Net Framework 解决方案中构建应用程序。
谁能告诉我我做错了什么?我只是指数据类型错误吗?
最佳答案
看起来您正在使用 Entity Framework 。
它将使用 .NET boolean
来表示 T-SQL bit
。对于任何其他数据访问方法,这也是一种明智的方法。 boolean true
将在位字段中转换为 1
,并将 false
转换为 0
。
事实上,它甚至不止一次记录在案,这是要使用的正确 .NET CLR 类型。参见 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/sql-clr-type-mapping 、 https://msdn.microsoft.com/en-us/library/cc716729(v=vs.100).aspx 和其他可能的。
所以在你的情况下 bool Firm
是合适的。
关于c# - 如何在 C# 中的方法参数中定义 Bit 数据类型?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48461874/