日期:2014-05-20 浏览次数:21168 次
--先搞一个测试环境 use tempdb create table test(id int identity(1,1))
//添加引用
            //Microsoft.SqlServer.ConnectionInfo.dll
            //Microsoft.SqlServer.Smo.dll
            Microsoft.SqlServer.Management.Common.ServerConnection conn = new Microsoft.SqlServer.Management.Common.ServerConnection(
                new System.Data.SqlClient.SqlConnection("server=localhost;uid=sa;pwd=***;database=master"));//一个数据库连接字符串
            Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(conn);
            Microsoft.SqlServer.Management.Smo.Database db = server.Databases["tempdb"];
            Microsoft.SqlServer.Management.Smo.Table tb= db.Tables["test"];
            System.Collections.Specialized.StringCollection sc= tb.Script();
            foreach (String s in sc)
            {
                Console.WriteLine(s);
            }