Let’s say I have a form in C# containing textboxes that correspond to fields in a SQL Server database table, and I want to extract data from the table and populate the textboxes.
From VS2005, I am familiar with the following code pattern:
command = conn.CreateCommand();
command.CommandText = "SELECT * FROM Customer WHERE ID = " + CustId;
reader = command.ExecuteReader();
FormCustomer.textBoxName = reader.GetString(reader.GetOrdinal("Name"));
FormCustomer.textBoxAddress = reader.GetString(reader.GetOrdinal("Address"));
FormCustomer.textBoxSuburb = reader.GetString(reader.GetOrdinal("Suburb"));
What is the currently accepted way to do this in C# 4.0 using VS2010?
Well the way you’ve got it is certainly one option, although you should be using parameterized SQL instead of creating SQL on the fly. (I hope you’ve also got appropriate
usingstatements to close connections etc).Other options:
DataTableinstead (still basic ADO.NET)That’s just off the top of my head. There are bound to be more.