Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7621007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:05:17+00:00 2026-05-31T04:05:17+00:00

I am using SMO to script out my objects from Sql server database using

  • 0

I am using SMO to script out my objects from Sql server database using .Net code. But as of now I am going through a sequential loop.

foreach(var table in TableCollection)
{
 var stringCollection=table.Script();
}

It is working fine. But when I convert the same loop to a Parallel.ForEach loop like:

Parallel.ForEach(TableCollection,table=>
{
 var stringCollection=table.Script();
});

It fails to script. Is there anybody who has used the same kind of approach or any other approach to script out objects from Sql server in parallel?

UPDATE :

I haven’t been able to work out Parallel LOOP as of now but I have used below mentioned code :

 server.SetDefaultInitFields(true);                

It improves performance up-to some extent.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:05:19+00:00Added an answer on May 31, 2026 at 4:05 am

    It seems SMO wasn’t built in a thread-safe manner. When you call Script() on a Table, it uses some shared state from its Server, so you can’t execute it on two tables from the same Server. But you can work around that by creating new Server object for each Table:

    private static TableCollection GetTables()
    {
        Server server = new Server(…);
        Database database = server.Databases[…];
        var tables = database.Tables;
        return tables;
    }
    
    …
    
    Parallel.For(0, GetTables().Count,
        i =>
        {
            var stringCollection = GetTables()[i].Script();
            …
        });
    

    This will make your queries parallel, but I have no idea whether it will make them actually faster.

    EDIT: If you want to create one Server for each thread, you could use an overload of Parallel.For() that allows thread-local initialization. Something like:

    Parallel.For(0, GetTables().Count,
        () => GetTables(),
        (i, _, tables) =>
        {
            var stringCollection = tables[i].Script();
            …
            return tables;
        },
        tables => { });
    

    This way, each thread will have its own Server object, but only one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the Microsoft.SqlServer.Management.Smo classes to script out SQL scripts for stored procedures,
How can I determine from code, if SQL Server Agent is running - using
I am doing a C# project which is using smo objects(Server,Database). I included using
I am automating login creations on a SQL Server 2008 database using SMO in
I'm enumerating all databases of an SQL Server 2005 instance using SMO like as
I am using SMO to create databases and tables on a SQL Server. I
I am trying to create a empty database in SQL server using powershell and
I wrote a Program that backup SQL Databases using SMO Library on .net ,
I'm trying to create a database through C# without using SMO, is it possible?!
I'm using SMO to execute a batch SQL script. In Management Studio, the script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.