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

  • SEARCH
  • Home
  • 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 7517763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:33:14+00:00 2026-05-30T01:33:14+00:00

I know that my title seems with others from StackOverflow but my intention is

  • 0

I know that my title seems with others from StackOverflow but my intention is other.

I created a C# function which creates BCP files for every table from my database using BCP command. The below code is a sample, part of function.

List<string> tables = GetTables(); // a function which takes all tables from my database

string command = @"bcp ADatabase.dbo.{0} out {0}.bcp -n -Usa -Ppassword";

ProcessStartInfo processInfo = null;
     try {
        foreach ( string table in tables ) {
           command = string.Format( command, table );
           processInfo = new ProcessStartInfo( "cmd", "/c " + command );
           processInfo.RedirectStandardOutput = true;
           processInfo.CreateNoWindow = false;
           Process process = new Process();
           process.StartInfo = processInfo;
           process.Start();
           string result = process.StandardOutput.ReadToEnd();
           Console.WriteLine( result );
        }
     } catch ( Exception e ) {
        Console.WriteLine( e.Message );
     }

I want to avoid executing multiple windows and I want to execute each command line in same window.

My code is good or is an alternative ?

Thanks

  • 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-30T01:33:15+00:00Added an answer on May 30, 2026 at 1:33 am

    I’m not sure if there’s a way to open one command window and then keep posting your commands to it, that would be nice if someone had a clever way of doing that.

    You could use Streamwriter to write a batch file with all the commands you want in it, and then run the batch file. That would at least only open one process.

    StreamWriter sw = new StreamWriter("MyBatchFile.bat");
                string command = @"bcp ADatabase.dbo.{0} out {0}.bcp -n -Usa -%1";
                ProcessStartInfo processInfo = null;
                try
                {
                    foreach (string table in tables)
                    {
                        command = string.Format(command, table);
                        sw.WriteLine(command);                                        
                    }
                    sw.Flush();
                    sw.Close();
                    processInfo = new ProcessStartInfo("cmd", "/c MyBatchFile.Bat Ppassword");
                    processInfo.RedirectStandardOutput = true;
                    processInfo.CreateNoWindow = false;
                    Process process = new Process();
                    process.StartInfo = processInfo;
                    process.Start();
                    string result = process.StandardOutput.ReadToEnd();
                    Console.WriteLine(result);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
    

    The batch file has the “%1” in the command line so when you start the process you pass the password for the database to the batch file

    ProcessStartInfo("cmd", "/c MyBatchFile.Bat Ppassword");
    

    This should make sure that if anyone ever found the batch file, it just has %1 where the password would be.

    Not sure if this is better than what you have already… but it’s only opening one command window 🙂

    Martyn

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

Sidebar

Related Questions

i didn't really know how to title this question, but here's a thing that
The title says it! I know that Jon Skeet was working on an implementation
Suppose that my index has 3 fields: title, x and y. I know one
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
Morning all, I know that this sounds like a simple referencing problem from the
I know the title may seem strange but this is what I want to
I know the title of the post is bad but hear me out. A
I have a CWnd window which I create dynamically, and it seems that the
I have a CWnd window which I create dynamically, and it seems that the
EDIT: changed the title. I didn't know it at the time but this is

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.