zoukankan      html  css  js  c++  java
  • DBNull和可空类型

      private static int GetAge()
            {
                DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Customers where id=4");
                T_Customers cust = new T_Customers();
                DataRow row = dt.Rows[0];
                //int i = null;//因为在C#中,int、bool、DateTime等值类型的数据不可以为null
                //string,Person等属于引用类型,可以为null

                //值类型:拷贝传递,不可以为null
                //引用类型:引用传递,可以为null。

                //如果数据库里面存的是NULL,那么用row["TelPhone"]
                //取出来的数据的值是DBNull.value,ADO.Net对于C#中int不可以为null
                //数据库中int可以为null的一个权衡
                //string s1 = null;
                //Person p1 = null;
                //DBNull.Value; null
                //null,DBNULL

                //DBNull.Value不等于null
                // MessageBox.Show((DBNull.Value == null).ToString());
                //if (row["TelPhone"]==DBNull.Value)//判断字段值是否为null的方法
                if (row.IsNull("TelPhone"))//更简单的判断数据库中字段值为null的方法
                {
                    cust.TelPhone = null;
                }
                else
                {
                    cust.TelPhone = (string)row["TelPhone"];
                }


                if (row.IsNull("Age"))
                {
                    return -1;
                }
                else
                {
                    int age = (int)row["Age"];
                    return age;
                }

                //dataGridView1.DataSource
                //    =  new T_CustomersBLL().ListAll();
            }

    namespace 可空类型
    {
        class Program
        {
            static void Main(string[] args)
            {
                //object obj = null;
                //object i = obj ?? DBNull.Value;

                //int? i = null;
                //i = 90;
                //i++;
                //Console.WriteLine(i);

                ////在值类型后加?就成为了可空类型

                //int? age = GetAge();
                //Console.WriteLine(age.ToString());
                ////int age2 = GetAge();
                ////int age2 = (int)GetAge();
                //if (age.HasValue)//判断可空类型的对象是否为null
                //{
                //    int i2 = age.Value;
                //    //int i = (int)age;
                //}
                //else
                //{
                //    Console.WriteLine("nulnullnull");
                //}

                ////int w1 = 50;
                ////int? w2 = w1;
                ////int w3 = w2;

                Console.ReadKey();
            }

            public static int? GetAge()
            {
                if (DateTime.Now.Second % 2 == 0)
                {
                    return 20;
                }
                else
                {
                    return null;
                }
            }
        }
    }

  • 相关阅读:
    第一节,Django+Xadmin打造上线标准的在线教育平台—创建用户app,在models.py文件生成3张表,用户表、验证码表、轮播图表
    Tensorflow 错误:Unknown command line flag 'f'
    Python 多线程总结
    Git 强制拉取覆盖本地所有文件
    Hive常用函数 傻瓜学习笔记 附完整示例
    Linux 删除指定大小(范围)的文件
    Python 操作 HBase —— Trift Trift2 Happybase 安装使用
    梯度消失 梯度爆炸 梯度偏置 梯度饱和 梯度死亡 文献收藏
    Embedding 文献收藏
    深度学习在CTR预估中的应用 文献收藏
  • 原文地址:https://www.cnblogs.com/ycxyyzw/p/2300801.html
Copyright © 2011-2022 走看看