3

我正在做一个项目,我想向数据表中添加行,如果我从数据库中获取两行,则只有两行中的一个被添加到数据表中,但我希望两者都添加到数据表中即数据表无法保存第一行的数据,数据被第二行的数据覆盖。该方法的代码是。这是我的呼吁声明。

    DataTable dttable = new DataTable();
    dttable = gettable(dtgreater, dtcurrentdate);
    public DataTable gettable(List<DateTime> objct1, DateTime objct2)
    {

    DataTable data=null;
    for (int j = 0; j < dtgreater.Count; j++)
    {
        sql = "select library_issue.STUDENTCODE,library_issue.studentname,library_book.bookname,library_issue.issuedate,library_issue.returndate from library_issue             join library_book on library_book.book_id = library_issue.book_id where library_issue.returndate ='" + objct1[j].ToString("dd/MM/yyyy") + "'";
        ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
        Label1.Text = (ds.Tables[0].Rows.Count).ToString();
        data = new DataTable();
        data.Columns.Add("STUDENTCODE", typeof(int));
        data.Columns.Add("Studentname", typeof(string));
        data.Columns.Add("Bookname", typeof(string));
        data.Columns.Add("Issuedate", typeof(string));
        data.Columns.Add("Returndate", typeof(string));
        data.Columns.Add("NO of Days Exceeded", typeof(string));
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            TimeSpan ts = objct1[j] - objct2;
            Label1.Text = ts.ToString("dd");
            data.Rows.Add(ds.Tables[0].Rows[i]["STUDENTCODE"], ds.Tables[0].Rows[i]["studentname"], ds.Tables[0].Rows[i]["bookname"], ds.Tables[0].Rows[i]["issuedate"], ds.Tables[0].Rows[i]["returndate"], ts.ToString("dd"));

        }

    }

     return data;
} 

更新:

   public DataTable gettable(List<DateTime> objct1, DateTime objct2)
    {

        DataTable data = new DataTable();

        data.Columns.Add("STUDENTCODE", typeof(int));
        data.Columns.Add("Studentname", typeof(string));
        data.Columns.Add("Bookname", typeof(string));
        data.Columns.Add("Issuedate", typeof(string));
        data.Columns.Add("Returndate", typeof(string));
        data.Columns.Add("NO of Days Exceeded", typeof(string));
    for (int j = 0; j < dtgreater.Count; j++)
    {
        sql = "select library_issue.STUDENTCODE,library_issue.studentname,library_book.bookname,library_issue.issuedate,library_issue.returndate from library_issue             join library_book on library_book.book_id = library_issue.book_id where library_issue.returndate ='" + objct1[j].ToString("dd/MM/yyyy") + "'";
        ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
        Label1.Text = (ds.Tables[0].Rows.Count).ToString();

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            TimeSpan ts = objct1[j] - objct2;
            Label1.Text = ts.ToString("dd");
            data.Rows.Add(ds.Tables[0].Rows[i]["STUDENTCODE"], ds.Tables[0].Rows[i]["studentname"], ds.Tables[0].Rows[i]["bookname"], ds.Tables[0].Rows[i]["issuedate"], ds.Tables[0].Rows[i]["returndate"], ts.ToString("dd"));

        }

    }

     return data;
} 

描述:当我在 for 循环中初始化数据表时,每次都会创建新的数据表实例。结果我每次只得到一行。

4

6 回答 6

4

您正在循环内创建数据表,因此在第二次迭代中,它将丢弃包含第一项的第一个数据表并为第二项创建一个新的空表。

创建数据表并将列添加到循环之前:

DataTable dttable = new DataTable();
dttable = gettable(dtgreater, dtcurrentdate);

public DataTable gettable(List<DateTime> objct1, DateTime objct2)
{

  DataTable data = new DataTable();
  data.Columns.Add("STUDENTCODE", typeof(int));
  data.Columns.Add("Studentname", typeof(string));
  data.Columns.Add("Bookname", typeof(string));
  data.Columns.Add("Issuedate", typeof(string));
  data.Columns.Add("Returndate", typeof(string));
  data.Columns.Add("NO of Days Exceeded", typeof(string));
  for (int j = 0; j < dtgreater.Count; j++) {
    sql = "select library_issue.STUDENTCODE,library_issue.studentname,library_book.bookname,library_issue.issuedate,library_issue.returndate from library_issue             join library_book on library_book.book_id = library_issue.book_id where library_issue.returndate ='" + objct1[j].ToString("dd/MM/yyyy") + "'";
    ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
    Label1.Text = (ds.Tables[0].Rows.Count).ToString();
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++) {

        TimeSpan ts = objct1[j] - objct2;
        Label1.Text = ts.ToString("dd");
        data.Rows.Add(ds.Tables[0].Rows[i]["STUDENTCODE"], ds.Tables[0].Rows[i]["studentname"], ds.Tables[0].Rows[i]["bookname"], ds.Tables[0].Rows[i]["issuedate"], ds.Tables[0].Rows[i]["returndate"], ts.ToString("dd"));

    }

  }

  return data;
}
于 2013-04-17T12:12:16.873 回答
1

