在sql Server2005 的使用当中,从2000起便提供了Script 支持,即:
这种脚本支持很方便,但需要你去选择,再去执行,time and time again,so boring.....,想个办法用程序实现。
在TechNet searcher 一下,找到了Table,Index..... 这些class,ok,接着找到sqlServer2005联机丛书上有相关介绍:
仔细看了看Index的Methods,找到了script()方法,对,就这个方法。Now,coding......
1private void indexScript(string servername,string username,string userpwd,string dbname,string tablename,string )schemaname
2 {
3 try
4 5 Server server =new Server( ServerConnection(servername,username, userpwd));
6 DatabaseCollection dbcollection server.Databases;
7 Database db dbcollection[dbname];
8 TableCollection tabCollection db.Tables;
9 Table tab tabCollection[tablename, schemaname];
10 IndexCollection indexCollection tab.Indexes;
11
12 for (int h 0; h < indexCollection.Count; h++)
13 {
14 StringCollection sc indexCollection[h].Script();
15 StringBuilder stringbuilder StringBuilder();
16 i ; i sc.Count; i17 18 stringbuilder.Append(sc[i].ToString() +"");
19 }2021 MessageBox.Show(stringbuilder.ToString());
22 }23 catch (Exception ex)
24 25 throw Exception(ex.Message);
2627 }
这样就可以将所有 index的script 显示出来,然后你可以再操纵这些script了,例如导称sql文件再执行。至于其他object都可以使用上面的方法。2 {
3 try
4 5 Server server =new Server( ServerConnection(servername,username, userpwd));
6 DatabaseCollection dbcollection server.Databases;
7 Database db dbcollection[dbname];
8 TableCollection tabCollection db.Tables;
9 Table tab tabCollection[tablename, schemaname];
10 IndexCollection indexCollection tab.Indexes;
11
12 for (int h 0; h < indexCollection.Count; h++)
13 {
14 StringCollection sc indexCollection[h].Script();
15 StringBuilder stringbuilder StringBuilder();
16 i ; i sc.Count; i17 18 stringbuilder.Append(sc[i].ToString() +"");
19 }2021 MessageBox.Show(stringbuilder.ToString());
22 }23 catch (Exception ex)
24 25 throw Exception(ex.Message);
2627 }
上面这个方法只能用于rebuilder数据库对象,因为script():Generates a Transact-sql script that can be used to re-create the index.
其他select、alter脚本估计也可以用程序实现,我正在找......,找到了再补充,呵呵。