I have the following create statement:
CREATE TABLE Products
(
SKU char(255) NOT NULL PRIMARY KEY
, Supplier int FOREIGN KEY REFERENCES Suppliers(Id) NOT NULL
, Category char(255) FOREIGN KEY REFERENCES ProductsCategories(name)
, Temp char(255) FOREIGN KEY REFERENCES StorageTemperatures(name)
, Description char(255) NOT NULL UNIQUE
, Price money NOT NULL
);
I have the following C# program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace mssqlDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = "Data Source=localhost;Initial Catalog=my_playground;Integrated Security=True";
connection.Open();
Console.WriteLine("Successfully connected to database! I'M NOT DUMB");
SqlCommand command = new SqlCommand("SELECT * FROM Products", connection);
using (SqlDataReader reader = command.ExecuteReader())
{
// while there is another record present
while (reader.Read())
{
Console.WriteLine(string.Format("There are {0} columns", reader.FieldCount));
Console.WriteLine(String.Format("{0} \t | {1} \t | {2} \t | {3} \t", reader[0], reader[1], reader[2], reader[3]));
}
}
connection.Close();
}
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
}
}
If I try to access more than 3 columns it will throw an error:
System.IndexOutOfRangeException
Why does it do that and how do I fix this?
Note that the value of reader.FieldCount
is 3.