为什么不输出数据呢?

程序没有报错,数据库中也有数据,程序运行后就直接关闭了,为什么不输出数据呢?


using System;

using System.Data;

using System.Data.SqlClient;


namespace ConsoleApp1

{

    class Program

    {

    static void Main(string[] args)

        {

            SqlConnection conn = new SqlConnection("server=wdt2019.australiasoutheast.cloudapp.azure.com;uid=s3642823;database=s3642823;pwd=abc123;");

            SqlCommand query = new SqlCommand("select * from dbo.Room", conn);

            SqlDataReader read;



            try

            {

                conn.Open();

                read = query.ExecuteReader();

                while (read.Read())

                {

                    Console.WriteLine("{0}",read["RoomID"]);

                }

                read.Close();


            }

            catch (SqlException se)

            {

                Console.WriteLine("SQL Exception: {0}", se.Message);

            }

            catch (Exception e)

            {

                Console.WriteLine("Exception: {0}", e.Message);

            }

            finally

            {

                if (conn.State == ConnectionState.Open)

                {

                    conn.Close();

                }


            }

        }


    }

}


安小圈
浏览 928回答 1
1回答

慕函数5213667

猜是显示了,但是数据太少,在read.Close()这里打个断点调试一下
打开App,查看更多内容
随时随地看视频慕课网APP