问题描述
/// <summary>
/// Create single Table script with Data of specified DataBase and Table.
/// </summary>
/// <param name="dataBaseName">DataBase Name</param>
/// <param name="tableName">Table Name</param>
/// <param name="connectionString">Connection String</param>
public void CreateScriptTable(string dataBaseName,string tableName,string connectionString)
{
sqlConnection con = new sqlConnection(connectionString);
ServerConnection serverConnection = new ServerConnection(con);
Server server = new Server(serverConnection);
Database database = server.Databases["" + dataBaseName + ""];//Here exception coming
if (database != null)
{
Scripter scripter = new Scripter(server);
scripter.Options.ScriptData = true;
scripter.Options.ScriptSchema = true;
scripter.Options.ScriptDrops = false;
var sb = new StringBuilder();
foreach (Table table in database.Tables)
{
if (table.Name.ToLower() == tableName.ToLower())
{
sb.Append("DROP TABLE " + table.Name);
sb.Append(Environment.NewLine);
foreach (string s in scripter.EnumScript(new Urn[] { table.Urn }))
{
sb.Append(s);
sb.Append(Environment.NewLine);
}
string folder = "C:\\temp\\Scripts\\";
string filename = folder + tableName + ".sql";
System.IO.StreamWriter fs = System.IO.File.CreateText(filename);
fs.Write(sb);
fs.Close();
}
}
}
}
添加的参考:
using Microsoft.sqlServer.Management.Common;
using Microsoft.sqlServer.Management.Sdk.Sfc;
using Microsoft.sqlServer.Management.Smo;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.sqlClient;
using System.Text;
using System.Windows.Forms;
例外:
Method not found: 'Microsoft.sqlServer.Management.Common.ServerConnection Microsoft.sqlServer.Management.Common.ServerConnection.GetDatabaseConnection(System.String)'.
at Microsoft.sqlServer.Management.Smo.Server.GetExecutionManager()
at Microsoft.sqlServer.Management.Smo.Server.get_ExecutionManager()
at Microsoft.sqlServer.Management.Smo.sqlSmoObject.get_DatabaseEngineType()
at Microsoft.sqlServer.Management.Smo.DatabaseCollection.get_Item(String name)
at CreateBatchFile.Form1.CreateScriptTable(String dataBaseName,String tableName,String connectionString) in C:\Test Projects\CreateBatchFile\CreateBatchFile\Form1.cs:line 45
at CreateBatchFile.Form1.btnGenerateBatchFile_Click(Object sender,EventArgs e) in C:\Test Projects\CreateBatchFile\CreateBatchFile\Form1.cs:line 29
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.onmouseup(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons button,Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.dispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.imsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)
at System.Windows.Forms.Application.threadcontext.RunMessageLoopInner(Int32 reason,ApplicationContext context)
at System.Windows.Forms.Application.threadcontext.RunMessageLoop(Int32 reason,ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at CreateBatchFile.Program.Main() in C:\Test Projects\CreateBatchFile\CreateBatchFile\Program.cs:line 19
解决方案资源管理器中的参考:
请让我知道我的方向错误。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)