using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "Data Source=.;Initial Catalog=ReportServer;User ID=sa;Password=55555";
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from dbo.Policies",con);
                SqlDataReader myReader = cmd.ExecuteReader();
                myReader.Read();
                con.Close();  
        }
    }
}刚学连接数据库,照书本打的,运行时只出现按任意键结束,不知道是什么原因。已经确认密码、数据库名称都没有错

解决方案 »

  1.   

    查询语句有没有查到结果?
    myReader.Read();
    只读一次,
    又没有做输出的操作,你想控制台程序给你什么样的反馈?
      

  2.   

    要读多条的话应该这样写while(myReader.Read())
    {
        ...
    }还有LZ没输出当然看不到结果。。
      

  3.   

     while (myReader.Read())  //通过遍历读取           {                                                                                       Console.WriteLine (myReader["字段名称"]);
    }