I need to develop a program that saves the result of a select
in two variables and saves the data in a txt and shows the resulting iteration of the urls on the console
I was able to get it to print correctly in the console, could you tell me how to save the result of the iteration in a txt, that for each line a url with a line break is written.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string url1 = null;
SqlConnection con = new SqlConnection("server=SQL2014;database=ventas;integrated security = true");
string strSQL = "select Numero,Terreno "+
"FROM datos";
SqlCommand cmd = new SqlCommand(strSQL, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string resultadoNumero = reader["Numero"].ToString();
string resultadoTerreno = reader["Terreno"].ToString();
url1 = "http://ReportServer/Pages/ReportViewer.aspx?/Pedidos" + resultadoNumero + resultadoTerreno;
Console.WriteLine(url1);
}
reader.Close();
con.Close();
Console.ReadLine();
}
}
}
You should consider the following:
Where, the variable
builder
will store all the Urls that are built and with the methodFile.WriteAllText
it will create (if it does not exist) the file and write all the text.P.S. Do not forget to make the necessary references.
Reference: