简体   繁体   中英

c# program as a .NET SQLCLR stored procedure rewrite

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

I am not entirely sure what you mean by:

pass the result of the above C# program to the .NET SQLCLR stored procedure

The code for the console app you have at the moment cannot be placed within a SQLCLR assembly and work as you are using SMO and that is specifically disabled within SQL Server's CLR host.

Generally speaking, you pass values into a SQLCLR object via input parameters, just like a T-SQL stored procedure or function.

Technically, you can keep the SMO code working in the console app and call that .EXE as an external process via Process.Start() within the SQLCLR object, but that requires:

  1. Setting your assembly to PERMISSION_SET = UNSAFE ,

    and

  2. setting this up to use a work-flow similar to:

    1. create a tempfile name in the SQLCLR code (so that concurrently running processes don't accidentally share the same file) using Path.GetTempFileName
    2. call the console app, passing in the tempfile name
    3. read the tempfile in the SQLCLR object into a string variable
    4. delete the tempfile (ie cleanup)
    5. return the string variable as an out parameter

I'm not sure that it's worth the trouble to do all of that, but that is what it would take to accomplish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM