Home Database Mysql Tutorial DBNull和Null的区别

DBNull和Null的区别

Jun 07, 2016 pm 04:20 PM
null the difference

DBNull 类 表示不存在的值。无法继承此类。 命名空间: System 程序集: mscorlib(在 mscorlib.dll 中) DBNull 类表示一个不存在的值。例如,在数据库的表中,某一行的某列中可能不包含任何数据。即,该列被视为根本不存在,而不只是没有值。一个表示不存在

   DBNull 类

  表示不存在的值。无法继承此类。

  命名空间: System

  程序集: mscorlib(在 mscorlib.dll 中)

  DBNull 类表示一个不存在的值。例如,在数据库的表中,某一行的某列中可能不包含任何数据。即,该列被视为根本不存在,而不只是没有值。一个表示不存在的列的 DBNull 对象。此外,COM 互操作使用 DBNull 类来区分 VT_NULL 变量(指示不存在的值)和 VT_EMPTY 变量(指示未指定的值)。

  DBNull 类型是一个单独的类,这意味着只有一个 DBNull 对象存在。DBNull::.Value 成员表示唯一的 DBNull对象。DBNull::.Value 可用于将不存在的值显式分配给数据库字段,但大多数 ADO.NET 数据提供程序在字段没有有效值时会自动分配 DBNull 值。您可以通过将从数据库字段检索到的值传递给 DBNull.Value.Equals 方法,确定该字段值是否为 DBNull 值。然而,有些语言和数据库对象提供一些方法,可以更容易地确定数据库字段值是否为 DBNull::.Value.这些方法包括 Visual Basic 的 IsDBNull 函数、Convert::.IsDBNull 方法、DataTableReader::.IsDBNull 方法和 IDataRecord::.IsDBNull 方法。

  请勿将面向对象的编程语言中的 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 概念与 DBNull对象混淆。在面向对象的编程语言中,nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 表示不存在对某个对象的引用。DBNull 则表示未初始化的变量或不存在的数据库列。

  备注

  示例

  下面的示例调用 DBNull.Value.Equals 方法,来确定联系人数据库中的数据库字段是否具有有效值。如果具有有效值,字段值将被追加到在标签中输出的字符串中。

  C# 中的用法

  private void OutputLabels(DataTable dt)

  {

  string label;

  // Iterate rows of table

  foreach (DataRow row in dt.Rows)

  {

  int labelLen;

  label = String.Empty;

  label += AddFieldValue(label, row, "Title");

  label += AddFieldValue(label, row, "FirstName");

  label += AddFieldValue(label, row, "MiddleInitial");

  label += AddFieldValue(label, row, "LastName");

  label += AddFieldValue(label, row, "Suffix");

  label += "/n";

  label += AddFieldValue(label, row, "Address1");

  label += AddFieldValue(label, row, "AptNo");

  label += "/n";

  labelLen = label.Length;

  label += AddFieldValue(label, row, "Address2");

  if (label.Length != labelLen)

  label += "/n";

  label += AddFieldValue(label, row, "City");

  label += AddFieldValue(label, row, "State");

  label += AddFieldValue(label, row, "Zip");

  Console.WriteLine(label);

  Console.WriteLine();

  }

  }

  private string AddFieldValue(string label, DataRow row,

  string fieldName)

  {

  if (! DBNull.Value.Equals(row[fieldName]))

  return (string) row[fieldName] + " ";

  else

  return String.Empty;

  }

  初学数据库编程我们可能会有一些对"空值"的疑问,比如通过编程新建的一个表中所有数据皆显示为,手动添加并删除文字后又变成了空白;一个字符串类型的字段,明明没有填值,却不等于"";用ADO.NET从数据库中取值,每遇到有的就出错……这需要我们正确认识。NET和SQL Server中几种不同的"空值".

  1、真正的空值,也就是"没有输入的值",可以出现在大多数类型的字段中(如果没有别的约束条件),SQL server中表示为null,显示为,手工在SQL server企业管理器中输入的方法是按Ctrl+0.它在。NET中对应System.DBNull.Value.在T-SQL命令中,判断一个值是不是空值,要用"is null"而不是"= null";处理空值有个ISNULL函数,它使用指定的值替换null.用ADO.NET从数据库得到的空值无法自动转化为空字符串或Nothing,须手动检测:如果得到System.DBNull.Value,则赋给数据对象Nothing或其它自定义的有意义的值。

  2、空字符串(零长度字符串),只出现在字符串类型(如nvarchar)的字段中,SQL server中表示为'',显示为空白,手工在SQL server企业管理器中输入时清空一个单元格即可。它在。NET中对应System.String.Empty,也就是我们常用的"".在T-SQL命令中处理空字符串和处理一般的字符串没什么区别。用ADO.NET从数据库得到的空字符串也和一般的字符串没什么区别。

  DBNull简介

  DBNull在DotNet是单独的一个类型 System.DBNull .它只有一个值 DBNull.Value .DBNull 直接继承 Object ,所以 DBNull 不是 string , 不是 int , 也不是 DateTime …

  但是为什么 DBNull 可以表示数据库中的字符串,数字,或日期呢?原因是DotNet储存这些数据的类(DataRow等)都是以 object 的形式来储存数据的。对于 DataRow , 它的 row[column] 返回的值永远不为 null , 要么就是具体的为column 的类型的值 . 要么就是 DBNull . 所以 row[column].ToString() 这个写法永远不会在ToString那里发生NullReferenceException.

  DBNull 实现了 IConvertible . 但是,除了 ToString 是正常的外,其他的ToXXX都会抛出不能转换的错误。

  在 IDbCommand(OleDbCommand,SqlCommand…) 的ExecuteScalar的返回值中,情况可以这样分析:

  select 1 这样返回的object是 1 select null 这样返回的是DBNull.Value select isnull(null,1) 返回的是 1 select top 0 id from table1 这样返回的值是null select isnull(id,0) from table1 where 1=0 返回的值是null

  这里 ExecuteScalar 的规则就是,,返回第一列,第一行的数据。如果第一列第一行不为空,那么ExecuteScalar就直接对应的DotNet的值。如果有第一行,但是第一列为空,那么返回的是 DBNull .如果一行都没有,那么ExecuteScalar就返回null

  规则就是这样的。这里容易犯的一个错误是,把ExecuteScalar返回DBNull与null的情况混淆,例如:

  string username=cmd.ExecuteScalar()。ToString();

  除非你认为cmd执行后,肯定至少有一行数据,否则这里就会出错。

  又或者 select id from usertable where username=@name 这样的sql语句,如果找不到记录,那么ExecuteScalar则会返回null,所以千万不要

  int userid=Convert.ToInt32(cmd.ExecuteScalar());

  或者你会这样写 SQL 语句:select isnull(id,0) from usertable where username=@name

  但是 int userid=Convert.ToInt32(cmd.ExecuteScalar()); 依然会出错,因为上面的语句不成立时,仍然是不返回任何行。

  对于IDbDataParameter(OleDDbParameter,SqlParameter)的Value,如果为null,则代表该参数没有指定,或者是代表DEFAULT.如果为DBNull.Value,则代表SQL中的NULL

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to set password protection for export PDF on PS How to set password protection for export PDF on PS Apr 06, 2025 pm 04:45 PM

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

