编写“null”的最佳方式是什么? 在组合框中显示选项时连接到 SQL 数据库?
我正在编写数据库的前端,将选项显示为组合框。 我需要能够将空值返回到数据库。
我目前有一个解决方案,其中涉及将记录“捏造”到查询中以填充数据表,然后检测选择并将 null
硬编码到我的更新语句中(如果已选择此项)。
--GetDataByAvailableTechs query
SELECT tblLEMSCredentialsId, TechNumber
FROM tblLEMSCredentials AS aLEMSCreds
UNION ALL
SELECT '99999' AS Expr1, '<NONE>' AS Expr2
。
//populate combo box
BV_LEMSHIPDataSet.tblLEMSCredentialsDataTable dtAvailableTechs = taLEMSCreds.GetDataByAvailableTechs(selectedSerial);
cboTechNumber.DataSource = dtAvailableTechs;
。
//Save back to DB
if (lemsCredsID == 99999)
{
taDevice.UpdateQuery_Restage(null, selectedSerial);
}
else
{
taDevice.UpdateQuery_Restage(lemsCredsID, selectedSerial);
}
有人可以建议更好的方法吗? 我需要让应用程序处理另外 5 个类似的字段,并且不想创建大量 if else 版本的更新。
G
I'm writing a front end to a database, presenting options as comboboxes. And I need to be able to return nulls to the database.
I have a solution at the moment which involves 'fudging' a record into the query to populate the Datatable, then detecting the selection and hard coding null
into my update statement if this item has been selected.
--GetDataByAvailableTechs query
SELECT tblLEMSCredentialsId, TechNumber
FROM tblLEMSCredentials AS aLEMSCreds
UNION ALL
SELECT '99999' AS Expr1, '<NONE>' AS Expr2
.
//populate combo box
BV_LEMSHIPDataSet.tblLEMSCredentialsDataTable dtAvailableTechs = taLEMSCreds.GetDataByAvailableTechs(selectedSerial);
cboTechNumber.DataSource = dtAvailableTechs;
.
//Save back to DB
if (lemsCredsID == 99999)
{
taDevice.UpdateQuery_Restage(null, selectedSerial);
}
else
{
taDevice.UpdateQuery_Restage(lemsCredsID, selectedSerial);
}
Can anyone suggest a better way of doing this please? I need to make the application cope with another 5 similar fields and don't want to create a multitude of if else versions of my update.
G
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
过去我使用过类似的方法,但对 NULL 值使用负数 - 这样您就可以在一个 if 语句中将任何小于零的值视为 NULL。
不过,这假设您在表中的 ID 始终为正:)
In the past I've used a similar approach but used minus numbers for the NULL values - that way you can you treat anything less than zero as NULL in one if statement.
This assumes that your ID's in the table are always positive though :)
空对象模式。 让它为 UI 返回一个空字符串,并提供一个方法调用来在更新数据库时提取值。 在正常情况下,这将返回您的真实值,但空对象将覆盖此行为以给您空值。
不需要幻数,您所需要做的就是在将控件绑定到数据源时将此值添加到“可绑定值”的开头。
Null object pattern. Make it return an empty string for the UI and provide a method call to extract the value when updating the database. In normal cases this will return your true value but the null object will override this behaviour to give you your null.
No magic numbers required and all you need to do is add this value to the start of the "bindable values" when you bind your control to the datasource.
您可以将字符串值作为组合框“空值”,例如:“thenullvalue”,然后将组合框值解析为可空 int 类型(int?)。 对于正确的选择,您将获得所需的值,对于“空选择”,您将获得 null(将“thenullvalue”解析为 int 将为您提供 null):)
然后将值保存到数据库,如下所示:
根据您的数据库提供商,您也可能需要将可空类型 null 值转换为 DBNull,如下所示:
希望这会有所帮助。
You may put an string value as the combobox 'null value', for example: "thenullvalue" then parse combobox value to nullable int type (int?). For proper selection you will get the desired value, for 'null selection' you will get null (parsing "thenullvalue" to int will give you null) :)
Then save the value to database just as:
Depending of your db provider you might also need to convert nullable type null value to DBNull, like this:
Hope this helps.
如果该值打算为 null,则将 null 传递给数据库将是一个很好的解决方案,这减少了对 null 的特殊值的额外逻辑处理,并且可能非常危险且不明确。
视图或应用程序层应该能够处理从数据库返回或传入数据库的 null。
例如,您可以在绑定控件之前将 null 转换为 string.Empty,也可以在传递到数据库之前将 string.Empty 转换为 null。
If the value is intended to be null, pass null to the database would be a good solution, this reduces the extra logic handling for special values for nulls and it can be quite risky and ambiguous.
The view or application layer should be able to handle null either returned from database or passing into it.
e.g. you can convert null to string.Empty before binding the control as well as you can convert string.Empty to null before passing into the database.