using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication1
{
    class Program
    {

       static string strcon = "server=.\\sqlexpress;uid=sa;pwd=123;database=AdventureWorks";
        static void Main(string[] args)
        {
            do
            {
                Console.WriteLine("请输入任意数字!");
                SqlConnection conn = new SqlConnection(strcon);
                conn.Open();
                string sql = "select FirstName from Person.Contact";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    //Response.Write(dr.FieldCount.ToString());
                    if (dr.FieldCount > 0) Console.WriteLine(dr.FieldCount);
                    //Console.WriteLine(dr.GetString(0));
                    Console.WriteLine(dr[0].ToString ());
                }

                dr.Close();
                // 传递该参数 会自动关闭connnection 的方法 
                cmd.ExecuteReader(CommandBehavior.CloseConnection);
                //CommandBehavior.CloseConnection 如果关闭该对象 则与之相关联的连接对象 也会关闭
            } while (Console.ReadLine() != "0");
            Console.ReadLine();

        }

        //SqlDataReader 数据读取器 类似于游标,其中有一个重要的方法就是read() 很想PHP中操作
        //数据库的函数

        // 1当sqlDataReader 没有关闭之前,数据库连接会一直保存连接状态 不用时要及时关闭SqlDataReader.Close()
        // 2  一个连接只能被一个sqlDataReader使用,
        //
    }
}