c#程序作为.net SQLCLR存储过程重写

本文关键字:SQLCLR 存储过程 重写 net 程序 | 更新日期: 2023-09-27 18:12:42

我使用的是Visual Studio 2015 &SQL Server 2014。我有下面的c#程序,我必须传递表对象名称和程序生成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("");
}

c#程序作为.net SQLCLR存储过程重写

我不太明白你所说的:

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

您目前拥有的控制台应用程序的代码不能放在SQLCLR程序集中并工作,因为您正在使用SMO,并且在SQL Server的CLR主机中特别禁用。

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

从技术上讲,可以保持SMO代码在控制台应用程序中工作,并通过SQLCLR对象中的Process.Start()调用。exe作为外部进程,但这需要:
  1. 将组件设置为PERMISSION_SET = UNSAFE

  2. 将其设置为使用类似于以下的工作流:

    1. 使用Path在SQLCLR代码中创建一个临时文件名(这样并发运行的进程就不会意外地共享同一个文件)。GetTempFileName
    2. 调用控制台应用程序,传入temp文件名
    3. 将SQLCLR对象中的tempfile读入字符串变量
    4. 删除临时文件(即清理)
    5. 返回字符串变量作为out参数

我不确定做这些事是否值得,但这是必须要完成的。