Click here to Skip to main content
11,496,146 members (579 online)
Click here to Skip to main content

agStoredProc

, 19 Nov 2004 57K 1.1K 49
With agStoredProc, you can generate a C# class out of all the stored procedures in a MS-SQL database. No Microsoft ApplicationBlocks needed.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

Introduction

With all the time developing applications using stored procedures, generating classes that used this stored procs, I got sick of having to go through the same process all the time; name of stored proc, parameters, parameter type, parameter direction,... oh God, really, it was sickening. So once, I decided I wouldn't do this anymore. That's why I did this little application and decided to share it with you.

At the time I developed it, May 2004, I didn't know about Microsoft Application Blocks, so I had to hard code it, let's say so. If I had known before about MS App Blocks, I would have done it with it, but it was done already, and it works sort of fast: some 40,000 lines of code in five seconds. Some friend said to me I could have used the CodeDom namespace to do so, but honestly I only used SqlClient class and one miserable StringBuilder, so it was pure appending strings.

This is a picture of the running application:

Sample image

This is a little snippet of the code I use to get the parameters for each of the stored procs:

foreach (string str in commands)
{
  SqlCommand myCommand = new SqlCommand("SELECT * " + 
             "FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_NAME = '" 
             + str + "'", mycon);
  SqlDataReader myReader = myCommand.ExecuteReader();
  if (myReader.HasRows)
  {
    string prms = "";
    SqlCommand cmdParams = new SqlCommand("SELECT DATA_TYPE," + 
               " PARAMETER_NAME FROM INFORMATION_SCHEMA.PARAMETERS" + 
               " WHERE SPECIFIC_NAME = '"+str+"'", mycon2);
    SqlDataReader ParamsReader = cmdParams.ExecuteReader();
    int prm = 0;
    if (ParamsReader.HasRows)
    {
      while(ParamsReader.Read())
      {
        string prmname = ParamsReader.GetString(0);
        prms += ParamTypeToString(prmname) + " _" + 
                ParamsReader.GetString(1).Remove(0,1).ToLower() 
                +", ";
        prm++;
      }
    }
    prms = prms.Remove(prms.Length -1, 1);
    strBuilder.Append("\n\n\tpublic void " + commands[i] + "(" + 
                      prms.Remove(prms.Length -1, 1)+")");
    strBuilder.Append("\n\t{");
    string sqlcon = (this.tbConnection.Text == "")? "\"" + 
                     this.connection+"\"" : this.tbConnection.Text;
    strBuilder.Append("\n\t\tSqlConnection sqlCon = 
                                  new SqlConnection("+sqlcon+");");
    strBuilder.Append("\n\t\tSqlCommand myCommand = 
                    new SqlCommand(\""+commands[i]+"\", sqlCon);");
    strBuilder.Append("\n\t\tmyCommand.CommandType = 
                                    CommandType.StoredProcedure;");
    while(myReader.Read())
    {
      string _size = (myReader.IsDBNull(9)) ? "": ", " + 
                        myReader.GetInt32(9).ToString();
      string param_name_whole = myReader.GetString(7);
      string param_name = param_name_whole.Remove(0,1);

As you can see, it was just some commands I run against the SQL Server. I didn't know about these commands but with the Help, I found them and it really worked.

Summary

Honestly, there are better ways to do this, but this was the first way I found. I didn't know about Application Blocks at the time and after I finished the application, I really wouldn't change the code because it really works and, for the use it has, it is pretty fine. It does go several times to the SQL Server for finding the stored procs, then parameters, etc., but I consider it runs real fast.

I expect that you can find some use out of it. Hope you enjoy it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Alexander German
Web Developer
Spain Spain
I have been programming for a few years now, like since 1996. Currently working as freelance.

C#.NET MCP. Works with tools such as Delphi, C#.NET, VB.NET, ASP.NET, ADO.NET, XML, java, j2me, Oracle, mySQL, and Sql Server, etc, etc. Open to everything.

Comments and Discussions

 
GeneralPerfect Pin
French Fry19-Sep-05 15:39
memberFrench Fry19-Sep-05 15:39 
GeneralGreat Start,,, Pin
Hugh Will Nevreaux2-Dec-04 7:51
memberHugh Will Nevreaux2-Dec-04 7:51 
GeneralRe: Great Start,,, Pin
Alexander German2-Dec-04 8:50
memberAlexander German2-Dec-04 8:50 
GeneralEncapsulate SqlConnection Pin
BloodBaz25-Nov-04 3:39
memberBloodBaz25-Nov-04 3:39 
GeneralRe: Encapsulate SqlConnection Pin
Alexander German29-Nov-04 7:43
memberAlexander German29-Nov-04 7:43 
GeneralCodegeneration Pin
Marc Sommer20-Nov-04 22:46
memberMarc Sommer20-Nov-04 22:46 
GeneralRe: Codegeneration Pin
Alexander German21-Nov-04 5:33
memberAlexander German21-Nov-04 5:33 
GeneralRe: Codegeneration Pin
Oskar Austegard24-Nov-04 8:13
memberOskar Austegard24-Nov-04 8:13 
GeneralGood Utility Pin
sides_dale19-Nov-04 17:22
membersides_dale19-Nov-04 17:22 
This is a very useful tool. There are only 2 improvements/additions I can think of.
1. Add an option for saving the output to a file. Much like this article http://www.codeproject.com/cs/database/genProcMethods.asp. Even though the other utility writes to an output file, I like your utility better because of the option to set the connection string, namespace and class with a gui instead of having to either change the source or create your own front end (which I did prior to seeing your article).
2. The ability to create classes for the tables as well as stored procedures.

Overall your utility is very useful though.
GeneralRe: Good Utility Pin
Alexander German21-Nov-04 5:21
memberAlexander German21-Nov-04 5:21 
GeneralRe: Good Utility Pin
sides_dale21-Nov-04 14:42
membersides_dale21-Nov-04 14:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 19 Nov 2004
Article Copyright 2004 by Alexander German
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid