OSQL userName and Password Prompt.

G

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?
 
W

Willy Denoyette [MVP]

| 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.
 
G

Guest

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
 
G

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



}
}
 
W

Willy Denoyette [MVP]

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

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..
 
G

gopal

Any response??
gopal said:
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..

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

Willy Denoyette [MVP]

| 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.
 
G

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"
 
W

Willy Denoyette [MVP]

| 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.
 
G

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top