I have some existing code that retrieves data from a database using ADO.NET that I want to convert to linq.
What the code does is receives an SQL query via command line, executes it, returns the rows and their column names, then prints them to the screen. I would like to know how to write this code in linq.
The ENTIRE sql query MUST be given via command line since I want to limit where I select rows from. This is the only way I want it done, so unless if you have a method that can work this way, I cannot use it. No one will have access to the program aside from myself, so security is NOT an issue.
private static SqlConnection sqlConnection = new SqlConnection();
private static void OConnection()
{
sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = MyConsoleApp.Properties.Settings.Default.ConnStr;
sqlConnection.Open();
}
...
string query = Console.ReadLine();
OpenConn();
SqlCommand command = new SqlCommand(query, sqlConnection);
SqlDataReader reader = command.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write("| {0}: {1}", reader.GetName(i), reader.GetValue(i));
}
Console.WriteLine();
}
}