将表创建放在循环之外。

DataTable data = new DataTable();

使用这样的可能会更好foreach

例如:

foreach (var dr in ds.Tables.First().Rows)
{
   string studentCode = dr["STUDENTCODE"].ToString();
}
于 2013-04-17T12:15:06.183 回答
0

删除行: data = new DataTable();

在 For 循环外创建数据表,如下所示...

    DataTable data = new DataTable();
    data.Columns.Add("STUDENTCODE", typeof(int));
    data.Columns.Add("Studentname", typeof(string));
    data.Columns.Add("Bookname", typeof(string));
    data.Columns.Add("Issuedate", typeof(string));
    data.Columns.Add("Returndate", typeof(string));
    data.Columns.Add("NO of Days Exceeded", typeof(string))
    for (int j = 0; j < dtgreater.Count; j++)
     {

它会解决你的问题...

于 2013-04-17T12:16:31.063 回答
0

你可以在这里做的是

    DataTable dttable = new DataTable();
    dttable = gettable(dtgreater, dtcurrentdate);
    public DataTable gettable(List<DateTime> objct1, DateTime objct2)
    {

    DataTable data=null;
    for (int j = 0; j < dtgreater.Count; j++)
    {
        sql = "select library_issue.STUDENTCODE,library_issue.studentname,library_book.bookname,library_issue.issuedate,library_issue.returndate from library_issue             join library_book on library_book.book_id = library_issue.book_id where library_issue.returndate ='" + objct1[j].ToString("dd/MM/yyyy") + "'";
        ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
        Label1.Text = (ds.Tables[0].Rows.Count).ToString();
       if(data.Columns.count==0)
        {
        data = new DataTable();
        data.Columns.Add("STUDENTCODE", typeof(int));
        data.Columns.Add("Studentname", typeof(string));
        data.Columns.Add("Bookname", typeof(string));
        data.Columns.Add("Issuedate", typeof(string));
        data.Columns.Add("Returndate", typeof(string));
        data.Columns.Add("NO of Days Exceeded", typeof(string));
        }

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            TimeSpan ts = objct1[j] - objct2;
            Label1.Text = ts.ToString("dd");
            data.Rows.Add(ds.Tables[0].Rows[i]["STUDENTCODE"], ds.Tables[0].Rows[i]["studentname"], ds.Tables[0].Rows[i]["bookname"], ds.Tables[0].Rows[i]["issuedate"], ds.Tables[0].Rows[i]["returndate"], ts.ToString("dd"));

        }

    }

     return data;
} 

我想这对你有用

于 2013-04-17T12:21:42.927 回答
0
DataTable dttable = new DataTable();
dttable = gettable(dtgreater, dtcurrentdate);
public DataTable gettable(List<DateTime> objct1, DateTime objct2)
{
    DataTable data=null;
    sql = "select library_issue.STUDENTCODE,library_issue.studentname,library_book.bookname,library_issue.issuedate,library_issue.returndate from library_issue             join library_book on library_book.book_id = library_issue.book_id where library_issue.returndate ='" + objct1[j].ToString("dd/MM/yyyy") + "'";
    ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
    Label1.Text = (ds.Tables[0].Rows.Count).ToString();


for (int j = 0; j < dtgreater.Count; j++)
{
   if(data.Columns.count==0)
    {
    data = new DataTable();
    data.Columns.Add("STUDENTCODE", typeof(int));
    data.Columns.Add("Studentname", typeof(string));
    data.Columns.Add("Bookname", typeof(string));
    data.Columns.Add("Issuedate", typeof(string));
    data.Columns.Add("Returndate", typeof(string));
    data.Columns.Add("NO of Days Exceeded", typeof(string));
    }

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {

        TimeSpan ts = objct1[j] - objct2;
        Label1.Text = ts.ToString("dd");
        data.Rows.Add(ds.Tables[0].Rows[i]["STUDENTCODE"], ds.Tables[0].Rows[i]["studentname"], ds.Tables[0].Rows[i]["bookname"], ds.Tables[0].Rows[i]["issuedate"], ds.Tables[0].Rows[i]["returndate"], ts.ToString("dd"));

}
 return data;
}
于 2015-03-26T14:53:22.197 回答
0
Var.GRV.Columns.Add("NAME COLUMN");
            Var.GRV.Columns.Add("NAME COLUMN");
            Var.GRV.Columns.Add("*");
            Var.GRV.Columns.Add("*");
            Var.GRV.Columns.Add("*");
于 2017-06-20T19:04:14.320 回答