\"Select to\". I thought that almost everything in SSMS was done through the SMO scripting engine, but I can\'t seem to find a way to do" />
开发者

Using SMO to script table as select

开发者 https://www.devze.com 2023-01-01 04:22 出处:网络
SSMS allows you to right-click a table and \"Script Table as\"->\"Select to\". I thought that almost everything in SSMS was done through the SMO scripting engine, but I can\'t seem to find a way to do

SSMS allows you to right-click a table and "Script Table as"->"Select to". I thought that almost everything in SSMS was done through the SMO scripting engine, but I can't seem to find a way to do this through SMO short of looping thr开发者_如何学Goough columns and generating the script myself.

Is my Google-Fu weak, or are people just not using SMO for something like this? I couldn't find any example scripts for this anywhere, but it seems like it would be a common need.


It looks like the Script() function of a table can do that:

Server server = new Server(".");
Database northwind = server.Databases["Northwind"];
Table categories = northwind.Tables["Categories"];
StringCollection script = categories.Script();
string[] scriptArray = new string[script.Count];
script.CopyTo(scriptArray, 0);

Now scriptArray will contain a list of SQL commands, the first entry of the string array will be set ansi_nulls on. See this blog post.

0

精彩评论

暂无评论...
验证码 换一张
取 消