方便而强大的SQLDOM SQLDMO.Views; 视图列表SQLDMO.StoredProcedure; 存储过程列表 代码片段: private void btnCreateTable_Click(object sender, EventArgs e) { string strFilePath=@"D:/CreateTable.txt"; StringBuilder SBCreateTable=new StringBuilder(); if(File.Exists(strFilePath)) { File.Delete(strFilePath); } SQLDMO.SQLServer DMOServer = new SQLDMO.SQLServerClass(); DMOServer.Connect(".", "sa", "sa"); for (int i = 0; i < DMOServer.Databases.Count; i++) { if (DMOServer.Databases.Item(i + 1, "dbo").Name.IndexOf("TestDBName") >= 0) { SQLDMO._Database db = DMOServer.Databases.Item(i + 1, "dbo"); for (int j = 0; j < db.Tables.Count; j++) { SQLDMO.Table DMOTable =(SQLDMO.Table) db.Tables.Item(j + 1, "dbo"); if (DMOTable.Name.IndexOf("TestTabel_") >= 0) { System.Windows.Forms.Application.DoEvents(); lblInfo.Text = "正在检索并创建第" + j.ToString() + "个表!"; SBCreateTable.Append(DMOTable.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default, Type.Missing, Type.Missing, SQLDMO.SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_Default)); SBCreateTable.Append(Environment.NewLine); } } } } using (StreamWriter sw = new StreamWriter(strFilePath, true)) { sw.Write(SBCreateTable.ToString()); sw.Close(); } }