{"id":43,"date":"2006-03-29T12:19:44","date_gmt":"2006-03-29T17:19:44","guid":{"rendered":"http:\/\/stein.everybody.org\/brain\/net-generic-method-to-run-a-stored-procedure\/"},"modified":"2011-03-10T10:57:33","modified_gmt":"2011-03-10T15:57:33","slug":"net-generic-method-to-run-a-stored-procedure","status":"publish","type":"post","link":"https:\/\/jeremystein.com\/brain\/net-generic-method-to-run-a-stored-procedure\/","title":{"rendered":".NET Generic Method to Run a Stored Procedure"},"content":{"rendered":"<p>Don&#8217;t you wish you could call your stored procedures just like they were methods in another class?  Using .NET 1.1, I added a method like this to a utility class:<\/p>\n<p><code><\/p>\n<pre>\npublic static DataTable ExecuteQuery(\n  string spName,\n  params object[] args)\n{\n  using (SqlConnection conn =\n         new SqlConnection(Global.ConnString))\n  {\n    using (SqlCommand command =\n           new SqlCommand(spName, conn))\n    {\n      command.CommandType = CommandType.StoredProcedure;\n      for (int i=0; i&lt;args.Length-1; i+=2)\n      {\n        command.Parameters.Add(\n          args[i].ToString(), args[i+1]);\n      }\n      using (SqlDataAdapter adapter =\n             new SqlDataAdapter(command))\n      {\n        DataTable table = new DataTable();\n        adapter.Fill(table);\n        return table;\n      }\n    }\n  }\n}\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Don&#8217;t you wish you could call your stored procedures just like they were methods in another class? Using .NET 1.1, I added a method like this to a utility class: public static DataTable ExecuteQuery( string spName, params object[] args) { using (SqlConnection conn = new SqlConnection(Global.ConnString)) { using (SqlCommand command = new SqlCommand(spName, conn)) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-net"],"_links":{"self":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":0,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}