我想读取某个表中的所有字段下面这个方法对吗要是错了应该怎么改啊!
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 = "";
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;
}
我也没调试过,你试试看吧。
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;
}
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();
}
如果数据量很大的话建议使用 StringBulider进行字符拼接。