从sql表中读取数据 [英] reading data from sql table

查看:137
本文介绍了从sql表中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从sql数据库中检索数据。我有以下代码。

有人可以向我指出我出错的地方或地点。当通过代码使用F10踩到
时它停在while(reader.Read())



 SqlConnection cn =  new  SqlConnection( @  Server = WD\\ \\ S789; Database = WinDI; User Id =; Password =;); 
SqlCommand cmd = new SqlCommand( SELECT区域,国家,书籍,音频来自Vand,cn);
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();
{
while (reader.Read())
{
catalog(reader [ zone]。ToString(),reader [ country]。ToString(),reader [ book]。ToString(),
reader [ audio]。ToString( ));
}
}
cn.Close();

解决方案

好吧,SELECT可能会花费一段时间,或者SQL Server实例非常繁忙,或者你有网络困难。

你的选择不会试图限制它返回的记录数量,而名称audio意味着这很可能是一大块数据。

尝试进行一些更改以隔离问题:

 使用(SqlConnection cn =  new  SqlConnection( @  Server = WD\S789; Database = WinDI;用户ID =;密码=;))
{
SqlCommand cmd = SqlCommand( SELECT区域FROM Vand WHERE ID< 10,cn);
cn.Open();
使用(SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader [ 。区]的ToString());
}
}
}

改变WHERE条件以适应您的数据库,但减少它返回的行数。

看看那是什么确实如此。



顺便说一句:如果那是你的生产SQL服务器,你 肯定 不应该使用它测试<!/ BLOCKQUOTE>

I am attempting to retrieve data from an sql database. I have the following piece of code.
Can somebody kindly point out to me what or where I have gone wrong. When stepping
through the code with F10 it stops at while(reader.Read())

SqlConnection cn = new SqlConnection(@"Server=WD\S789;Database=WinDI;User Id=;Password=;");
         SqlCommand cmd = new SqlCommand("SELECT zone,country,book,audio FROM Vand ", cn);
         cn.Open();
         SqlDataReader reader = cmd.ExecuteReader();
         {
             while (reader.Read())
             {
                 catalog(reader["zone"].ToString(), reader["country"].ToString(), reader["book"].ToString(),
                         reader["audio"].ToString());
             }
         }
         cn.Close();

解决方案

Well, it's possible that the SELECT is taking a fair while, or the SQL Server instance is pretty busy, or you have network difficulties.
Your select doesn't try to limit the number of records it returns, and the name "audio" implies that that is quite possibly a large chunk of data.
Try some changes to isolate the problem:

using (SqlConnection cn = new SqlConnection(@"Server=WD\S789;Database=WinDI;User Id=;Password=;"))
    {
    SqlCommand cmd = new SqlCommand("SELECT zone FROM Vand WHERE ID < 10", cn);
    cn.Open();
    using (SqlDataReader reader = cmd.ExecuteReader())
        {
        while (reader.Read())
            {
            Console.WriteLine(reader["zone"].ToString());
            }
        }
    }

Altering the WHERE condition to suit your DB but reduce the number of rows it returns.
See what that does.

BTW: if that's your production SQL server, you definately shouldn't be using it for testing!


这篇关于从sql表中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