码迷,mamicode.com
首页 > 其他好文 > 详细

【2017-04-21】Ado.Nte属性扩展

时间:2017-04-22 20:42:44      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:ext   nbsp   cat   color   space   row   tab   cut   generic   

通过对数据库表的封装,对该表的属性进行扩展。

1、例如:表中的性别是bool类,要实现显示给用户看的为“男、女”

技术分享

2、通过表中的生日datetime类,来实现显示给用户看的年月日,自动计算年龄。

技术分享

3、通过数据库User表中的民族编号,显示给用户看该民族编号对应的Nation表中的民族名称

技术分享

调用一个查询民族名称的方法

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.App_Code
{
    public class UserNationData
    {
        SqlConnection conn;
        SqlCommand cmd;

        public UserNationData()
        {
            conn = new SqlConnection("server=.;database=Data0216;user=sa;pwd=123");
            cmd = conn.CreateCommand();
        }

        public string SelectNationName(string ncode)
        {
            string end = "<暂无>";

            cmd.CommandText = "select *from UserNation where NationCode = @a";
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@a", ncode);

            try
            {
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    end = dr["NationName"].ToString();
                }
            }
            catch { }
            finally { conn.Close(); }
            return end;
        }
    }
}

 

【2017-04-21】Ado.Nte属性扩展

标签:ext   nbsp   cat   color   space   row   tab   cut   generic   

原文地址:http://www.cnblogs.com/qq609113043/p/6748936.html

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