I am using Visual Studio 2015 & SQL Server 2014. I have the below C# program for which I have to pass the Table object name and the program generates the Create Table script. Now I am working on writing this as a SQLCLR Stored Procedure. Could you please give me an idea or direction to accomplish this?
public class CSHProgram
{
static void Main()
{
Server dbservername = new Server(@"(local)");
string tablename = "";
try
{
dbservername.ConnectionContext.LoginSecure = true;
savetbldeftofile(dbservername, tablename);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (dbservername.ConnectionContext.IsOpen)
dbservername.ConnectionContext.Disconnect();
Console.ReadKey();
}
}
public static void savetbldeftofile(Server dbservername, string tablename)
{
StringBuilder sb = new StringBuilder();
string tblName = @"\b" + tablename + "\b";
foreach (Database DBServer in dbservername.Databases)
{
Match db = Regex.Match(DBServer.Name, "UserTestDB", RegexOptions.IgnoreCase);
if (db.Success)
{
foreach (Table dbTbl in DBServer.Tables)
{
Match tabl = Regex.Match(dbTbl.Name, tblName, RegexOptions.IgnoreCase);
if (tabl.Success)
{
ScriptingOptions soptions = new ScriptingOptions();
soptions.ClusteredIndexes = true;
soptions.Default = true;
soptions.DriAll = true;
soptions.Indexes = true;
soptions.IncludeHeaders = true;
soptions.AppendToFile = true;
StringCollection SCollection = dbTbl.Script(soptions);
foreach (string str in SCollection)
{
sb.Append(str);
sb.Append(Environment.NewLine);
}
}
StreamWriter sw = File.CreateText(@"c:\temp\" + tablename + ".sql");
sw.Write(sb.ToString());
sw.Close();
}
}
}
}
}
How would I pass the result of the above C# program to the .NET SQLCLR stored procedure below?
public static void Scriptfile (string table)
{
SqlPipe Procedure = SqlContext.Pipe;
Procedure.Send("");
}