什么是正确的循环控制结构以提供正确的输出?

时间:2016-08-29 18:04:57

标签: c# datatable system.data.datatable

我正在尝试准备一个调度算法,其中我使用了3个DataTables来保存讲师详细信息面板详细信息和大厅可用性详细信息。我使用for循环来访问DataTables。我发现for循环控制结构在这种情况下无法帮助我提供正确的输出

这是我创建表的SQL语句

create table Lecturer_availability(
lecid int,
name varchar(100),
dat1 varchar(100),
time1 varchar(100),
dat2 varchar(100),
time2 varchar(100),
dat3 varchar(100),
time3 varchar(100),
)

create table panel(
groupid int,
e1 int,     //examiner 1
e2 int,     //examiner 2
sup int,    //supervisor   
)

create table Location_availability(
hname varchar(100),
dat1 varchar(100),
time1 varchar(100),
dat2 varchar(100),
time2 varchar(100),
dat3 varchar(100),
time3 varchar(100),
)


create table Schedule(
Location varchar(100),
Group_ID varchar(100),
Date varchar(100),
Time_slot varchar(100),
Examiner_1 varchar(100),
Examiner_2 varchar(100),
Supervisor varchar(100),
)

在c#代码中我创建3个DataTable并使用for循环我将讲座可用日期和时间与大厅可用日期和时间相匹配,并将已验证的记录插入到日程表中。小组桌上有讲师的讲座

这是我的算法。在这里我只考虑date1和time1

string connString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;

 //Load panel details    

            var panel = new DataTable();
            using (var da = new SqlDataAdapter("SELECT * FROM panel",connString))
            {
                da.Fill(panel);

            }

             //Load lecturer avaialability

            var lecavaial = new DataTable();
            using (var da = new SqlDataAdapter("select * from Lecturer_availability", connString))
            {
                da.Fill(lecavaial);

            }

             //Hall Availability

            var hallavaial = new DataTable();
            using (var da = new SqlDataAdapter("select * from Location_availability", connString))
            {
                da.Fill(hallavaial);

            }

 string ex1date1, ex1time1, ex2date1, ex2time1, supdate1, suptime1;
            string ex1name, ex2name, sup, hall, groupid, date, time;

            for (int j = 0; j < hallavaial.Rows.Count; j++)
            {
                for (int k = 0; k < panel.Rows.Count; k++)
                {
                    for (int i = 0; i < lecavaial.Rows.Count; i++)
                    {
                        if (panel.Rows[k]["e1"].ToString() == lecavaial.Rows[i]["lecid"].ToString())
                        {
                            ex1date1 = lecavaial.Rows[i]["dat1"].ToString();
                            ex1time1 = lecavaial.Rows[i]["time1"].ToString();

                            if (ex1date1 == hallavaial.Rows[j]["dat1"].ToString())
                            {
                                if (ex1time1 == hallavaial.Rows[j]["time1"].ToString())
                                {
                                    ex1name = lecavaial.Rows[i]["name"].ToString();
                                    Label5.Text = ex1name;


                                    if (panel.Rows[k]["e2"].ToString() == lecavaial.Rows[i]["lecid"].ToString())
                                    {
                                        ex2date1 = lecavaial.Rows[i]["dat1"].ToString();
                                        ex2time1 = lecavaial.Rows[i]["time1"].ToString();

                                        if (ex2date1 == hallavaial.Rows[j]["dat1"].ToString())
                                        {
                                            if (ex2time1 == hallavaial.Rows[j]["time1"].ToString())
                                            {
                                                ex2name = lecavaial.Rows[i]["name"].ToString();
                                                Label6.Text = ex2name;

                                                if (panel.Rows[k]["sup"].ToString() == lecavaial.Rows[i]["lecid"].ToString())
                                                {
                                                    supdate1 = lecavaial.Rows[i]["dat1"].ToString();
                                                    suptime1 = lecavaial.Rows[i]["time1"].ToString();

                                                    if (supdate1 == hallavaial.Rows[j]["dat1"].ToString())
                                                    {
                                                        if (suptime1 == hallavaial.Rows[j]["time1"].ToString())
                                                        {
                                                            sup = lecavaial.Rows[i]["name"].ToString();
                                                            Label7.Text = sup;
                                                        }

                                                    }


                                                }

                                            }

                                        }
                                    }

                                }
                            }

                        }

                        hall = hallavaial.Rows[j]["hname"].ToString();
                        groupid = panel.Rows[k]["gid"].ToString();
                        date = lecavaial.Rows[i]["dat1"].ToString();
                        time = lecavaial.Rows[i]["time1"].ToString();
                        ex1name = Label5.Text;
                        ex2name = Label6.Text;
                        sup = Label7.Text;


                        using (SqlConnection connection = new SqlConnection(connString))
                        {
                            SqlCommand cmd = new SqlCommand("INSERT INTO Schedule (Location,Group_ID,Date,Time_slot,Examiner_1,Examiner_2,Supervisor) VALUES (@loc, @gid, @date, @time, @ex1, @ex2, @sup)");
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = connection;
                            cmd.Parameters.AddWithValue("@loc", hall);
                            cmd.Parameters.AddWithValue("@gid", groupid);
                            cmd.Parameters.AddWithValue("@date", date);
                            cmd.Parameters.AddWithValue("@time", time);
                            cmd.Parameters.AddWithValue("@ex1", ex1name);
                            cmd.Parameters.AddWithValue("@ex2", ex2name);
                            cmd.Parameters.AddWithValue("@sup", sup);
                            connection.Open();
                            cmd.ExecuteNonQuery();
                        }

                    }
                }
            }

这就是我应该如何获得输出

Schedule table after comparisons

0 个答案:

没有答案