Using SMO to script table as select
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.
精彩评论