sql数据库导出,调用SQLDMO实现数据库对象的导出

项目需求,导出所有表定义到.sql文件,所有的表、视图、存储过程、用户定义函数分目录存放,每个数据库对象定义保存为一个.sql文件,文件名为对象名。 本来想调用命令行工具进行,但是对于数据库对象的遍历又无法写成能自适应的过程,需要手动指定。 SQLDMO为SQL Server浏览和操作数据库对象及进行DDL的基本接口,相关资料参照MSDN: http://msdn.microsoft.com/en-us/library/ms131540.aspx 对于批量数据导出,大家可以使用bcp工具,相关资料参照MSDN: http://msdn.microsoft.com/en-us/library/ms162802.aspx 对于数据库Schema和数据的导出,可以使用sqlpubwiz工具,相关资料参照codeplex: http://sqlhost.codeplex.com/wikipage?title=DPW%20Command%20Line%20Interface
下面是使用SQLDMO进行数据对象导出的示例:(需要C#版本的请留言,晚上可取)
Imports System.Data.SqlClient Imports SQLDMO Imports System.IO Imports System.Text Module Module1 Sub Main() Dim currentPath As String = String.Empty currentPath = AppDomain.CurrentDomain.BaseDirectory Dim fs As StreamWriter = Nothing Dim di As DirectoryInfo = Nothing Dim server As New SQLDMO.SQLServer() Dim db As SQLDMO.Database2 = Nothing server.Connect("ServerName", "UserID", "Password") For Each d As Database2 In server.Databases If d.Name.ToLower().Equals("Database Name".ToLower()) Then db = d Exit For End If Next di = Directory.CreateDirectory(Path.Combine(currentPath, "Tables")) For Each table As Table2 In db.Tables If Not table.SystemObject Then Dim sql As New StringBuilder sql.AppendLine("/*--Begin Table " & table.Name & "--*/") sql.AppendLine("IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" & table.Name & "]'))") sql.AppendLine("DROP FUNCTION [dbo].[" & table.Name & "]") sql.AppendLine() sql.AppendLine(table.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default, _ String.Empty, _ String.Empty, _ SQLDMO.SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_Default)) sql.AppendLine("/*--End Table " + table.Name + "--*/") fs = New StreamWriter(Path.Combine(di.FullName, table.Name & ".sql")) fs.WriteLine(sql.ToString()) fs.Flush() fs.Close() End If Next di = Directory.CreateDirectory(Path.Combine(currentPath, "StoredProcedures")) For Each sp As StoredProcedure2 In db.StoredProcedures If Not sp.SystemObject Then Dim sql As New StringBuilder sql.AppendLine("/*--Begin Stored Procedure " & sp.Name & "--*/") sql.AppendLine("IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" & sp.Name & "]'))") sql.AppendLine("DROP FUNCTION [dbo].[" & sp.Name & "]") sql.AppendLine() sql.AppendLine(sp.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default, _ String.Empty, _ SQLDMO.SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_Default)) sql.AppendLine("/*--End Stored Procedure " + sp.Name + "--*/") fs = New StreamWriter(Path.Combine(di.FullName, sp.Name & ".sql")) fs.WriteLine(sql.ToString()) fs.Flush() fs.Close() End If Next di = Directory.CreateDirectory(Path.Combine(currentPath, "Views")) For Each view As View2 In db.Views If Not view.SystemObject Then Dim sql As New StringBuilder sql.AppendLine("/*--Begin View " & view.Name & "--*/") sql.AppendLine("IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" & view.Name & "]'))") sql.AppendLine("DROP FUNCTION [dbo].[" & view.Name & "]") sql.AppendLine() sql.AppendLine(view.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default, _ String.Empty, _ SQLDMO.SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_Default)) sql.AppendLine("/*--End View " + view.Name + "--*/") fs = New StreamWriter(Path.Combine(di.FullName, view.Name & ".sql")) fs.WriteLine(sql.ToString()) fs.Flush() fs.Close() End If Next di = Directory.CreateDirectory(Path.Combine(currentPath, "Functions")) For Each func As UserDefinedFunction In db.UserDefinedFunctions If Not func.SystemObject Then Dim sql As New StringBuilder sql.AppendLine("/*--Begin Function " & func.Name & "--*/") sql.AppendLine("IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" & func.Name & "]'))") sql.AppendLine("DROP FUNCTION [dbo].[" & func.Name & "]") sql.AppendLine() sql.AppendLine(func.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default, _ String.Empty, _ SQLDMO.SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_Default)) sql.AppendLine("/*--End Function " + func.Name + "--*/") fs = New StreamWriter(Path.Combine(di.FullName, func.Name & ".sql")) fs.WriteLine(sql.ToString()) fs.Flush() fs.Close() End If Next End Sub End Module
其实是很简单的东西,不知道微软为什么不实现一个简单版本。希望此工具能给大家带来便利,下一篇随笔估计就是如何进行批量.sql文件在数据库中生成数据库对象的实现,感谢大家的支持。
Tags:  如何导出数据库 exp导出数据库 数据库导出 sql数据库导出

延伸阅读

最新评论

发表评论