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;
                }
            }
        }
    }

  • 相关阅读:
    multiprocessing模块
    socket&socketserver网络编程
    collections模块
    socket模块
    网络基础知识
    迭代器,生成器,列表推导式,生成器表达式
    logging模块
    Module
    页面中公用的全选按钮,单选按钮组件的编写
    ajax
  • 原文地址:https://www.cnblogs.com/ycxyyzw/p/2300801.html
Copyright © 2011-2022 走看看