下面的代码我没有异常错误,这可能意味着我的sql语句没有问题。但是,在尝试验证它是否返回任何值时,我将返回的项目传递给字符串值并尝试在消息框中显示它。
我面临的问题是消息框只显示列的名称,而不是表格中请求的数据。
可能是什么问题?如果有更好的解决方法,请提出建议......
public void DisplayPurchase(OleDbConnection mDB)
{
openDB();
string sqlQuery;
OleDbCommand cmd;
OleDbDataReader rdr;
sqlQuery = "SELECT CustomerTable.[Youth ID], CustomerTable.Firstname, " +
"CustomerTable.Lastname, Youth.Purchaseid, Youth.NumbersOfSport, " +
"Youth.Price, Youth.TotalCostOfTraining, Youth.PercentageDiscount, " +
"Youth.AmountDue, Youth.DatePurchased" +
" FROM CustomerTable, Youth WHERE Youth.YouthID = CustomerTable.[Youth ID]" +
" AND CustomerTable.[Youth ID] = 7";
try
{
cmd = new OleDbCommand(sqlQuery, mDB);
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
qtyInt1 = (int)rdr["Youth.NumbersOfSport"];
youthInt1 = (int)rdr["CustomerTable.[Youth ID]"];
firstStr1 = (string)rdr["CustomerTable.Firstname"];
purStr1 = (string)rdr["Youth.Purchaseid"];
lastStr1 = (string)rdr["CustomerTable.Lastname"];
priceStr1 = (string)rdr["Youth.Price"];
totalCstStr1 = (string)rdr["Youth.TotalCostOfTraining"];
discountStr1 = (string)rdr["Youth.PercentageDiscount"];
amtDueStr1 = (string)rdr["Youth.AmountDue"];
//purDate1 = (DateTime)rdr["Youth.DatePurchased"];
MessageBox.Show(firstStr1.ToString());
closeDB();
}
else
{
MessageBox.Show("Reader has no rows");
closeDB();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
谢谢
答案 0 :(得分:3)
我很确定你必须先调用rdr.Read()才能访问它的任何数据。因此,将其添加为if之后的第一行(rdr.HasRows())
答案 1 :(得分:1)
您需要在阅读器上调用Read()以读取第一行。
if(rdr.HasRows) {
rdr.Read();
...
祝你好运,答案 2 :(得分:0)
你试过了吗?
firstStr1 = (string)rdr["Firstname"];
datareader中的字段名称通常不包含表名前缀。
答案 3 :(得分:0)
检索数据时删除表名:
qtyInt1 = (int)rdr["NumbersOfSport"];
youthInt1 = (int)rdr["Youth ID"]; // You may need to rename this one in the query
firstStr1 = (string)rdr["Firstname"];
purStr1 = (string)rdr["Purchaseid"];
lastStr1 = (string)rdr["Lastname"];
priceStr1 = (string)rdr["Price"];
totalCstStr1 = (string)rdr["TotalCostOfTraining"];
discountStr1 = (string)rdr["PercentageDiscount"];
amtDueStr1 = (string)rdr["AmountDue"];
答案 4 :(得分:0)
令人惊讶的是这些东西的运作方式...... 我首先要感谢所有为解决这个问题做出贡献的人。非常感谢所发布的每一个字母。
sqlQuery =“SELECT Youth.YouthID,Firstname,Lastname,NumbersOfSport,Price,TotalCostOFTraining,PercentageDiscount,Purchaseid,AmountDue,DatePurchased FROM CustomerTable,Youth WHERE CustomerTable.YouthID = Youth.YouthID AND Youth.YouthID =”+ toSql(youthInt) );
try
{
cmd = new OleDbCommand(sqlQuery, mDB);
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
rdr.Read();
qtyInt1 = (int)rdr["NumbersOfSport"];
youthInt1 = (int)rdr["YouthID"];
firstStr1 = (string)rdr["Firstname"];
purInt1 = (int)rdr["Purchaseid"];
lastStr1 = (string)rdr["Lastname"];
priceStr1 = (string)rdr["Price"];
totalCstStr1 = (string)rdr["TotalCostOfTraining"];
discountStr1 = (string)rdr["PercentageDiscount"];
amtDueStr1 = (string)rdr["AmountDue"];
purDate1 = (DateTime)rdr["DatePurchased"];
closeDB();
}
else
{
MessageBox.Show("Reader has no rows");
closeDB();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我不得不打电话给rdr.read();函数以及引用数据库列的表,每个帮助来自不同的来源..... 棒极了... 谢谢大家...