2017-05-31 86 views
0

以下是我的控制台应用程序代码以从SQL获取数据。从数据读取器向前端获取数据

 using (SqlConnection con = new SqlConnection("server=fcpcdb02; database=campus6; user id=; password=;")) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       cmd.Connection = con; 

       cmd.CommandText = "select * from [Campus6].[dbo].[TRANSCRIPTDETAIL] where FINAL_GRADE='I' and ACADEMIC_YEAR = '2017' and ACADEMIC_TERM='02TERM'"; 
       cmd.Connection.Open(); 
       using (SqlDataReader sqlRdr = cmd.ExecuteReader()) 
       { 
        // table = new DataTable(); 
        // table.Load(reader); 
        if (sqlRdr.HasRows) 
        { 
         while (sqlRdr.Read()) 
         { 
          id = (sqlRdr["PEOPLE_CODE_ID"].ToString()); 
          subname = (sqlRdr["EVENT_LONG_NAME"].ToString()); 
          eventid = (sqlRdr["EVENT_ID"].ToString()); 

          year1 = (sqlRdr["ACADEMIC_YEAR"].ToString()); 
          term = (sqlRdr["ACADEMIC_TERM"].ToString()); 
          session = (sqlRdr["ACADEMIC_SESSION"].ToString()); 
          subtype = (sqlRdr["EVENT_SUB_TYPE"].ToString()); 
          section = (sqlRdr["SECTION"].ToString()); 
          orgcode = (sqlRdr["ORG_CODE_ID"].ToString()); 
         } 

这是我的代码来生成HTML文件

TextWriter tw = File.CreateText(@"C:\Users\sganatra\Documents\Visual Studio 2015\Projects\incompletechange\files\" + year1 + "" + term + ".html"); 


          string template = System.IO.File.ReadAllText((@"C:\Users\sganatra\Documents\Visual Studio 2015\Projects\incompletechange\files\changegrade.html")); 

          tw.WriteLine("" + template + ""); 
          Console.WriteLine("Text created"); 
          tw.Close(); 
          Console.WriteLine(Console.Read()); 

现在想来我试图做的就是从数据读取器的值,并在下载之前将其存储在HTML中。我隐藏了几个选项,他们为.aspx文件工作,我得到的数据,但是当我下载它转换成html我看不到任何数据。

+0

这是您最小的,完整的和可验证的代码示例吗?通过以下https://stackoverflow.com/help/mcve任何机会改善您的问题? –

+0

aspx已经插入标签,因此它们是html。直的文本文件没有标签,您必须在软件中添加标签。看到下面发布的示例将数据表转换为html:https://stackoverflow.com/questions/9792882/creating-html-from-a-datatable-using-c-sharp – jdweng

+1

@QualityCatalyst我删除了不需要的代码。 – Naive

回答

0

您需要做的是用占位符填充模板,一旦将该模板加载到您的应用程序中,您将需要使用Replace(string, string)方法来填充空白。

请注意,您的查询可能返回大于1分的记录,你读者程序只是要通过迭代所有的记录和您的字符串变量要包含的最后一个记录的值。如果是这种情况,您可能需要重新考虑您的总体设计。如果你想写出所有这些记录,模板系统将需要在'while(Read)'循环内

在下面的不完整的例子中,我将在我的模板中的占位符包围双英镑符号(即##Placeholder##)。

// earlier code 
string template = System.IO.File.ReadAllText((@"C:\Users\sganatra\Documents\Visual Studio 2015\Projects\incompletechange\files\changegrade.html")); 

template.Replace("##id##", id); 
template.Replace("## subname##", subname); 
template.Replace("##eventid##", eventid); 
// and so on for year1 => orgcode 

tw.WriteLine("" + template + ""); 
// and the rest of your code