I have some existing code that retrieves data from a database using ADO.NET, which I want to convert to linq.
What the code does is, it receives the SQL query through the command line, executes it, returns the rows and column names, and then displays them. I would like to know how to write this code in linq.
A FULL sql query MUST be provided via the command line, since I want to limit the selection of rows. This is the only way I want to do this, so if you have a method that can work this way, I cannot use it. No one will have access to the program except me, so security is NOT a problem.
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(); } }
source share