在SQL Server Management Studio中,我可以通过右键单击表并选择“Script Table As”生成表的CREATE TABLE脚本。
我应该如何在C#中获得同样的结果?我可以利用SMO或其他方法吗?
[为了避免问题被关闭,请发布可用的代码示例,而不是一行代码或高级文档的链接。]
我应该如何在C#中获得同样的结果?我可以利用SMO或其他方法吗?
[为了避免问题被关闭,请发布可用的代码示例,而不是一行代码或高级文档的链接。]
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.SqlEnum;
using System.Configuration;
using System.Collections.Specialized;
namespace SmoTest {
class Program {
static void Main(string[] args) {
Server server = new Server("XXX");
Database database = new Database();
database = server.Databases["YYY"];
Table table = database.Tables["ZZZ", @"PPP"];
StringCollection result = table.Script();
var script = "";
foreach (var line in result) {
script += line;
}
System.IO.StreamWriter fs = System.IO.File.CreateText(@"QQQ");
fs.Write(script);
fs.Close();
}
}
}
这里有一个稍微更完整的例子(从我的朋友Ben Miller那里借来的):
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.SqlEnum;
using Microsoft.SqlServer.Management.Smo.CoreEnum;
using System.Configuration;
using System.Collections.Specialized;
namespace SmoTest
{
class Program
{
static void Main(string[] args)
{
Server srv = new Server();
// really you would get these from config or elsewhere:
srv.ConnectionContext.Login = "foo";
srv.ConnectionContext.Password = "bar";
srv.ConnectionContext.ServerInstance = "ServerName";
string dbName = "DatabaseName";
Database db = new Database();
db = srv.Databases[dbName];
StringBuilder sb = new StringBuilder();
foreach(Table tbl in db.Tables)
{
ScriptingOptions options = new ScriptingOptions();
options.ClusteredIndexes = true;
options.Default = true;
options.DriAll = true;
options.Indexes = true;
options.IncludeHeaders = true;
StringCollection coll = tbl.Script(options);
foreach (string str in coll)
{
sb.Append(str);
sb.Append(Environment.NewLine);
}
}
System.IO.StreamWriter fs = System.IO.File.CreateText("c:\\temp\\output.txt");
fs.Write(sb.ToString());
fs.Close();
}
}
}
您可以尝试以下函数使用C#从SQL Server数据库获取表脚本。完整文章:在两个SQL Server数据库之间传输数据或脚本
C#代码:
public string GetTableScript(string TableName, string ConnectionString) { string Script = "";using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Common;
using System.Collections.Specialized;
using System.IO;
using System.Data;
using Microsoft.SqlServer.Management;
using System.Data.SqlClient;
namespace GenrateScriptsForDatabase
{
class Program
{
static void Main(string[] args)
{
var server = new Server(new ServerConnection { ConnectionString = new SqlConnectionStringBuilder { DataSource = @"Your Server Name", UserID="Your User Id",Password="Your Password" }.ToString() });
server.ConnectionContext.Connect();
var database = server.Databases["Your Database Name"];
using (FileStream fs = new FileStream(@"H:\database_scripts\Gaurav.sql", FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
for each (Table table in database.Tables)
{
if (table.Name == "Your Table Name")
{
var scripter = new Scripter(server) { Options = { ScriptData = true } };
var script = scripter.EnumScript(new SqlSmoObject[] { table });
for each (string line in script)
{
sw.WriteLine(line);
Console.WriteLine(line);
}
}
}
}
}
}
}