Generating Table Script from SQL Server using blueprism

Soumya Guttedar 0 Reputation points
2023-06-12T07:34:30.0466667+00:00

Hi Everyone

I want to generate table script from SQL Server.

I am using below code and added in global code of object

    public static class TableScriptGenerator
    {
        public static string GenerateTableScript(string serverInstance,string databaseName, string tableName, out string errorMessage)
        {
            try
            {
                //Server srv = new Server();
                //srv.ConnectionContext.Login = login;
                //srv.ConnectionContext.Password = password;
Server srv;
srv=new Server(serverInstance);
                Database db= new Database();
                db = srv.Databases[databaseName];
StringBuilder sb = new StringBuilder();
                Table tbl = db.Tables[tableName];
                ScriptingOptions options = new ScriptingOptions();
                options.ClusteredIndexes = true;
                options.Default = true;
                options.DriAll = true;
                options.Indexes = true;
                options.IncludeHeaders = true;
                StringCollection coll = tbl.Script(options);
                string script = string.Join(Environment.NewLine, coll);
                errorMessage = string.Empty;  // Clear any previous error message
                return script;
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;  // Assign the error message to the output variable
                return null;
            }
        }
    }

and I am calling function Generate Table script in code stage of object

string serverInstance = "abc\\SQLEXPRESS";

string databaseName = "Demo";
string tableName = "humanresources";

string script;
string errorMessage;

script = TableScriptGenerator.GenerateTableScript(serverInstance,databaseName, tableName, out errorMessage);

OutputVariable = script;
ErrorMessageOutputVariable = errorMessage;

In this I have added OutputVariable and ErrorMessageOutputVariable are output variables

I have added required dll's and namespace

I am getting error like cant connect to server

If anyone have idea on this, please help me on this

Thanks,

Soumya

SQL Server | Other
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Raksha Chourasia 75 Reputation points
    2023-06-13T06:38:43.76+00:00

    The error message "can't connect to server" comes only when your connection is yet to be built.

    You should first ensure that you are using the correct credentials to connect to your server.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.