从列表动态C#添加到SQL表

时间:2016-08-09 03:20:21

标签: c# visual-studio-2010

我有以下代码:

protected void btnGetData_Click(object sender, EventArgs e)
        {
            clearGrid();

            bool isValid = true;
            DateTime fromDate, toDate;

            DateTime.TryParse(txtFromDate.Text, out fromDate);
            DateTime.TryParse(txtToDate.Text, out toDate);

            if (String.IsNullOrEmpty(txtTag1.Text) && String.IsNullOrEmpty(txtTag2.Text) && String.IsNullOrEmpty(txtTag3.Text) &&
                String.IsNullOrEmpty(txtTag4.Text) && String.IsNullOrEmpty(txtTag5.Text))
            {
                ltErrorMessage.Text = "Tag cannot empty";
                isValid = false;
            }

            if (fromDate == DateTime.MinValue || toDate == DateTime.MinValue)
            {
                ltErrorMessage.Text = "Not valid date";
                isValid = false;
            }

            if (toDate < fromDate)
            {
                ltErrorMessage.Text = "To Date cannot be earlier than From Date";
                isValid = false;
            }

            if (isValid)
            {
                TimeSpan ts = new TimeSpan(1, 0, 0);

                if (ddlTimeSpan.SelectedValue == "d")
                {
                    ts = new TimeSpan(Convert.ToInt32(txtTimeSpan.Text), 0, 0, 0);
                }
                else if (ddlTimeSpan.SelectedValue == "h")
                {
                    ts = new TimeSpan(Convert.ToInt32(txtTimeSpan.Text), 0, 0);
                }
                else if (ddlTimeSpan.SelectedValue == "m")
                {
                    ts = new TimeSpan(0, Convert.ToInt32(txtTimeSpan.Text), 0);
                }
                else if (ddlTimeSpan.SelectedValue == "s")
                {
                    ts = new TimeSpan(0, 0, Convert.ToInt32(txtTimeSpan.Text));
                }


                var service = new PILoaderService.PILoaderServiceClient();

                try
                {
                    service.Endpoint.Address = new System.ServiceModel.EndpointAddress(pisvcendpoint);
                    service.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(pisvcuser, pisvcpassword);
                    service.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;

                    List<dynamic> PIResult = new List<dynamic>();

                    string[] data1 = null;
                    string[] data2 = null;
                    string[] data3 = null;
                    string[] data4 = null;
                    string[] data5 = null;

                    if (!String.IsNullOrEmpty(txtTag1.Text))
                    {
                        data1 = service.GetPIData(serverName, conString, fromDate, toDate, ts, txtTag1.Text);
                    }
                    if (!String.IsNullOrEmpty(txtTag2.Text))
                    {
                        data2 = service.GetPIData(serverName, conString, fromDate, toDate, ts, txtTag2.Text);
                    }
                    if (!String.IsNullOrEmpty(txtTag3.Text))
                    {
                        data3 = service.GetPIData(serverName, conString, fromDate, toDate, ts, txtTag3.Text);
                    }
                    if (!String.IsNullOrEmpty(txtTag4.Text))
                    {
                        data4 = service.GetPIData(serverName, conString, fromDate, toDate, ts, txtTag4.Text);
                    }
                    if (!String.IsNullOrEmpty(txtTag5.Text))
                    {
                        data5 = service.GetPIData(serverName, conString, fromDate, toDate, ts, txtTag5.Text);
                    }
                    if (data1.Count() == 0 && data2.Count() == 0 && data3.Count() == 0 && data4.Count() == 0 && data5.Count() == 0)
                    {
                        ltErrorMessage.Text = "Result contains no data";
                    }
                    else
                    {
                        var pointDate = fromDate;
                        foreach (var item in data1)
                        {
                            PIResult.Add(new { Date = pointDate.ToString("dd-MMM-yyyy HH:mm"), Tag = txtTag1.Text, Value = item });
                            pointDate = pointDate.Add(ts);
                        }
                        if (data2 != null)
                        {
                            pointDate = fromDate;
                            foreach (var item in data2)
                            {
                                PIResult.Add(new { Date = pointDate.ToString("dd-MMM-yyyy HH:mm"), Tag = txtTag2.Text, Value = item });
                                pointDate = pointDate.Add(ts);
                            }
                        }
                        if (data3 != null)
                        {
                            pointDate = fromDate;
                            foreach (var item in data3)
                            {
                                PIResult.Add(new { Date = pointDate.ToString("dd-MMM-yyyy HH:mm"), Tag = txtTag3.Text, Value = item });
                                pointDate = pointDate.Add(ts);
                            }
                        }
                        if (data4 != null)
                        {
                            pointDate = fromDate;
                            foreach (var item in data4)
                            {
                                PIResult.Add(new { Date = pointDate.ToString("dd-MMM-yyyy HH:mm"), Tag = txtTag4.Text, Value = item });
                                pointDate = pointDate.Add(ts);
                            }
                        }
                        if (data5 != null)
                        {
                            pointDate = fromDate;
                            foreach (var item in data5)
                            {
                                PIResult.Add(new { Date = pointDate.ToString("dd-MMM-yyyy HH:mm"), Tag = txtTag5.Text, Value = item });
                                pointDate = pointDate.Add(ts);
                            }
                        }
                    }
                    grdPIData.DataSource = PIResult.OrderBy(o => o.Date).ThenBy(o => o.Tag);
                    grdPIData.DataBind();
                }
                catch (Exception ex)
                {
                    ltErrorMessage.Text = ex.ToString();
                }
                finally
                {
                    service.Close();
                }
            }
        }

如果我查看grdPIData.DataSource = PIResult.OrderBy(o => o.Date).ThenBy(o => o.Tag);grdPIData.DataBind();,结果将绑定到gridview。我的问题是,如何将结果直接添加到SQL表?因此结果不会添加到gridview。

问题是如果txtTag1直到txtTag5被填满,那么从日期到“todate”的文本就是说一个月。结果不会出现,因为检索的记录超过100,000条。 1小时是3,600条记录。 1天,24 * 3600 = 86,400。这是1 txtTag。如果5 txtTag,那么哇。 :)

表结构为:

oDate datetimePITag varchar(25)pValue decimal(18,2)

感谢。

0 个答案:

没有答案