使用.NET Framework生成SQL Server脚本脚本、Framework、NET、Server

2023-09-08 08:41:20 作者:求佛不如拜我

是否有可能从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)

 
精彩推荐