使用if else循环方法使用以前创建的数据表填充数据表

时间:2012-10-29 09:09:01

标签: c#

作为c#中的newby,我遇到了从其他数据表中填充数据表的问题,该数据表已经构建了内存。并非新数据表中需要所有列。我可以在这里得到一些帮助吗。

这是我到目前为止的代码:我找到了解决方案的一部分。但不知道该怎么办?

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;

public partial class _Planning : System.Web.UI.Page
{
ConnectionStringSettings Leerstoftabel;
SqlConnection connection;
int cursusID = 2;
DataTable LeerstofTabel;
DataTable Planning;


protected void Page_Load(object sender, EventArgs e)
{
    Leerstoftabel = ConfigurationManager.ConnectionStrings["CursusPlanner"];
    connection = new SqlConnection(Leerstoftabel.ConnectionString);

    SqlCommand cmd = connection.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT cursus.naam AS [Cursus Naam], vak.vaknaam, cursusvak.volgnummer, les.dagnummer, les.hoofdstuknummer, les.naam AS [Les Naam], les.lesomschrijving FROM cursus INNER JOIN cursusvak ON cursus.cursusID = cursusvak.cursusID INNER JOIN vak ON cursusvak.vakID = vak.vakID INNER JOIN les ON vak.vakID = les.vakID WHERE(cursus.cursusID = @cursusID)ORDER BY cursusvak.volgnummer";

    //int cursusID = Convert.ToInt32(DropDownList1.SelectedValue);
    cmd.Parameters.Add("@cursusID", SqlDbType.Int, 0).Value = cursusID;

    connection.Open();

    //het creeëren van een datatabel
    DbDataReader rdr = cmd.ExecuteReader();
    LeerstofTabel = new DataTable();
    LeerstofTabel.Load(rdr);

    connection.Close();
}


protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    // weekend days can't be selected in calendar
    if (e.Day.IsWeekend)
        e.Day.IsSelectable = false;
}

protected void Button1_Click(object sender, EventArgs e)
{
    //create the DataTable named "Planning"
    Planning = new DataTable ("Planning");

    //Add the DataColumn using defaults
    DataColumn datum = new DataColumn("Datum");
    datum.DataType = typeof(DateTime);
    datum.AllowDBNull = false;
    datum.Caption = "Datum";
    Planning.Columns.Add(datum);

    //Add the DataColumn using defaults
    DataColumn hoofdstuk = new DataColumn("Hoofdstuk");
    hoofdstuk.AllowDBNull = false;
    hoofdstuk.Caption = "Hoofdstuk";
    Planning.Columns.Add(hoofdstuk);

    //Add the DataColumn using defaults
    DataColumn omschrijving = new DataColumn("Omschrijving");
    omschrijving.AllowDBNull = false;
    hoofdstuk.Caption = "Omschrijving";
    Planning.Columns.Add(omschrijving);

    //Add the DataColumn using defaults
    DataColumn lessen = new DataColumn("Lessen");
    lessen.AllowDBNull = false;
    lessen.Caption = "Lessen";
    Planning.Columns.Add(lessen);


    for (int i = 0; i < LeerstofTabel.Rows.Count; i++)
    {
        if (LeerstofTabel ["vaknaam"]).ToString() != vorigvak  // this statement doesn't work
        {
            DataRow newplannning = Planning.NewRow();
            newplannning["Datum"] = "";
            newplannning["Hoofdstuk"] = "*" + LeerstofTabel["vaknaam"];
            newplannning["Omschrijving"] = "";
            newplannning["Lessen"] = "";
            Planning.Rows.Add(newplannning);

        }

        Vorigvak = (LeerstofTabel["vaknaam"])ToString();
    }


    GridView1.DataSource = Planning;
    GridView1.DataBind();

3 个答案:

答案 0 :(得分:0)

使用带有主数据表的循环,并检查哪些列应导入新数据表。然后复制数据。

答案 1 :(得分:0)

如果您对复制感兴趣,可以这样做

DataTable newTable;
newTable= OldTable.Copy();

答案 2 :(得分:0)

使用DataTable的一种方法是使用:

int index; //the index of column u want to remove
string columnName; // name of the column you want to remove
DataTable dt;
dt= dtMain.Copy();
dt.Columns.RemoveAt(index); //or
dt.Columns.Remove(columnName);

使用删除或删除您删除不需要的列。

其他方法是使用对象列表并从数据表中复制数据。 例如:

List<SomeClass> info;
foreach (DataRow dr in dt.Rows)
{
info = new SomeClass { para1 = dr["col1"], para2 = dr["col2"], para3 = dr["col3"]};
}
选择哪种适合您的情况。