473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

OSQL UserName and PWd

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
7 2258
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
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

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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ajay Garg | last post by:
I am running the following OSQL command and capturing the return code for the error .Whenver i have an error like server not exists or uable to login I get a return code of 1 for the...
2
by: Bob | last post by:
Everybody, I've been doing a lot of on-line research and cannot find any reference to the exact problem I'm having. Let me preface this question with the fact that I'm coming from an Oracle...
2
by: Murtix Van Basten | last post by:
Hi, I have dumped a very large database from mysql (using mysqldump program) as a raw sql file. The reason was, convert this database to a MSSQL database. Since mysqldump creates the file as raw...
2
by: arfanameer | last post by:
I have to create database usign OSQL. i am usin a command like this C:> osql -E -i db1.sql But it doesnt create database and returns error saying user login failed for administrator whereas I...
24
by: LineVoltageHalogen | last post by:
Greetings All, I was hoping that someone out there has run into this issue before and can shed some light on it for me. I have a stored procedure that essentially does a mini ETL from a source...
1
by: Florimond | last post by:
Can I use osql to known the database install on a server via 'guest' account ? I had over 300 servers with sql server all around France (different version : 6.5, 7 and 8) I need to check all...
1
by: Dundealing | last post by:
I have tried to follow an example in a book using OSQL. However, the first command: osql -E returns an error message, saying the server does not exist or access denied. It then goes on to say...
5
by: mike | last post by:
I have a question. I am doing some work for someone and I have a batch file that they can run that will execute an OSQL line and a DTSRUN line. In both lines I run them using the /S /U /P...
11
by: gopal | last post by:
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.