将存储过程返回日期与控制台应用程序当前日期进

时间:2015-08-12 09:57:08

标签: c# xml stored-procedures console-application

我有一个存储过程,它显示如下所示的天数。

2015-08-08
2015-08-22
2015-09-05
2015-09-19
2015-10-03
2015-10-17
2015-10-31
2015-11-14
2015-11-28
2015-12-12
2015-12-26

我在Console应用程序中有一个XML文件。其中有一个根元素和一个子元素。子元素的默认值为40.如下所示,

<Hours>
  <TotalHours>40</TotalHours>
</Hours>

要求每当控制台应用程序中的当前日期与任何一个存储过程返回日期匹配时,都应将xml值从40更改为48。

2 个答案:

答案 0 :(得分:0)

试试这个

    static void Main()
    {
        DateTime currentDate = DateTime.Now.Date;            

        //Mock dataTable is used instead of stored procedure resultset
        DataTable dt = new DataTable();
        dt.Columns.Add("Date", typeof(DateTime));
        dt.Rows.Add("2015-08-11");
        dt.Rows.Add("2015-08-13");

        string xmlFile = "hours.xml";            

        var isDateFound = from row in dt.AsEnumerable()
                          where row.Field<DateTime>("Date") == currentDate
                          select row;
        if (isDateFound.Count() > 0)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFile);
            var node = doc.DocumentElement.SelectSingleNode("TotalHours");
            node.InnerText = (Convert.ToInt32(node.InnerText) + 8).ToString();
            doc.Save(xmlFile);
        }         

    }

- SJ

答案 1 :(得分:0)

这有效,

public class Class1
{
   static void Main()
   {
       SqlConnection conn = new SqlConnection(connection);
       conn.Open();
       SqlCommand cmd = new SqlCommand("calculatesaturday", conn);


       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.Add("@firstsaturday", SqlDbType.Date).Value = "2015-08-08";
       SqlDataReader reader = cmd.ExecuteReader();
       DataTable dt1 = new DataTable();
       dt1.Load(reader);
       conn.close();
       string xmlFile1 = "xmlfile.xml"; 

       DateTime currentDate1 = DateTime.Now.Date;

       var isDateFound = from row in dt1.AsEnumerable()
                         where row.Field<DateTime>("StartDate") == currentDate1
                         select row;
       if (isDateFound.Count() > 0)
       {
           XmlDocument doc = new XmlDocument();
           doc.Load(xmlFile1);
           var node = doc.DocumentElement.SelectSingleNode("TotalHours");
           node.InnerText = (Convert.ToInt32(node.InnerText) + 8).ToString();
           doc.Save(xmlFile1);
       }       
   }      

}

此处,“StartDate”是存储过程

的列名

Creadits @codeninja