我正在尝试使用.net读取数据库的所有记录,而我在while循环中有一个逻辑错误,我无法理解问题是什么,如果有人可以看看我会非常感激
{
System.Data.OleDb.OleDbConnection con;
DataSet dsl;
System.Data.OleDb.OleDbDataAdapter da;
public String accessDatabase()
{
initializecomponent();
con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Owner\\Documents\\CIS3052.mdb";
dsl = new DataSet();
String displayID = null;
String displayFname = null;
String displayLname = null;
String displayAge = null;
String displayJob = null;
String viewAll = null;
int inc = 0;
int MaxRows = 0;
string sql = "SELECT * FROM Employee";
da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
con.Open();
da.Fill(dsl, "Employee");
MaxRows = dsl.Tables["Employee"].Rows.Count;
while (inc != MaxRows -1)
{
DataRow dRow = dsl.Tables["Employee"].Rows[inc];
displayID = dRow.ItemArray.GetValue(0).ToString();
displayFname = dRow.ItemArray.GetValue(1).ToString();
displayLname = dRow.ItemArray.GetValue(2).ToString();
displayAge = dRow.ItemArray.GetValue(3).ToString();
displayJob = dRow.ItemArray.GetValue(4).ToString();
viewAll = viewAll + displayID + " " + displayFname + " " + displayLname + " " + displayAge + " " + displayJob + " ";
}
con.Close();
con.Dispose();
return viewAll;
}
}
}
答案 0 :(得分:1)
为什么不尝试使用For-Loop ??
for(int i = 0; i < MaxRows; i++)
{
DataRow dRow = dsl.Tables["Employee"].Rows[i];
displayID = dRow.ItemArray.GetValue(0).ToString();
displayFname = dRow.ItemArray.GetValue(1).ToString();
displayLname = dRow.ItemArray.GetValue(2).ToString();
displayAge = dRow.ItemArray.GetValue(3).ToString();
displayJob = dRow.ItemArray.GetValue(4).ToString();
viewAll = viewAll + displayID + " " + displayFname + " " + displayLname + " " + displayAge + " " + displayJob + " ";
}
答案 1 :(得分:0)
替换 while(inc!= MaxRows -1) {
与 for(int inc = 0; inc&lt; MaxRows; inc ++) {