我在数据库中有一个 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://learn.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# 的方法参数中定义位数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48461874/