Home All Groups Group Topic Archive Search About

OSQL userName and Password Prompt.

Author
21 Sep 2006 10:25 AM
gopal
Hi,

I am trying to call the OSQL utility from my C# console application and
i am having problems

i have the following code
In Main method

ProcessStartInfo psi = new ProcessStartInfo("osql","");
        psi.RedirectStandardInput = false;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = false;
        psi.CreateNoWindow = false;
        psi.UseShellExecute = false;
        Process pc = Process.Start(psi);
        Console.WriteLine(pc.StandardOutput.ReadToEnd());
        pc.WaitForExit();

i am not able to capture the username and password of the SQL server in
the command prompt..
can anyone please help me?

Author
21 Sep 2006 10:36 AM
Willy Denoyette [MVP]
Show quote Hide quote
"gopal" <gopal.sr***@gmail.com> wrote in message
news:1158834312.459600.30730@b28g2000cwb.googlegroups.com...
| Hi,
|
| I am trying to call the OSQL utility from my C# console application and
| i am having problems
|
| i have the following code
| In Main method
|
| ProcessStartInfo psi = new ProcessStartInfo("osql","");
| psi.RedirectStandardInput = false;
| psi.RedirectStandardOutput = true;
| psi.RedirectStandardError = false;
| psi.CreateNoWindow = false;
| psi.UseShellExecute = false;
| Process pc = Process.Start(psi);
| Console.WriteLine(pc.StandardOutput.ReadToEnd());
| pc.WaitForExit();
|
| i am not able to capture the username and password of the SQL server in
| the command prompt..
| can anyone please help me?
|

You need to pass them as command arguments (run osql /? for details).
I also do not see what you are trying to achieve here, osql is an
interactive utility, you will have to write commands to stdin and read
responses from stdout, that is redirect stdin and stdout. Why not simply run
osql from the command line?

Willy.
Are all your drivers up to date? click for free checkup

Author
21 Sep 2006 10:37 AM
Ciaran O''Donnell
What actually happens when this code runs?

Ciaran O'Donnell

Show quoteHide quote
"gopal" wrote:

> Hi,
>
> I am trying to call the OSQL utility from my C# console application and
> i am having problems
>
> i have the following code
> In Main method
>
> ProcessStartInfo psi = new ProcessStartInfo("osql","");
>         psi.RedirectStandardInput = false;
>         psi.RedirectStandardOutput = true;
>         psi.RedirectStandardError = false;
>         psi.CreateNoWindow = false;
>         psi.UseShellExecute = false;
>         Process pc = Process.Start(psi);
>         Console.WriteLine(pc.StandardOutput.ReadToEnd());
>         pc.WaitForExit();
>
> i am not able to capture the username and password of the SQL server in
> the command prompt..
> can anyone please help me?
>
>
Author
21 Sep 2006 10:42 AM
Ciaran O''Donnell
This code works for me inside a windows app and a console app in .NET 2.
It writes out:
Error: No user selected. Try with -U or -E switches

Ciaran O'Donnell

Show quoteHide quote
"gopal" wrote:

> Hi,
>
> I am trying to call the OSQL utility from my C# console application and
> i am having problems
>
> i have the following code
> In Main method
>
> ProcessStartInfo psi = new ProcessStartInfo("osql","");
>         psi.RedirectStandardInput = false;
>         psi.RedirectStandardOutput = true;
>         psi.RedirectStandardError = false;
>         psi.CreateNoWindow = false;
>         psi.UseShellExecute = false;
>         Process pc = Process.Start(psi);
>         Console.WriteLine(pc.StandardOutput.ReadToEnd());
>         pc.WaitForExit();
>
> i am not able to capture the username and password of the SQL server in
> the command prompt..
> can anyone please help me?
>
>
Author
21 Sep 2006 10:59 AM
gopal
I am puttng the full code now -

The following program when run, just displays command output without
getting displayed

I tried to get the Database UserName and Password from user, but in
vain.

using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;
using System.Threading;

