Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 926997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:46:47+00:00 2026-05-15T19:46:47+00:00

is there an elegant object-orient based framework?

  • 0

is there an elegant object-orient based framework?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:46:47+00:00Added an answer on May 15, 2026 at 7:46 pm

    Here is some code that I wrote for generating ‘insert’ stored procedures for every table in a database. It also handles returning the new id for those tables that have an identity column. It uses SQL SMO. Some of it is a bit specific to my project so please let me know if you have any questions.

        void InsertScripts(Database db)
        {
            var tables = db.Tables.ToIEnumerable(); //this is an extension method to convert Database.Tables into an IEnumerable<Table>             
            {
    
                foreach (var t in tables)
                {
                    var sb = new StringBuilder();
                    var sp = new StoredProcedure(db, "gen_insert_" + t.Name);
    
                    sp.AnsiNullsStatus = false;
                    sp.QuotedIdentifierStatus = false;
                    sp.TextMode = false;                    
    
                    var columns = t.Columns.ToIEnumerable().Where(c => !c.Identity && !c.IsReadOnly()).ToList();
    
                    foreach (var c in columns)
                    {
                        var p = new StoredProcedureParameter(sp, "@" + t.Name + "_" + c.Name, c.DataType);                        
    
                        p.IsCursorParameter = false;
    
    
                        if(c.Default != null && c.Default.Length > 0)
                            p.DefaultValue = c.Default;
    
                        if (c.Nullable)
                            p.DefaultValue = "NULL";
    
    
                        sp.Parameters.Add(p);                        
    
                    }
    
    
                    var cols = string.Join(",", columns.Select(c => c.Name).ToArray());
                    var vals = string.Join(",", columns.Select(c => "@" + t.Name + "_" + c.Name).ToArray());
    
    
                    var sql = string.Format("insert into {0} ({1}) values ({2});", t.Name, cols, vals);
    
                    sb.AppendLine(sql);
    
                    if (t.Columns.ToIEnumerable().Any(c => c.Identity))
                    {
                        var declaration = "declare @newid int;\r\n";
                        var ret = "select @newid = scope_identity();\r\nselect @newid;\r\nreturn @newid";
    
                        sb.Insert(0, declaration);
                        sb.AppendLine(ret);
    
                    }
    
                    sp.TextBody = sb.ToString();
    
                    if(cols.Length > 0 && sp.Parent.StoredProcedures[sp.Name] == null)
                        sp.Create();
    
                }
    
    
            }
        }
    
    public static class Utils //Extension methods...
    {
        public static IEnumerable<Table> ToIEnumerable(this TableCollection tables)
        {
            var list = new List<Table>();
    
            foreach (Table t in tables)
                list.Add(t);
    
            return list;
        }
    
        public static IEnumerable<View> ToIEnumerable(this ViewCollection views)
        {
            var list = new List<View>();
    
            foreach (View v in views)
                list.Add(v);
    
            return list;
        }
    
        public static IEnumerable<Column> ToIEnumerable(this ColumnCollection columns)
        {
            var list = new List<Column>();
    
            foreach (Column c in columns)
                list.Add(c);
    
            return list;
        }
    
    
        public static IEnumerable<ForeignKey> ToIEnumerable(this ForeignKeyCollection columns)
        {
            var list = new List<ForeignKey>();
    
            foreach (ForeignKey c in columns)
                list.Add(c);
    
            return list;
        }
    
    
        public static IEnumerable<string> ToIEnumerable(this ForeignKeyColumnCollection columns)
        {
            var list = new List<string>();
    
            foreach (ForeignKeyColumn c in columns)
                list.Add(c.Name);
    
            return list;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an elegant way to asynchronously map an object or array in coffeescript?
Is there an elegant way to create a CLLocationCoordinate2D object from an array of
Are there any elegant quick way to map object to a dictionary and vice
Is there an elegant way to use a specific dependency as a file object
The object in question consists of key/value pairs aka @property. Is there an elegant
Is there an elegant way in java to check if an int is equal
Is there an elegant way to make whitespace string. To be more precise, I'm
Is there an elegant way to do this in MySQL: SELECT (subquery1) AS s1,
Is there more elegant (less code) way of find a matrix OUT, with colSums(OUT)<=a
Is there an elegant way to create an empty jquery element (versus null) ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.