码迷,mamicode.com
首页 > 数据库 > 详细

自学.net(6)DBNULL

时间:2014-09-04 00:00:06      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   数据   art   div   cti   

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DBNULL
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            object objName;
            object objAge;
            object objHeight;

            string name=tbName.Text;
            string age=tbAge.Text;
            string height=tbHeight.Text;

            if (name.Length<=0)
            {
                objName = DBNull.Value;
            }
            else
            {
                objName = name;
            }

            if (age.Length <= 0)
            {
                objAge = DBNull.Value;
            }
            else
            {
                objAge = name;
            }

            if (height.Length <= 0)
            {
                objHeight = DBNull.Value;
            }
            else
            {
                objHeight = height;
            }

            SqlHelper.ExecuteNonQuery(@"insert into T_Null(Name,Age,Height) 
            values (@Name,@Age,@Height)",
             new SqlParameter("@Name", objName),
             new SqlParameter("@Age", objAge),
             new SqlParameter("@Height", objHeight));
        }

        private void btn2_Click(object sender, RoutedEventArgs e)
        {
            DataTable table= SqlHelper.ExecuteDataTable("select * from T_Null where Id=7");
            DataRow row = table.Rows[0];
            string name;
            if (row["Name"]==DBNull.Value)
            {
                name = null;
            }
            else
            {
                name = (string)row["Name"];
            }

            int? age; //int类型不能转换为null值,int?为可空数据类型
            if (row["Age"]==DBNull.Value)
            {
                age = null;
            }
            else
            {
                age = (int)row["Age"];
            }

             int? height;
            if (row["Height"]==DBNull.Value)
            {
                height = null;
            }
            else
            {
                height = (int)row["Height"];
            }
           
        }
    }
}

 

自学.net(6)DBNULL

标签:style   blog   color   io   ar   数据   art   div   cti   

原文地址:http://www.cnblogs.com/xiaoyuyouyou/p/3954950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!