{"id":78933,"date":"2026-11-10T19:42:56","date_gmt":"2026-11-10T19:42:56","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78933"},"modified":"2026-11-10T19:55:30","modified_gmt":"2026-11-10T19:55:30","slug":"rewritten-sql-server-70287","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78933","title":{"rendered":"Using C# with SQL Server: Relationships, Procedures, and Safe Parameters"},"content":{"rendered":"<p>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.<\/p>\n<h2>Model keys and foreign keys<\/h2>\n<p>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.<\/p>\n<h2>T-SQL schema example<\/h2>\n<pre><code>CREATE TABLE dbo.Customers (\n CustomerID int NOT NULL PRIMARY KEY,\n CustomerName nvarchar(150) NOT NULL\n);\nCREATE TABLE dbo.Orders (\n OrderID int NOT NULL PRIMARY KEY,\n CustomerID int NOT NULL REFERENCES dbo.Customers(CustomerID),\n Amount decimal(12,2) NOT NULL CHECK (Amount &gt;= 0)\n);<\/code><\/pre>\n<h2>A parameterized .NET call<\/h2>\n<pre><code>using Microsoft.Data.SqlClient;\nusing var connection = new SqlConnection(connectionString);\nusing var command = new SqlCommand(\n    \"SELECT OrderID, Amount FROM dbo.Orders WHERE CustomerID = @id\",\n    connection);\ncommand.Parameters.Add(\"@id\", System.Data.SqlDbType.Int).Value = 42;\nconnection.Open();\nusing var reader = command.ExecuteReader();\nwhile (reader.Read())\n    Console.WriteLine(reader.GetInt32(0));<\/code><\/pre>\n<h2>Safer design<\/h2>\n<p>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.<\/p>\n<hr\/>\n<p><strong>AI, sources and reuse:<\/strong> This is an AI-generated rewrite informed by <a href=\"https:\/\/bangla.sitestree.com\/?p=70287\">the original Open Education post (#70287)<\/a>, 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. <strong>AI can make mistakes.<\/strong> Verify version support, code correctness, and security practices against Microsoft documentation, and test with nonproduction data first.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[20,182],"tags":[466],"class_list":["post-78933","post","type-post","status-publish","format-standard","hentry","category-information-technology","category---blog","tag-sql-server","item-wrap"],"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":78936,"url":"http:\/\/bangla.sitestree.com\/?p=78936","url_meta":{"origin":78933,"position":0},"title":"C# and SQL Server: From Foreign Keys to Reusable Database Commands","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"Database-backed .NET applications depend on both a correct schema and correctly parameterized application queries. This tutorial links foreign keys, stored procedures, and prepared SQL commands.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78949,"url":"http:\/\/bangla.sitestree.com\/?p=78949","url_meta":{"origin":78933,"position":1},"title":"SQL and SQL Server Interview Questions: Concepts with Short Answers","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"A good SQL Server interview review connects fundamental relational principles to practical database development, administration, and troubleshooting.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":70287,"url":"http:\/\/bangla.sitestree.com\/?p=70287","url_meta":{"origin":78933,"position":2},"title":"Random C# and MS SQl Server #.Net Web Applications","author":"Author-Check- Article-or-Video","date":"August 31, 2021","format":false,"excerpt":"Brought from: http:\/\/salearningschool.com\/displayArticle.php?table=Articles&articleID=1321&title=Random%20C#%20and%20MS%20SQl%20Server Foreign Key in Table Declaration CREATE TABLE ORDERS ( ID integer primary key, Order_Date datetime, Customer_ID integer references CUSTOMER(ID), Amount double ); ALTER TABLE ORDERS ADD FOREIGN KEY (customer_id) REFERENCES CUSTOMER(ID); Stored Procedure Example CREATE PROCEDURE [dbo].[procedure_name] @param1 VARCHAR(100) ,@param2 VARCHAR(200) OUTPUT AS BEGIN DECLARE @param3 VARCHAR(100)\u2026","rel":"","context":"In &quot;C# - Misc&quot;","block_context":{"text":"C# - Misc","link":"http:\/\/bangla.sitestree.com\/?cat=1973"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":69886,"url":"http:\/\/bangla.sitestree.com\/?p=69886","url_meta":{"origin":78933,"position":3},"title":"Random C# and MS SQl Server #19","author":"Author-Check- Article-or-Video","date":"August 21, 2021","format":false,"excerpt":"Foreign Key in Table Declaration CREATE TABLE ORDERS ( ID integer primary key, Order_Date datetime, Customer_ID integer references CUSTOMER(ID), Amount double); ALTER TABLE ORDERS ADD FOREIGN KEY (customer_id) REFERENCES CUSTOMER(ID); Stored Procedure Example CREATE PROCEDURE [dbo].[procedure_name] @param1 VARCHAR(100) ,@param2 VARCHAR(200) OUTPUT AS BEGIN DECLARE @param3 VARCHAR(100) SET @param3 = '\u2026","rel":"","context":"In &quot;C# - Misc&quot;","block_context":{"text":"C# - Misc","link":"http:\/\/bangla.sitestree.com\/?cat=1973"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5933,"url":"http:\/\/bangla.sitestree.com\/?p=5933","url_meta":{"origin":78933,"position":4},"title":"SQL (\u098f\u09b8\u0995\u09bf\u0989\u098f\u09b2) FOREIGN KEY \u0995\u09a8\u09b8\u09cd\u099f\u09cd\u09b0\u09c7\u0987\u09a8\u09cd\u099f\u09b8\u09cd. SQL Foreign Key Constraint","author":"Author-Check- Article-or-Video","date":"January 30, 2015","format":false,"excerpt":"SQL (\u098f\u09b8\u0995\u09bf\u0989\u098f\u09b2) FOREIGN KEY \u0995\u09a8\u09b8\u09cd\u099f\u09cd\u09b0\u09c7\u0987\u09a8\u09cd\u099f\u09b8\u09cd Article By: Protap Chandra FOREIGN KEY \u0995\u09a8\u09b8\u09cd\u099f\u09cd\u09b0\u09c7\u0987\u09a8\u09cd\u099f \u09ac\u09cd\u09af\u09ac\u09b9\u09be\u09b0 \u0995\u09b0\u09be \u09af\u09be\u09df \u099f\u09c7\u09ac\u09bf\u09b2\u0997\u09c1\u09b2\u09cb\u09b0 \u09ae\u09a7\u09cd\u09af\u09c7 \u0986\u09a8\u09cd\u09a4:\u09b8\u0982\u09af\u09cb\u0997 \u09af\u09be\u09a4\u09c7 \u09a8\u09b7\u09cd\u099f \u09a8\u09be \u09b9\u09df \u09a4\u09be \u09a8\u09bf\u09b6\u09cd\u099a\u09bf\u09a4 \u0995\u09b0\u09be\u09b0 \u099c\u09a8\u09cd\u09af\u0964 \u098f\u099b\u09be\u09dc\u09be FOREIGN KEY \u0995\u09a8\u09b8\u09cd\u099f\u09cd\u09b0\u09c7\u0987\u09a8\u09cd\u099f foreign key \u09af\u09c1\u0995\u09cd\u09a4 \u0995\u09b2\u09be\u09ae\u09c7 \u0985\u0997\u09cd\u09b0\u09b9\u09a3\u09af\u09cb\u0997\u09cd\u09af \u09a1\u09be\u099f\u09be \u0987\u09a8\u09aa\u09c1\u099f\u09c7 \u09a6\u09c7\u09df\u09be\u09b0 \u09b9\u09be\u09a4 \u09a5\u09c7\u0995\u09c7\u0993 \u09ac\u09cd\u09af\u09ac\u09b9\u09be\u09b0\u0995\u09be\u09b0\u09c0\u09a6\u09c7\u09b0 \u09ac\u09bf\u09b0\u09a4 \u09b0\u09be\u0996\u09c7\u0964 \u0995\u09be\u09b0\u09a3 FOREIGN KEY \u09b8\u0999\u09cd\u0997\u09be\u09df\u09bf\u09a4 \u0995\u09b2\u09be\u09ae\u09c7 \u0995\u09c7\u09ac\u09b2 \u09b8\u09c7\u0987\u2026","rel":"","context":"In &quot;\u098f\u09b8 \u0995\u09bf\u0989 \u098f\u09b2 - \u09e6\u09e6\u09e7 \u0964 SQL - 001&quot;","block_context":{"text":"\u098f\u09b8 \u0995\u09bf\u0989 \u098f\u09b2 - \u09e6\u09e6\u09e7 \u0964 SQL - 001","link":"http:\/\/bangla.sitestree.com\/?cat=155"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78924,"url":"http:\/\/bangla.sitestree.com\/?p=78924","url_meta":{"origin":78933,"position":5},"title":"SQL Server Dynamic SQL, Procedures, Cursors, and Injection: Concepts and Examples","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"Stored procedures, dynamic SQL, and cursors solve different problems. Good T-SQL design starts with ordinary set-based queries and introduces these features only when they are warranted.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78933"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78933\/revisions"}],"predecessor-version":[{"id":78995,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78933\/revisions\/78995"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78933"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}