Task 1
Write a program that connects to a SQL database, runs a parameterised query, prints the rows, and disconnects.
Sage IT: write a program to connect to and disconnect from a SQL databaseStuck? Hints
- Which three ADO.NET types do you need?
- How do you make sure the connection is closed even if the query throws?
- Where does the value for the WHERE clause go, if not into the SQL string?
Show a solution
Lending.sln · where this file sits
using(orawait using) on connection, command and reader.- A parameter with its type (
Add(..., SqlDbType.Decimal)), never string concatenation; can say why can hurt. - Async calls: , , .
- Connection string from configuration, not hard-coded with a password.
- Can say what "disconnect" really does: back to the pool.