Executing a SQL Server stored procedure in C#

Are you kidding me? I’m a major amatuer at anything .NET, and after some Googling and about 15 minutes of coding, this works. How ridiculously easy.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6.  
  7. namespace ExecuteStoredProcedure
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. SqlConnection conn;
  14. try
  15. {
  16. conn = new SqlConnection("server=server;database=db;Trusted_Connection=yes");
  17. conn.Open();
  18. Console.WriteLine("Connection successful...");
  19. }
  20. catch (Exception ex)
  21. {
  22. Console.WriteLine(ex.Message);
  23. return;
  24. }
  25.  
  26. SqlCommand db_cmd = new SqlCommand("dbo.usp_c_sharp_test",conn);
  27. db_cmd.CommandType = CommandType.StoredProcedure;
  28.  
  29. SqlDataReader data_reader;
  30. data_reader = db_cmd.ExecuteReader();
  31. data_reader.Close();
  32. conn.Close();
  33. }
  34. }
  35. }

4 Comments

  1. Jenni "red" Halcomb says:

    …..”these aren’t the droids you’re looking for” (wave hand here)

  2. Chad says:

    Dwight also informed me about that one being left off the top ten list. Maybe I’ll make it a top eleven list later.

  3. jenni "red" says:

    you should add photos….i’m sure someone has a photo of dwight passed out somewhere (love you dwight if you read this)

  4. Chad says:

    I’ll have to look for some pictures. I know I have some in a box. Actually, I think there is still very incriminating video out there somewhere that Dwight shot of me.

Leave a Reply