OSQL UserName and PWd

G

gopal

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


}
 
P

PS

gopal said:
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
 
P

PS

gopal said:
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
 
W

Willy Denoyette [MVP]

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

gopal

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
 
W

Willy Denoyette [MVP]

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

gopal

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: 1> 2> 3> (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);
}
}
}
 
W

Willy Denoyette [MVP]

| 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: 1> 2> 3> (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.
 

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