尝试保存数据库记录时出现循环中的逻辑错误
本文关键字:循环 错误 保存 数据库 记录 | 更新日期: 2023-09-27 18:20:24
我正在尝试使用.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;
}
}
}
为什么不尝试For循环??
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 + " ";
}
替换while(inc!=最大行数-1){
与for(int inc=0;inc<MaxRows;inc++){