使用.NET Framework生成SQL Server脚本 [英] Generate sql server scripts using .net framework

查看:255
本文介绍了使用.NET Framework生成SQL Server脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从SQL Server使用.NET Framework生成的脚本?

Is it possible to generate script from sql server using .net framework?

例如,如果我有数据库罗斯文。然后,我想在窗体文件扩展名来创建/生成脚本插入.SQL。(例如:northwind_insert.sql)

For example, if I have database "Northwind". Then I wish to create/generate script insert" in form file extension .sql (ex: northwind_insert.sql).

时使用的东西时,你的SQL服务器>脚本表作为桌子上单击鼠标右键>插入为>文件等。

Is something like when you using sql-server "right-click on table" > "script table as" > "Insert to" > "file".

推荐答案

如果您在code添加对的 SMO 组件,那么你就可以写简单的code如下:

If you add references in your code to the SMO assemblies, then you can write simple code as follows:

using Microsoft.SqlServer.Management.Smo;

namespace PlayAreaCS2010
{
    class Program
    {
        static void Main(string[] args)
        {
            var srv = new Server(@"(local)\sql2k8");
            var db = srv.Databases["TestDN"];
            foreach (Table tab in db.Tables)
            {
                foreach (string s in tab.Script())
                {
                    Console.WriteLine(s);
                }
            }
            Console.Read();
        }
    }

}

SMO组件需要引用 - Microsoft.SqlServer.Smo,Microsoft.SqlServer.ConnectionInfo,Microsoft.SqlServer.Management.Sdk.Sfc - 都位于C:\ Program Files文件\ Microsoft SQL Server的\ 100 \ SDK \组件(或同等学历,调整版本,位数)

SMO Assemblies you need to reference - Microsoft.SqlServer.Smo, Microsoft.SqlServer.ConnectionInfo, Microsoft.SqlServer.Management.Sdk.Sfc - are all located in C:\Program Files\microsoft sql server\100\SDK\Assemblies (Or equivalent, adjusting for version and bitness)

这篇关于使用.NET Framework生成SQL Server脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