Winforms 中的 DatagridView ,SqlDataReader 是否在循环结束时自动关闭?

发布于 2024-10-31 17:31:05 字数 975 浏览 2 评论 0原文

int a ;

SqlCommand cmd = new SqlCommand (" Select * From items order by ItemID ", conn );

SqlDataReader reader = cmd.ExecuteReader();

while(reader.Read())
        {
            a = reader.GetInt32(0);

            if (reader.HasRows == false)
            {
                dataGridView1.Visible = false;
            }

            else
            {
                dataGridView1.Visible = true;

                DataTable dt = null;

                dt = new DataTable();

                dt.Load(reader);

                dataGridView1.DataSource = dt;

                if (reader.IsClosed == true)
                {
                        break;
                }   
            }

我想问读者是否自动关闭,因为这里没有使用 reader.Close() 但它仍然关闭?另外,在我的项目表中,我的第一条记录为

ItemId | ItemName

 1       Bag
 2       Laptop
 8       Weighing Machine 

但是,当此数据显示在 datagridview 中时,第 1 行(即项目名称“BAG”)不会显示。为什么会这样?

int a ;

SqlCommand cmd = new SqlCommand (" Select * From items order by ItemID ", conn );

SqlDataReader reader = cmd.ExecuteReader();

while(reader.Read())
        {
            a = reader.GetInt32(0);

            if (reader.HasRows == false)
            {
                dataGridView1.Visible = false;
            }

            else
            {
                dataGridView1.Visible = true;

                DataTable dt = null;

                dt = new DataTable();

                dt.Load(reader);

                dataGridView1.DataSource = dt;

                if (reader.IsClosed == true)
                {
                        break;
                }   
            }

I want to ask that is the reader closed automatically, because here am not using reader.Close() and still it is closed? also , in my items table i have the first record as

ItemId | ItemName

 1       Bag
 2       Laptop
 8       Weighing Machine 

But, when this data is displayed in the datagridview then the row 1 , that is, the itemname "BAG" is not displayed. why so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久随 2024-11-07 17:31:05

通过调用 Read(),您已经“声明”了第一行(因此 Bag 没有显示 - 因为您没有对它执行任何操作);然而,dt.Load将执行while(reader.Read())。我希望你想要(注意我没有在这里调用 Read ,并且没有 while 循环):

if(reader.HasRows)
{
   // load via dt.Load() and show
}
else
{
     // hide
}

它退出的原因是一旦你调用了 Load 您已经读取了所有数据,因此没有其他内容可读取。老实说,我不知道到达 TDS 流的末尾是否会隐式关闭阅读器,但您应该使用:

using(var cmd = new SqlCommand (" Select * From items order by ItemID ", conn))
using(var reader = cmd.ExecuteReader()) 
{
     // everything else in here
} 

By calling Read(), you have already "claimed" the first row (hence why Bag isn't showing - because you aren't doing anything with it); and yet dt.Load is also going to do a while(reader.Read()). I expect you want (note I'm not calling Read here, and have no while loop):

if(reader.HasRows)
{
   // load via dt.Load() and show
}
else
{
     // hide
}

The reason it is exiting is that once you've called Load you've already read all the data, so there is nothing else to read. I honestly don't know whether getting to the end of the TDS stream implicitly closes the reader, but you should be using:

using(var cmd = new SqlCommand (" Select * From items order by ItemID ", conn))
using(var reader = cmd.ExecuteReader()) 
{
     // everything else in here
} 
美人如玉 2024-11-07 17:31:05

您将读取器加载到 DataTable 中,因此不需要 while(reader.Read()) 循环。

第一条记录未显示,因为 reader.Read() 已获取第一条记录,而 dt.Load() 从第二条记录开始。

using (SqlConnection connection = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = connection.CreateCommand())
    {
        cmd.CommandText = "SELECT * FROM teams";
        connection.Open();
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            if (reader.HasRows)
            {
                dataGridView.Visible = true;
                DataTable dt = new DataTable();
                dt.Load(reader);
                dataGridView.DataSource = dt;
            }
            else
            {
                dataGridView.Visible = false;
            }
        }
    }
}

You are loading the reader into the DataTable so the while(reader.Read()) loop is not required.

The first record is not displaying because reader.Read() has taken the first record and dt.Load() is starting from the second record.

using (SqlConnection connection = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = connection.CreateCommand())
    {
        cmd.CommandText = "SELECT * FROM teams";
        connection.Open();
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            if (reader.HasRows)
            {
                dataGridView.Visible = true;
                DataTable dt = new DataTable();
                dt.Load(reader);
                dataGridView.DataSource = dt;
            }
            else
            {
                dataGridView.Visible = false;
            }
        }
    }
}
亚希 2024-11-07 17:31:05

即使这对我来说也很好用

string strcon = ConfigurationSettings.AppSettings["Constring"].ToString();
MySqlConnection conn = new MySqlConnection(strcon);
MySqlCommand cmd = new MySqlCommand("Select * From items order by ItemID", conn);
conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();

if (reader.HasRows)
{
   dataGridView1.Visible = true;
   DataTable dt = null;
   dt = new DataTable();
   dt.Load(reader);
   dataGridView1.DataSource = dt;
}

Even this too works fine for me

string strcon = ConfigurationSettings.AppSettings["Constring"].ToString();
MySqlConnection conn = new MySqlConnection(strcon);
MySqlCommand cmd = new MySqlCommand("Select * From items order by ItemID", conn);
conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();

if (reader.HasRows)
{
   dataGridView1.Visible = true;
   DataTable dt = null;
   dt = new DataTable();
   dt.Load(reader);
   dataGridView1.DataSource = dt;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文