首页 新闻 会员 周边 捐助

急!读取字段!asp.net

0
[已关闭问题]

 我想读取某个表中的所有字段下面这个方法对吗要是错了应该怎么改啊! 

public string  QeryList()
        {
            string str = 0;
            string qery = "SELECT Name ,Nums ,UnitPrice from orderList ";
            SqlDataReader reader = DBHelper.GetReadData(qery);

            if (reader.HasRows)
            { reader.Read();
                 str += "\n 物品名:" + reader["Name"].ToString();
                 str += "\n 数量:"+reader["Nums"].ToString();
                 str += "\n 数量:"+reader["UnitPrice"].ToString();  }
            return str;

        }

我调用这个方法在lable上显示lable.text=o.QeryList();

问题补充: string str ="";
沐紫菀的主页 沐紫菀 | 初学一级 | 园豆:1
提问于:2009-12-22 11:03
< >
分享
其他回答(2)
0

 

代码
public string QeryList()
{
string str = ""
;
string qery = "SELECT Name ,Nums ,UnitPrice from orderList "
;
SqlDataReader reader
=
DBHelper.GetReadData(qery);

while
(reader.Read())//应该是while
{
str
+= "\n 物品名:" + reader["Name"
].ToString();
str
+= "\n 数量:"+reader["Nums"
].ToString();
str
+= "\n 数量:"+reader["UnitPrice"
].ToString();
       }
return
str;

}

 

 

 

 

我也没调试过,你试试看吧。

Jaryleely | 园豆:367 (菜鸟二级) | 2009-12-22 11:09
0

public string  QeryList()
        {
            string str = 0;
            string qery = "SELECT Name ,Nums ,UnitPrice from orderList ";
            SqlDataReader reader = DBHelper.GetReadData(qery);

            if (reader.HasRows)
            { 

            while(reader.Read())//多條記錄用while

            if(reader.Read())//單條記錄用while

               {
                 str += "\n 物品名:" + reader["Name"].ToString();
                 str += "\n 数量:"+reader["Nums"].ToString();
                 str += "\n 数量:"+reader["UnitPrice"].ToString(); 

              }
            return str;

        }

woody.wu | 园豆:3621 (老鸟四级) | 2009-12-22 11:30
0

public string  QeryList()
        {
            string str = 0;//很郁闷的问题 string str="";
            string qery = "SELECT Name ,Nums ,UnitPrice from orderList ";
            SqlDataReader reader = DBHelper.GetReadData(qery);

           while (reader.Read())
            {
                 str
+= "\n 物品名:" + reader["Name"
].ToString();
                 str
+= "\n 数量:"+reader["Nums"
].ToString();
                 str
+= "\n 数量:"+reader["UnitPrice"
].ToString(); 

       }
           
return
str;

        }

如果数据量很大的话建议使用 StringBulider进行字符拼接。

邢少 | 园豆:10926 (专家六级) | 2009-12-22 14:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册