I happened to write a console application to generate a file based on the number of users logged in specific time. I hope this might be useful since its interesting to work with console application after a long time.
.. I hope this makes sense for all whose looking for a starting point for writing such applications..
static void Main(string[] args)
{
String[] commandLineArgs = System.Environment.GetCommandLineArgs();
int duration = System.Convert.ToInt32(commandLineArgs[1]);
int delay = System.Convert.ToInt32(commandLineArgs[2]);
String str = commandLineArgs[3];
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=myserver;uid=sa;password=sa;Initial Catalog=AdventureWorks;";
conn.Open();
FileStream fs = File.Create("c:\\ " + str + ".log");
StreamWriter sw = new StreamWriter(fs);
while (duration > 0)
{
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [AdventureWorks].[dbo].[myTestTable]", conn);
DataSet ds = new DataSet();
StringBuilder stb = new StringBuilder();
da.Fill(ds);
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Columns.Count == 2 && i == 0)
{
sw.WriteLine(ds.Tables[0].Columns[0].ColumnName + " " + ds.Tables[0].Columns[1].ColumnName);
sw.WriteLine("=====================================");
}
string info = ds.Tables[0].Rows[i][0].ToString() + " " + ds.Tables[0].Rows[i][1].ToString();
sw.WriteLine(info);
}
sw.WriteLine();
}
}
Thread.Sleep(delay * 1000);
duration = duration - 1;
}
sw.Close();
fs.Close();
}
}
To run the application..
goto commandprompt of visual studio and change the directory to debug of the current application and type
myapplication.exe 10 10 mylog
this refers to run the exe of the application with arguments duration of 1min and delay of 10 sec and name of the file to log the needed information.
No comments:
Post a Comment