namespace ConsoleApplication3
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>

        [STAThread]
        static void Main(string[] args)
        {
    try
    {

    ProcessStartInfo psi = new ProcessStartInfo("osql.exe","-Usa -Psa");
                psi.RedirectStandardOutput=true;
    psi.RedirectStandardInput=true;
    psi.RedirectStandardError=true;
    psi.UseShellExecute=false;
    psi.CreateNoWindow=true;
    Process proc = Process.Start (psi);
    ProcessOutputReader por = new ProcessOutputReader (proc);
    por.Start();
    proc.StandardInput.WriteLine (@"sp_help GO");
    proc.StandardInput.WriteLine();

    proc.StandardInput.WriteLine ("mohan");
    proc.StandardInput.WriteLine ("rahul");


        class ProcessOutputReader
        {
            Process proc;


            public ProcessOutputReader (Process proc)
            {
                this.proc = proc;
            }


            public void Start()
            {
            new Thread (new ThreadStart(ReadAll)).Start();
            }


            void ReadAll()
            {
            StreamReader reader = proc.StandardOutput;
            string line;
            while ((line = reader.ReadLine())!=null)
            Console.WriteLine ("Process output: {0}", line);
            }
        }



    }
}



Ciaran O''Donnell wrote:
Show quoteHide quote
> This code works for me inside a windows app and a console app in .NET 2.
> It writes out:
> Error: No user selected. Try with -U or -E switches
>
> Ciaran O'Donnell
>
> "gopal" wrote:
>
> > Hi,
> >
> > I am trying to call the OSQL utility from my C# console application and
> > i am having problems
> >
> > i have the following code
> > In Main method
> >
> > ProcessStartInfo psi = new ProcessStartInfo("osql","");
> >         psi.RedirectStandardInput = false;
> >         psi.RedirectStandardOutput = true;
> >         psi.RedirectStandardError = false;
> >         psi.CreateNoWindow = false;
> >         psi.UseShellExecute = false;
> >         Process pc = Process.Start(psi);
> >         Console.WriteLine(pc.StandardOutput.ReadToEnd());
> >         pc.WaitForExit();
> >
> > i am not able to capture the username and password of the SQL server in
> > the command prompt..
> > can anyone please help me?
> >
> >
Author
21 Sep 2006 11:43 AM
Willy Denoyette [MVP]
Show quote Hide quote
"gopal" <gopal.sr***@gmail.com> wrote in message
news:1158836389.836924.251110@i42g2000cwa.googlegroups.com...
|I am puttng the full code now -
|
| The following program when run, just displays command output without
| getting displayed
|
| I tried to get the Database UserName and Password from user, but in
| vain.
|
| using System;
| using System.Diagnostics;
| using System.IO;
| using Microsoft.Win32;
| using System.Threading;
|
| namespace ConsoleApplication3
| {
| /// <summary>
| /// Summary description for Class1.
| /// </summary>
| public class Class1
| {
| /// <summary>
| /// The main entry point for the application.
| /// </summary>
|
| [STAThread]
| static void Main(string[] args)
| {
| try
| {
|
| ProcessStartInfo psi = new ProcessStartInfo("osql.exe","-Usa -Psa");
|                psi.RedirectStandardOutput=true;
| psi.RedirectStandardInput=true;
| psi.RedirectStandardError=true;
| psi.UseShellExecute=false;
| psi.CreateNoWindow=true;
| Process proc = Process.Start (psi);
| ProcessOutputReader por = new ProcessOutputReader (proc);
| por.Start();
| proc.StandardInput.WriteLine (@"sp_help GO");
| proc.StandardInput.WriteLine();
|
| proc.StandardInput.WriteLine ("mohan");
| proc.StandardInput.WriteLine ("rahul");

You need to block the main thread, else your main thread will exit pulling
down stdin stderr and stdout

    proc.StandardInput.WriteLine (@"go");
    Console.ReadLine();
}
Author
21 Sep 2006 12:25 PM
gopal
Hi,

i tried putting the two lines, but i am getting the following error

Standard Out has not been redirected.

Atleast i would like the prompt to be displayed when the application is
started..for inutting Databaser UserName and Password..


Willy Denoyette [MVP] wrote:
Show quoteHide quote
> "gopal" <gopal.sr***@gmail.com> wrote in message
> news:1158836389.836924.251110@i42g2000cwa.googlegroups.com...
> |I am puttng the full code now -
> |
> | The following program when run, just displays command output without
> | getting displayed
> |
> | I tried to get the Database UserName and Password from user, but in
> | vain.
> |
> | using System;
> | using System.Diagnostics;
> | using System.IO;
> | using Microsoft.Win32;
> | using System.Threading;
> |
> | namespace ConsoleApplication3
> | {
> | /// <summary>
> | /// Summary description for Class1.
> | /// </summary>
> | public class Class1
> | {
> | /// <summary>
> | /// The main entry point for the application.
> | /// </summary>
> |
> | [STAThread]
> | static void Main(string[] args)
> | {
> | try
> | {
> |
> | ProcessStartInfo psi = new ProcessStartInfo("osql.exe","-Usa -Psa");
> |                psi.RedirectStandardOutput=true;
> | psi.RedirectStandardInput=true;
> | psi.RedirectStandardError=true;
> | psi.UseShellExecute=false;
> | psi.CreateNoWindow=true;
> | Process proc = Process.Start (psi);
> | ProcessOutputReader por = new ProcessOutputReader (proc);
> | por.Start();
> | proc.StandardInput.WriteLine (@"sp_help GO");
> | proc.StandardInput.WriteLine();
> |
> | proc.StandardInput.WriteLine ("mohan");
> | proc.StandardInput.WriteLine ("rahul");
>
> You need to block the main thread, else your main thread will exit pulling
> down stdin stderr and stdout
>
>     proc.StandardInput.WriteLine (@"go");
>     Console.ReadLine();
> }
Author
21 Sep 2006 1:41 PM
gopal
Any response??
gopal wrote:
Show quoteHide quote
> Hi,
>
> i tried putting the two lines, but i am getting the following error
>
> Standard Out has not been redirected.
>
> Atleast i would like the prompt to be displayed when the application is
> started..for inutting Databaser UserName and Password..
>
>
> Willy Denoyette [MVP] wrote:
> > "gopal" <gopal.sr***@gmail.com> wrote in message
> > news:1158836389.836924.251110@i42g2000cwa.googlegroups.com...
> > |I am puttng the full code now -
> > |
> > | The following program when run, just displays command output without
> > | getting displayed
> > |
> > | I tried to get the Database UserName and Password from user, but in
> > | vain.
> > |
> > | using System;
> > | using System.Diagnostics;
> > | using System.IO;
> > | using Microsoft.Win32;
> > | using System.Threading;
> > |
> > | namespace ConsoleApplication3
> > | {
> > | /// <summary>
> > | /// Summary description for Class1.
> > | /// </summary>
> > | public class Class1
> > | {
> > | /// <summary>
> > | /// The main entry point for the application.
> > | /// </summary>
> > |
> > | [STAThread]
> > | static void Main(string[] args)
> > | {
> > | try
> > | {
> > |
> > | ProcessStartInfo psi = new ProcessStartInfo("osql.exe","-Usa -Psa");
> > |                psi.RedirectStandardOutput=true;
> > | psi.RedirectStandardInput=true;
> > | psi.RedirectStandardError=true;
> > | psi.UseShellExecute=false;
> > | psi.CreateNoWindow=true;
> > | Process proc = Process.Start (psi);
> > | ProcessOutputReader por = new ProcessOutputReader (proc);
> > | por.Start();
> > | proc.StandardInput.WriteLine (@"sp_help GO");
> > | proc.StandardInput.WriteLine();
> > |
> > | proc.StandardInput.WriteLine ("mohan");
> > | proc.StandardInput.WriteLine ("rahul");
> >
> > You need to block the main thread, else your main thread will exit pulling
> > down stdin stderr and stdout
> >
> >     proc.StandardInput.WriteLine (@"go");
> >     Console.ReadLine();
> > }
Author
21 Sep 2006 2:35 PM
Willy Denoyette [MVP]
"gopal" <gopal.sr***@gmail.com> wrote in message
news:1158846093.539727.326660@e3g2000cwe.googlegroups.com...
| Any response??

Do you think this is a helpdesk or something?

I did not ask to add two lines, I asked to add a Console.ReadLine() to
prevent the main thread to exit before the sub-thread was even able to do
anything usefull.
If you need to get user and password, you can get them from the command line
and insert both in the string you pass as arguments to osql.

Willy.
Author
21 Sep 2006 3:27 PM
gopal
Willy, I am sorry for this..what i had mailed for..Here is the code i
have


static void Main(string[] args)
        {
            String[] sConnectionString;
            try
            {

    ProcessStartInfo psi = new ProcessStartInfo("osql.exe","-Usa -Psa");
    psi.RedirectStandardOutput=false;
    psi.RedirectStandardInput=false;
    psi.RedirectStandardError=false;
    psi.UseShellExecute=false;
    psi.CreateNoWindow=true;
    Process proc = Process.Start (psi);
    ProcessOutputReader por = new ProcessOutputReader (proc);
                por.Start();
    proc.StandardInput.WriteLine (@"go");
    Console.ReadLine();


    proc.StandardInput.WriteLine ("Hello");
    proc.StandardInput.WriteLine ("there");



    }
             catch (Exception e)
    {
        Console.WriteLine("{0}",e.ToString());
            }
    }
    class ProcessOutputReader
    {
            Process proc;


            public ProcessOutputReader (Process proc)
            {
                this.proc = proc;
            }


            public void Start()
            {
                new Thread (new ThreadStart(ReadAll)).Start();
            }


            void ReadAll()
            {

                StreamReader reader = proc.StandardOutput;
                string line;
                while ((line = reader.ReadLine())!=null)
                    Console.WriteLine ("Process output: {0}", line);
            }
        }

    }



It gives me error "Standard out has not been redirected"



Willy Denoyette [MVP] wrote:
Show quoteHide quote
> "gopal" <gopal.sr***@gmail.com> wrote in message
> news:1158846093.539727.326660@e3g2000cwe.googlegroups.com...
> | Any response??
>
> Do you think this is a helpdesk or something?
>
> I did not ask to add two lines, I asked to add a Console.ReadLine() to
> prevent the main thread to exit before the sub-thread was even able to do
> anything usefull.
> If you need to get user and password, you can get them from the command line
> and insert both in the string you pass as arguments to osql.
>
> Willy.
Author
21 Sep 2006 3:56 PM
Willy Denoyette [MVP]
"gopal" <gopal.sr***@gmail.com> wrote in message
news:1158852476.230622.99560@m7g2000cwm.googlegroups.com...
| Willy, I am sorry for this..what i had mailed for..Here is the code i
| have
|
You need this:
    psi.RedirectStandardOutput=true;
before starting osql.

And what's the purpose of this?
proc.StandardInput.WriteLine ("Hello");
what do you think osql will do with it?
Honestly I don't quite understand what you are trying to achieve. osql is a
end-user tool (a DBA utility) why are you wrapping this in yet another
console application, IMO it add's no value.

Willy.
Author
21 Sep 2006 4:14 PM
gopal
Hi

The purpose of this console application is to run the OSQL utility from
my console application.

When my application is run, the OSQL utility is started and it has to
prompt for Database UserName & Password [Database Name & SQL files will
be provided as other options for this OSQL].

But i am having problems in getting from the prompt, Database -
UserName & Password

Once I get userName & Password, i already have the Database Server Name
& SQL file
Now i will install the SQL files on the particular database





Willy Denoyette [MVP] wrote:
Show quoteHide quote
> "gopal" <gopal.sr***@gmail.com> wrote in message
> news:1158852476.230622.99560@m7g2000cwm.googlegroups.com...
> | Willy, I am sorry for this..what i had mailed for..Here is the code i
> | have
> |
> You need this:
>     psi.RedirectStandardOutput=true;
> before starting osql.
>
> And what's the purpose of this?
> proc.StandardInput.WriteLine ("Hello");
> what do you think osql will do with it?
> Honestly I don't quite understand what you are trying to achieve. osql is a
> end-user tool (a DBA utility) why are you wrapping this in yet another
> console application, IMO it add's no value.
>
> Willy.



Post Thread options