编程语言
首页 > 编程语言> > 在c#中向Datatable添加行

在c#中向Datatable添加行

作者:互联网

我正在研究一个项目,我想向Datatable添加行,如果我从数据库中获取两行,只有两行中的一行被添加到数据表中,但我想将两者都添加到数据表中即数据表不能保存第一行数据,数据被第二行数据覆盖.
方法的代码是.
这是我的呼吁声明.

    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循环中初始化数据表时,每次都会导致创建新的datatable实例.
 结果我每次只得到一行.

解决方法:

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

创建数据表并在循环之前向其添加列:

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;
}

标签:datarow,mysql,asp-net,datatable,c-2
来源: https://codeday.me/bug/20190901/1780026.html