2 u012843031 u012843031 于 2013.11.30 11:31 提问

输出是null不知道为什么
c#

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
a4310269   2013.12.06 20:31

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

Csdn user default icon
上传中...
上传图片
插入图片