免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
使用SMO程序化生成SQL Server表數(shù)據(jù)

作為ETL的一部分,有時(shí)候就是需要把數(shù)據(jù)的Insert腳本生成出來,然后人肉拷貝到另一個(gè)地方執(zhí)行。

熟悉SMSS的同學(xué)們都知道,有個(gè)生成腳本的任務(wù),可以生成數(shù)據(jù)庫的create腳本啊什么的,其實(shí)也能夠生產(chǎn)表中的數(shù)據(jù)。

自動化的ETL總不能連導(dǎo)出數(shù)據(jù)都人肉。。。一是容易出錯,二是太low了。

C#控制臺代碼可以搞定這些,直接上代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management; 
using Microsoft.SqlServer.Management.Sdk.Sfc;  

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String todayDate = DateTime.Now.Day + "_" + DateTime.Now.Month + "_" + DateTime.Now.Year;
            String backupDirectory = "1a:\\DBBackup\\";
            String backupFileName = backupDirectory + todayDate + ".sql";
            if (File.Exists(backupFileName))
            {

                File.Delete(backupFileName);

            }
            StreamWriter sw = File.CreateText(backupFileName);


            Console.WriteLine(backupFileName);
            Console.ReadKey();            
            Console.WriteLine("hello!");


            //Console.ReadLine();
            String dbName = "2oy"; // database name   
            Server srv = new Server("3lba1");

            // Reference the database.    
            Database db = srv.Databases[dbName];

            // Define a Scripter object and set the required scripting options.   
            Scripter scrp = new Scripter(srv);
           
            scrp.Options.ScriptSchema = false;
            scrp.Options.ScriptDrops = false;
            scrp.Options.WithDependencies = false;
            scrp.Options.Indexes = false;   // To include indexes  
            scrp.Options.DriAllConstraints = false;   // to include referential constraints in the script  
            scrp.Options.ScriptData = true; //Data include!!!!!!

            
        

            //Iterate through the tables in database and script each one.

            foreach (Table tb in db.Tables)
            {

                if (!tb.IsSystemObject)
                {

                    foreach (string s in scrp.EnumScript(new Urn[] { tb.Urn }))
                    {

                        sw.WriteLine(s);

                        sw.Flush();

                    }

                }

            }


            /*
             * 此方法不能生成帶數(shù)據(jù)的腳本,但是可以生成schema腳本
            foreach (Table tb in db.Tables)
            {
                System.Collections.Specialized.StringCollection sc = scrp.Script(new Urn[]{tb.Urn}); 

                foreach (string st in sc)
                {
                    Console.WriteLine(st);
                }
                Console.WriteLine("--");

            }
             */
             
        }
    }
}

 

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#連接MySQL,并完成對數(shù)據(jù)庫的增刪查改
依賴注入框架Autofac的簡單使用
SqlConnection類
14-11讀取數(shù)據(jù)庫-DataReader(1)
EF6 用Database.BeginTransaction管理事務(wù)
C#窗體連接MySql并通過DataGridView展示數(shù)據(jù)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服