新问题:
我建立的一个SQL数据表,并且我用C#编程,我要检索某个班级的全体成员,
 表如下 :  班级详单1班  A  B    C    D 
2班  E  F    G    H 
3班  I  J    K    L 
 
我的程序如下using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
           
                SqlConnection SqlCon = new SqlConnection("Data Source=;Database=AAA;User id=sa;pwd=SQL4567");
                SqlCon.Open();
                string SqlStr="SELECT * FROM Table_1 WHERE 班级=1班";
                SqlCommand SqlCmd = new SqlCommand(SqlStr, SqlCon);
                SqlDataReader SqlRd = SqlCmd.ExecuteReader();
                while (SqlRd.Read())
                {
                    Console.WriteLine(SqlRd.GetString());
                }
                SqlCon.Close();      
        }
    }

貌似我用的GetString()方法不能将内容显示到控制台  有什么其他方法吗?

解决方案 »

  1.   

    Console.Write(SqlRd[0].ToString() + "      " + SqlRd[1].ToString() +"     " + SqlRd[2].ToString())
    Console.writeLine();
      

  2.   

                    while (SqlRd.Read())
                    {
                        Console.WriteLine(SqlRd[0].ToString());
                        Console.WriteLine(SqlRd[1].ToString());
                        ...................
                    }
      

  3.   

    SqlRd.GetString()这里面要传递一个Index
      

  4.   

    SqlRd.Read()只能是读取列中的下一条记录 而不能读出行中的下一条记录  对吗?
      

  5.   

    我这样写对吗?为什么记过只有一个 A 呢?using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                    int i = 0;
                    SqlConnection SqlCon = new SqlConnection("Data Source=;Database=AAA;User id=sa;pwd=SQL4567");
                    SqlCon.Open();
                    string SqlStr="SELECT * FROM Table_1 WHERE 班级='1'";
                    SqlCommand SqlCmd = new SqlCommand(SqlStr, SqlCon);
                    SqlDataReader SqlRd = SqlCmd.ExecuteReader();
                    while (SqlRd.Read())
                    {
                        while (SqlRd[i] != NULL)
                        {
                            Console.Write(SqlRd[i].ToString() + "  ");
                            i++;
                        }
                    }
                    Console.WriteLine();
                    SqlCon.Close();      
            }
        }
    }
      

  6.   


    C# 
    public override string GetString (
    int i
    )这是MSDN中对sqlDataReader.GetString()方法的定义,lz都没有给GetString()传参数
     
      

  7.   

    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                    int i = 0;
                    SqlConnection SqlCon = new SqlConnection("Data Source=;Database=AAA;User id=sa;pwd=SQL4567");
                    SqlCon.Open();
                    string SqlStr="SELECT * FROM Table_1 WHERE 班级='1'";
                    SqlCommand SqlCmd = new SqlCommand(SqlStr, SqlCon);
                    SqlDataReader SqlRd = SqlCmd.ExecuteReader();
                    while (SqlRd.Read())
                    {for(int i=0;i<SqlRd.FieldCount();i++)
    {
    Console.Write(SqlRd[i]);
    }

                    }
                    Console.WriteLine();
                    SqlCon.Close();      
            }
        }
    }
    这样!!!!!!!!!!!!!!!
    基础书去看看啊
      

  8.   

    能给推荐哪本书上有相关内容吗?  我看了ADO.net  可是书上基本找不到我用的着的东西...
      

  9.   

    比如你们说的那个 FieldCount()方法  我从来没在书上见过  是我读的书不对吗?
      

  10.   

    static void Main(string[] args)
            {
                string conString = "Data Source=.;Database=Northwind;User id=sa;pwd=";
                SqlConnection con = new SqlConnection(conString);            SqlCommand command = new SqlCommand("select * from Orders",con);
                con.Open();
                SqlDataReader reader = command.ExecuteReader();            while (reader.Read())
                {
                    Console.WriteLine(reader.GetString(1).ToString());
                }
                con.Close();
                con.Dispose();
                Console.ReadLine();
            }