how i can generate programmatically “insert into” data script file from a database table?

前端 未结 3 505
醉话见心
醉话见心 2021-01-27 08:03

is there an elegant object-orient based framework?

3条回答
  •  醉话见心
    2021-01-27 08:34

    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             
            {
    
                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
    ToIEnumerable(this TableCollection tables) { var list = new List
    (); foreach (Table t in tables) list.Add(t); return list; } public static IEnumerable ToIEnumerable(this ViewCollection views) { var list = new List(); foreach (View v in views) list.Add(v); return list; } public static IEnumerable ToIEnumerable(this ColumnCollection columns) { var list = new List(); foreach (Column c in columns) list.Add(c); return list; } public static IEnumerable ToIEnumerable(this ForeignKeyCollection columns) { var list = new List(); foreach (ForeignKey c in columns) list.Add(c); return list; } public static IEnumerable ToIEnumerable(this ForeignKeyColumnCollection columns) { var list = new List(); foreach (ForeignKeyColumn c in columns) list.Add(c.Name); return list; } }

    提交回复
    热议问题