c#program作为.NET SQLCLR存储过程重写

时间:2016-10-12 17:20:52

标签: c# .net sql-server sqlclr smo

我正在使用Visual Studio 2015& SQL Server 2014.我有以下C#程序,我必须传递Table对象名称,程序生成Create Table脚本。现在我正在努力将其写为SQLCLR存储过程。能否请你给我一个想法或方向来实现这个目标?

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();
                    }
                }
            }
        }
    }

如何将上述C#程序的结果传递给下面的.NET SQLCLR存储过程?

public static void Scriptfile (string table)
{
    SqlPipe Procedure = SqlContext.Pipe;
    Procedure.Send("");
}

1 个答案:

答案 0 :(得分:0)

我不完全确定你的意思:

  

将上述C#程序的结果传递给.NET SQLCLR存储过程

您目前使用的控制台应用程序的代码不能放在SQLCLR程序集中,并且可以像使用SMO一样工作,并且在SQL Server的CLR主机中被禁用。

一般来说,您可以通过输入参数将值传递给SQLCLR对象,就像T-SQL存储过程或函数一样。

从技术上讲,可以保持SMO代码在控制台应用程序中运行,并通过SQLCLR对象中的Process.Start()将该.EXE作为外部进程调用,但这需要:

  1. 将程序集设置为PERMISSION_SET = UNSAFE

  2. 将其设置为使用类似于:

    的工作流程
    1. 使用Path.GetTempFileName
    2. 在SQLCLR代码中创建临时文件名(以便同时运行的进程不会意外地共享同一文件)
    3. 调用控制台应用程序,传入临时文件名称
    4. 将SQLCLR对象中的tempfile读入字符串变量
    5. 删除临时文件(即清理)
    6. 将字符串变量作为out参数
    7. 返回
  3. 我不确定做这一切是否值得麻烦,但这就是完成所需要的。

相关问题