【问题标题】:I have code to write to the console from a stored procedure, however I'd like to write to a text file / Excel我有代码要从存储过程写入控制台,但是我想写入文本文件/Excel
【发布时间】:2021-08-18 14:44:55
【问题描述】:

我有以下代码从存储过程写入控制台应用程序,但是我想知道如何写入文本文件/Excel,然后发送电子邮件。

private static void ListEnrollments() **Function Name**
{
    using (SqlConnection conn = new SqlConnection("**My connection String removed**"))
    {
        using (SqlCommand cmd = new SqlCommand("Enrollments", conn))
        {               
            // Opens the database     
            conn.Open();

            // Call the stored procedure
            cmd.CommandType = CommandType.StoredProcedure; 

            // read data returned from the stored procedure
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                // Select the column from the returned dataset 
                String Message = reader.GetString("Message");

                // Write to the console 
                Console.WriteLine($"{Message}");
            }

            conn.Close();
        }                
    }
}

【问题讨论】:

    标签: sql-server stored-procedures console-application writetofile


    【解决方案1】:

    我已经完成了下面的代码,它似乎写入了一个测试文件,这是最好的首选方式吗?

                    while (reader.Read())
                    {
                        FileStream ostrm;//Provides a stream, allows to read and write
                        StreamWriter writer;//write characters to the stream
                        TextWriter oldOut = Console.Out;//writes a series of characters = displays output
                        try
                        {
                            ostrm = new FileStream("./EnrollmentsTest.txt", FileMode.OpenOrCreate, FileAccess.Write);//Initialises a new Instance of the FilesStream Class with Path and the allows it to write
                            writer = new StreamWriter(ostrm);//Writes to the FileStream Class
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Cannot open Redirect.txt for writing");
                            Console.WriteLine(e.Message);
                            return;
                        }
    

    【讨论】:

      猜你喜欢
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多