我试图将accessDatabase中的特定单元格数据写入数据表。我使用SELECT查询获取此数据,但是当将数据写入数据表中的特定单元格时,循环在迭代甚至一次之前中断,此时它应该迭代两次。
以下是我使用的代码:
DataTable dt = new DataTable(); // Creates Table
dt.Clear();
dt.Columns.Add("Hol/Abs", typeof(string)); // Column 0
dt.Columns.Add("FirstDay", typeof(DateTime)); // Column 1
dt.Columns.Add("LastDay", typeof(DateTime)); // Column 2
dt.Columns.Add("TotalDays", typeof(int)); // Column 3
dt.Columns.Add("Reason", typeof(string)); // Column 4
LblName.Text = PassName; //Loads Name
string ConnString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\HoliPlanData.accdb;Persist Security Info=False";
string Query = "SELECT PayrollNo FROM [Employee] WHERE (FirstName + ' ' + LastName) = @Name";
string CountQuery = "SELECT COUNT(*) FROM [Holiday]";
string GetStartQuery = "SELECT StartDate FROM [Holiday] WHERE PayrollNo = @PayrollNo";
string GetEndQuery = "SELECT EndDate FROM [Holiday] WHERE PayrollNo = @PayrollNo";
string GetReasonQuery = "SELECT Reason FROM [Holiday] WHERE PayrollNo = @PayrollNo";
using (OleDbConnection conn = new OleDbConnection(ConnString))
using (OleDbCommand GetPayroll = new OleDbCommand(Query, conn))
{
conn.Open();
GetPayroll.Parameters.Add("@Name", OleDbType.VarChar).Value = LblName.Text;
int GotPayroll = Convert.ToInt32(GetPayroll.ExecuteScalar()); //Uses Query to Get PayrollNo
OleDbCommand CountRowsInHol = new OleDbCommand(CountQuery, conn);
CountRowsInHol.Parameters.AddWithValue("@PayrollNo", OleDbType.Integer).Value = GotPayroll;
int HolidayCount = (int) (CountRowsInHol.ExecuteScalar()); //Uses CountQuery to Get HowMany lines are in [Holiday]
int LoopCount = 0;
while (LoopCount <= HolidayCount) //Will go though all Lines in [Holiday]
{
OleDbCommand GetStart = new OleDbCommand(GetStartQuery, conn);
OleDbCommand GetEnd = new OleDbCommand(GetEndQuery, conn);
OleDbCommand GetReason = new OleDbCommand(GetReasonQuery, conn);
GetStart.Parameters.Add("@PayrollNo", OleDbType.Integer).Value = GotPayroll;
GetEnd.Parameters.Add("@PayrollNo", OleDbType.Integer).Value = GotPayroll;
GetReason.Parameters.Add("@PayrollNo", OleDbType.Integer).Value = GotPayroll;
DateTime StartHold = Convert.ToDateTime(GetStart.ExecuteScalar());
DateTime EndHold = Convert.ToDateTime(GetEnd.ExecuteScalar());
string ReasonHold = (GetReason.ExecuteScalar()).ToString();
dt.Rows[LoopCount]["FirstDay"] = StartHold;
dt.Rows[LoopCount]["LastDay"] = EndHold;
dt.Rows[LoopCount]["Reason"] = ReasonHold;
LoopCount = LoopCount + 1;
}
}
我知道它很长但很基本。代码中断了..
dt.Rows [LoopCount] [&#34; FirstDay&#34;] = StartHold;
..而不是写入数据表。 任何帮助或建议都将不胜感激,我也非常乐意明确有关格式化代码的建议,以便更容易阅读和更普遍的标准。
答案 0 :(得分:0)
感谢帮助,CrowCoder让我朝着正确的方向前进。我从不添加行!使用这个代码一切都很棒,所以希望其他noobie程序员不会像我一样长时间陷入困境!
使用以下
替换自 dt.rows [LoopCount]DataRow NewLine = dt.NewRow();
NewLine["Hol/Abs"] = "Holiday";
NewLine["FirstDay"] = StartHold;
NewLine["LastDay"] = EndHold;
NewLine["TotalDays"] = (EndHold - StartHold);
NewLine["Reason"] = ReasonHold;
dt.Rows.Add(NewLine);
答案 1 :(得分:-1)
不完全确定,但替换时可能是一个小错误:
dt.Rows[LoopCount]["FirstDay"] = StartHold;
使用:
dt.Rows[LoopCount].["FirstDay"] = StartHold;
也许我错了,但只是想知道dt.Rows [0]是否包含字段[“FirstDay”]。