What is the difference between syntax for adding columns in different database systems What is the difference between syntax for adding columns in different database systems Apr 09, 2025 pm 02:15 PM

不同数据库系统添加列的语法为:MySQL:ALTER TABLE table_name ADD column_name data_type;PostgreSQL:ALTER TABLE table_name ADD COLUMN column_name data_type;Oracle:ALTER TABLE table_name ADD (column_name data_type);SQL Server:ALTER TABLE table_name ADD column_name data_

How to use XPath to search from a specified DOM node in JavaScript? How to use XPath to search from a specified DOM node in JavaScript? Apr 04, 2025 pm 11:15 PM

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

What are the different ways of promoting H5 and mini programs? What are the different ways of promoting H5 and mini programs? Apr 06, 2025 am 11:03 AM

There are differences in the promotion methods of H5 and mini programs: platform dependence: H5 depends on the browser, and mini programs rely on specific platforms (such as WeChat). User experience: The H5 experience is poor, and the mini program provides a smooth experience similar to native applications. Communication method: H5 is spread through links, and mini programs are shared or searched through the platform. H5 promotion methods: social sharing, email marketing, QR code, SEO, paid advertising. Mini program promotion methods: platform promotion, social sharing, offline promotion, ASO, cooperation with other platforms.

See all articles