By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,056 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,056 IT Pros & Developers. It's quick & easy.

OSQL UserName and PWd

P: n/a
Hi,

I have a console application whose purpose 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

Here is the code
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);
}
}
}

Sep 21 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
PS

"gopal" <go*********@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
Hi,

I have a console application whose purpose 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

Here is the code
static void Main(string[] args)
{
String[] sConnectionString;
try
{
ProcessStartInfo psi = new ProcessStartInfo("osql.exe","-Usa
-Psa");
You want to use -U sa -P sa with the spaces.

PS

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);
}
}
}
Sep 21 '06 #2

P: n/a
PS

"gopal" <go*********@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
Hi,

I have a console application whose purpose 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

Here is the code
static void Main(string[] args)
{
String[] sConnectionString;
try
{
ProcessStartInfo psi = new ProcessStartInfo("osql.exe","-Usa
-Psa");
Also I think you will need to specify server -S and database name -d

so you end up with the equivalent of

osql -S myServer -d myDatabase -U user -P password
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);
}
}
}
Sep 21 '06 #3

P: n/a

"gopal" <go*********@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
| Hi,
|
| I have a console application whose purpose 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].
|
|
osql will not prompt for the password, you have to pass all arguments to the
osql command before strting osql.
That means you prompt the user to enter the arguments, read them using
ReadLine(), parse them and pass them to the argument string in your
Process.Start command.

Willy.


Sep 21 '06 #4

P: n/a
Hi Willy,

Is it something

1. call the OSQL with parameters of User, Password, Server, SQL files
[any order I guess]

2. Once the authentication is done, get the User & Pwd from, prompt the
user
as thogh he is entering the U & Pwd, then start installing the SQL
files?

Are these two steps in right order? Can you if you can send me the
possible code. I would be greatful to learn about this.

Thanks
JP

Willy Denoyette [MVP] wrote:
"gopal" <go*********@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
| Hi,
|
| I have a console application whose purpose 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].
|
|
osql will not prompt for the password, you have to pass all arguments to the
osql command before strting osql.
That means you prompt the user to enter the arguments, read them using
ReadLine(), parse them and pass them to the argument string in your
Process.Start command.

Willy.
Sep 22 '06 #5

P: n/a

"gopal" <go*********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
| Hi Willy,
|
| Is it something
|
| 1. call the OSQL with parameters of User, Password, Server, SQL files
| [any order I guess]
|
| 2. Once the authentication is done, get the User & Pwd from, prompt the
| user
| as thogh he is entering the U & Pwd, then start installing the SQL
| files?
|
| Are these two steps in right order? Can you if you can send me the
| possible code. I would be greatful to learn about this.
|
| Thanks
| JP
|

No, the right order is:
1. Prompt the user and password ...
2. Use the values from above to connect to SQL server using osql.

Something like this to get you started...

string server = "someSQLInstance";
string user, password;
// Prompt user to enter user and password
Console.Write("Enter User:");
user = Console.ReadLine();
Console.Write("Enter Password:");
password = Console.ReadLine();
string osqlArgs = String.Format(" /S {0} /U {1} /P {2}", server, user,
password);
....

ProcessStartInfo psi = new ProcessStartInfo("osql.exe",osqlArgs);
// rest of your code
//....

Willy.
Sep 22 '06 #6

P: n/a
Willy

I had tried the foll code, seems its working okay, but i have couple of
points to be clarified, the following is the code, can you pls verify
the code..

I want the password to be entered as ******, not as actual characters,
i am getting the output as

Enter User:sa
Enter Password:sa
Process output: 123(1 row affected)
Process output: (1 row affected)

i want to supress the 1>2>3 and want to display only the number of rows
affected as
1 row affected

using System.IO;
using System.Diagnostics;
using System;
using System.Threading;

public class class1
{
static void Main()
{
string server;
string user, password,SQLFile;
SQLFile = "tbContact.sql";

Console.Write("Enter User:");
user = Console.ReadLine();
Console.Write("Enter Password:");
password = Console.ReadLine();
string osqlArgs = String.Format(" /S {0} /D {1} /U {2} /P {3} /i {4}",
"local","mySVR",user,password,SQLFile);
ProcessStartInfo psi = new ProcessStartInfo("osql.exe",osqlArgs);
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
psi.RedirectStandardOutput=true;
Process proc = Process.Start (psi);
ProcessOutputReader po = new ProcessOutputReader(proc);
po.Start();
}

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

Willy Denoyette [MVP] wrote:
"gopal" <go*********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
| Hi Willy,
|
| Is it something
|
| 1. call the OSQL with parameters of User, Password, Server, SQL files
| [any order I guess]
|
| 2. Once the authentication is done, get the User & Pwd from, prompt the
| user
| as thogh he is entering the U & Pwd, then start installing the SQL
| files?
|
| Are these two steps in right order? Can you if you can send me the
| possible code. I would be greatful to learn about this.
|
| Thanks
| JP
|

No, the right order is:
1. Prompt the user and password ...
2. Use the values from above to connect to SQL server using osql.

Something like this to get you started...

string server = "someSQLInstance";
string user, password;
// Prompt user to enter user and password
Console.Write("Enter User:");
user = Console.ReadLine();
Console.Write("Enter Password:");
password = Console.ReadLine();
string osqlArgs = String.Format(" /S {0} /U {1} /P {2}", server, user,
password);
....

ProcessStartInfo psi = new ProcessStartInfo("osql.exe",osqlArgs);
// rest of your code
//....

Willy.
Sep 22 '06 #7

P: n/a

"gopal" <go*********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
| Willy
|
| I had tried the foll code, seems its working okay, but i have couple of
| points to be clarified, the following is the code, can you pls verify
| the code..
|
| I want the password to be entered as ******, not as actual characters,
| i am getting the output as
|

In this case you'll have to turn-off the console echo mode. There is no
support for this in the FCL, so you'll have to PInvoke some console API's.
Following is a sample.

using ...
using System.Runtime.InteropServices;
public class class1
{
[DllImport("Kernel32")]
extern static IntPtr GetStdHandle(int inp);
[DllImport("Kernel32")]
extern static bool GetConsoleMode(IntPtr handle, ref uint mode);
[DllImport("Kernel32")]
extern static bool SetConsoleMode(IntPtr handle, uint mode);
static uint ENABLE_ECHO_INPUT = 0x0004;
static void Main() {
...

password = GetPasswordFromConsole();
string osqlArgs = String.Format("....
...
} // end Main

static string GetPasswordFromConsole() {

IntPtr hCon = GetStdHandle(-10);
uint oldMode = 0;
GetConsoleMode(hCon, ref oldMode);
uint newMode = oldMode & ~ENABLE_ECHO_INPUT;
SetConsoleMode(hCon, newMode);
// Prompt user to enter password
Console.Write("Enter password: ");
string pwd = Console.ReadLine();
Console.WriteLine();
SetConsoleMode(hCon, oldMode);
return pwd;
}
| Enter User:sa
| Enter Password:sa
| Process output: 123(1 row affected)
| Process output: (1 row affected)
|
| i want to supress the 1>2>3 and want to display only the number of rows
| affected as
| 1 row affected
|
Well, just parse the returned string, but I'm affraid you will have to do it
yourself. Hint take a look at the String.Index and String.IndexOf and
String.Substring methods.

Willy.

Willy.


Sep 22 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.