I am trying to connect to a database in C# and bring up certain data points. The database has numerous columns and tables and I simply want to display them in the console using Writeline(). Below is what I have so far. The code runs without error but does not display anything either.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data.Sql;
namespace SQLIntro
{
class Program
{
static void Main(string[] args)
{
using (SqlConnection connection = new SqlConnection("Server=[SQ_SIXTEEN];Database=[PocketCentral];User ID=[un];Password=[pw];Trusted_Connection=true"))
{
connection.Open();
using (SqlCommand command = new SqlCommand("SELECT * FROM tbl_Terminal", connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.WriteLine(reader.GetValue(i));
}
Console.WriteLine();
}
}
}
}
}
}
}
The one thing is where the column info would go...The SQL command? or in the while loop?.
SELECT * FROM tbl_Terminalreturn any records?where the column info would go<= You should write this out hard coded (or from code anyway). Sql queries executed against a relational database should be executed knowing which columns will be returned. Usingselect *is considered bad practice, the schema could change (column added, columns moved, ...) instead specify the columns and if you want to display where the data is originating from in the app then code that in.