C# applications typically access SQL Server through ADO.NET. A reliable design starts with proper relational constraints, calls stored procedures using typed parameters, and manages connections predictably.
Model keys and foreign keys
Define a primary key for each record and a foreign key for relationships. SQL Server does not use the DOUBLE column type in the way some other databases do; decimal(p,s), float, or money-like types have different trade-offs.
T-SQL schema example
CREATE TABLE dbo.Customers (
CustomerID int NOT NULL PRIMARY KEY,
CustomerName nvarchar(150) NOT NULL
);
CREATE TABLE dbo.Orders (
OrderID int NOT NULL PRIMARY KEY,
CustomerID int NOT NULL REFERENCES dbo.Customers(CustomerID),
Amount decimal(12,2) NOT NULL CHECK (Amount >= 0)
);
A parameterized .NET call
using Microsoft.Data.SqlClient;
using var connection = new SqlConnection(connectionString);
using var command = new SqlCommand(
"SELECT OrderID, Amount FROM dbo.Orders WHERE CustomerID = @id",
connection);
command.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = 42;
connection.Open();
using var reader = command.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader.GetInt32(0));
Safer design
In modern projects prefer the supported Microsoft.Data.SqlClient provider, parameterize values, scope connections with using, and load secrets from secure configuration rather than source files. Explicitly verify types and lengths for production code.
AI, sources and reuse: This is an AI-generated rewrite informed by the original Open Education post (#70287), with additional explanations and examples. The new article text may be copied, adapted, and republished with or without a reference to this site. Third-party resources and trademarks keep their own rights. AI can make mistakes. Verify version support, code correctness, and security practices against Microsoft documentation, and test with nonproduction data first.
