I only talk to SQL Server databases lately, so I am most familiar with its handy INFORMATION_SCHEMA.Columns view. This can give you the column listing for any table. A companion to this is the SYSOBJECTS table which harbors a list of the parameters in your Stored Procedures. Remember the XTYPE='P' in your where clause.
As the picture begins to render on how to do this, I want to tell somebody really bad about using CodeDom namespace for building the actual code. You could use StringBuilder to concatenate a big bunch of text, but the CodeDom does it more elegantly.
Getting started it always the hardest with new white canvas so I post the opener :
// Declare a Class
CodeTypeDeclaration ct = new CodeTypeDeclaration("data");
ct.IsClass = true;
Another tricky thing for me was making the code concise by having the declaration and the instancing of the variables on one line :
// Declare a Connection
csne = new CodeSnippetExpression("this.ConnectionString");
// You need a Param array of things passed to a constructor.
CodeExpression[] cParams = { csne };
coc = new CodeObjectCreateExpression("SqlConnection", cParams);
// Then .. all the instancing and declaring on one line :
codeVar =
new CodeVariableDeclarationStatement("SqlConnection", "sqlConnection", coc);
cmm.Statements.Add(codeVar);
Finally, building the actual class code from your heap of objects in memory could be elusive since you have been buried in the CodeDom bits for a few hours :
// This could be a VB thing as well.
CodeDomProvider prov = new Microsoft.CSharp.CSharpCodeProvider();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
TextWriter tw = new StringWriter(sb);
prov.GenerateCodeFromMember(ct, tw, null);
HTH
- Brian
No comments:
Post a Comment