必有邻 2013-11-30 03:31 采纳率: 0%
浏览 1764

输出是null不知道为什么

public void second()
{
string strsql = "data source=KNUZ7ZSDSSK5SAJ;initial catalog=myschool;user id=sa;pwd=123";

        SqlConnection con = new SqlConnection(strsql);
        try
        {
            con.Open();
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("select");
            sb.AppendLine("    [studentname]");
            sb.AppendLine("    [studentno]");
            sb.AppendLine("from");
            sb.AppendLine("       [student]");
            SqlCommand com = new SqlCommand(sb.ToString(), con);                           
            SqlDataReader reader= com.ExecuteReader();
            if(reader==null){
                Console.WriteLine("出现异常");                                   
            }
            Console.WriteLine("学号\t姓名");
            reader.Read();
            while(reader.Read())
            {
                sb.AppendFormat("{0}\t{1}", reader["studentname"], reader["studentno"]);
                Console.WriteLine(sb);
                sb.Length = 0;                   
            }
            reader.Close();
        }
        catch (Exception)
        {
            Console.WriteLine("null");
        }
        finally 
        {
            con.Close();         
        }

    }
  • 写回答

1条回答

  • a4310269 2013-12-06 12:31
    关注

    SQL语句拼接有问题啊,童鞋

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料