• 20150505 数据访问2(练习)


    public const string CONNECTIONSTRING = "server=.;database=mydb;uid=sa;pwd=123";
    static string GetNationName(string code)
    {
    string str = "";

    //根据民族代号查询民族名称
    SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
    conn.Open();

    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "select * from nation where code='" + code + "' ";
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
    dr.Read();
    str = dr["Name"].ToString();
    }
    else
    {
    str = "未知";
    }

    conn.Close();

    return str;
    }
    static string GetWorks(string code)
    {
    string str = "";

    SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
    conn.Open();

    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "select * from work where infocode='" + code + "'";
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
    str += ((DateTime)dr["StartDate"]).ToString("yyyy年MM月dd日") + " ";
    str += ((DateTime)dr["EndDate"]).ToString("yyyy年MM月dd日") + " ";
    str += dr["Firm"].ToString()+ " ";
    str += dr["Depart"].ToString() + " ";
    }

    conn.Close();

    return str;
    }
    static void Main(string[] args)
    {
    //显示
    SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
    conn.Open();

    SqlCommand cmd = conn.CreateCommand();
    //cmd.CommandText = "select info.code,info.name,info.sex,nation.name as nationname,info.birthday from info,nation where info.nation = nation.code";
    //cmd.CommandText = "select code,name,sex,(select name from nation where info.nation=nation.code) as nationname,birthday from info";
    cmd.CommandText = "select * from info";
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
    string code = dr["code"].ToString();
    string name = dr["name"].ToString();
    string sex = ((bool)dr["Sex"]) ? "男" : "女";
    string nation = GetNationName(dr["nation"].ToString());
    string birthday = ((DateTime)dr["birthday"]).ToString("yyyy年MM月dd日");

    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine(code + " " + name + " " + sex + " " + nation + " " + birthday);
    Console.ResetColor();
    //显示工作简历
    Console.WriteLine("****工作简历****");
    Console.WriteLine(GetWorks(code));
    }

    conn.Close();

  • 相关阅读:
    设计模式-观察者模式(Observer Pattern)
    设计模式-策略模式(Strategy Pattern)
    数据结构-红黑树
    数据结构-二叉搜索树(BST binary search tree)
    算法-插入排序(Insertion sorting)
    算法-桶排序(Bucket sort)
    设计模式-单例模式(Singleton Pattern)
    算法-基数排序(radix sort)
    算法-计数排序及其变体
    Pytest框架的使用
  • 原文地址:https://www.cnblogs.com/m123/p/4479523.html
Copyright © 2020-2023  润新知