473,770 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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[] sConnectionStri ng;
try
{
ProcessStartInf o psi = new ProcessStartInf o("osql.exe", "-Usa
-Psa");
psi.RedirectSta ndardOutput=fal se;
psi.RedirectSta ndardInput=fals e;
psi.RedirectSta ndardError=fals e;
psi.UseShellExe cute=false;
psi.CreateNoWin dow=true;
Process proc = Process.Start (psi);
ProcessOutputRe ader por = new ProcessOutputRe ader (proc);
por.Start();
proc.StandardIn put.WriteLine (@"go");
Console.ReadLin e();
proc.StandardIn put.WriteLine ("Hello");
proc.StandardIn put.WriteLine ("there");
}
catch (Exception e)
{
Console.WriteLi ne("{0}",e.ToSt ring());
}
}
class ProcessOutputRe ader
{
Process proc;
public ProcessOutputRe ader (Process proc)
{
this.proc = proc;
}
public void Start()
{
new Thread (new
ThreadStart(Rea dAll)).Start();
}
void ReadAll()
{
StreamReader reader =
proc.StandardOu tput;
string line;
while ((line =
reader.ReadLine ())!=null)
Console.WriteLi ne ("Process
output: {0}", line);
}
}
}

Sep 21 '06 #1
7 2283
PS

"gopal" <go*********@gm ail.comwrote in message
news:11******** **************@ d34g2000cwd.goo glegroups.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[] sConnectionStri ng;
try
{
ProcessStartInf o psi = new ProcessStartInf o("osql.exe", "-Usa
-Psa");
You want to use -U sa -P sa with the spaces.

PS

psi.RedirectSta ndardOutput=fal se;
psi.RedirectSta ndardInput=fals e;
psi.RedirectSta ndardError=fals e;
psi.UseShellExe cute=false;
psi.CreateNoWin dow=true;
Process proc = Process.Start (psi);
ProcessOutputRe ader por = new ProcessOutputRe ader (proc);
por.Start();
proc.StandardIn put.WriteLine (@"go");
Console.ReadLin e();
proc.StandardIn put.WriteLine ("Hello");
proc.StandardIn put.WriteLine ("there");
}
catch (Exception e)
{
Console.WriteLi ne("{0}",e.ToSt ring());
}
}
class ProcessOutputRe ader
{
Process proc;
public ProcessOutputRe ader (Process proc)
{
this.proc = proc;
}
public void Start()
{
new Thread (new
ThreadStart(Rea dAll)).Start();
}
void ReadAll()
{
StreamReader reader =
proc.StandardOu tput;
string line;
while ((line =
reader.ReadLine ())!=null)
Console.WriteLi ne ("Process
output: {0}", line);
}
}
}
Sep 21 '06 #2
PS

"gopal" <go*********@gm ail.comwrote in message
news:11******** **************@ d34g2000cwd.goo glegroups.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[] sConnectionStri ng;
try
{
ProcessStartInf o psi = new ProcessStartInf o("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.RedirectSta ndardOutput=fal se;
psi.RedirectSta ndardInput=fals e;
psi.RedirectSta ndardError=fals e;
psi.UseShellExe cute=false;
psi.CreateNoWin dow=true;
Process proc = Process.Start (psi);
ProcessOutputRe ader por = new ProcessOutputRe ader (proc);
por.Start();
proc.StandardIn put.WriteLine (@"go");
Console.ReadLin e();
proc.StandardIn put.WriteLine ("Hello");
proc.StandardIn put.WriteLine ("there");
}
catch (Exception e)
{
Console.WriteLi ne("{0}",e.ToSt ring());
}
}
class ProcessOutputRe ader
{
Process proc;
public ProcessOutputRe ader (Process proc)
{
this.proc = proc;
}
public void Start()
{
new Thread (new
ThreadStart(Rea dAll)).Start();
}
void ReadAll()
{
StreamReader reader =
proc.StandardOu tput;
string line;
while ((line =
reader.ReadLine ())!=null)
Console.WriteLi ne ("Process
output: {0}", line);
}
}
}
Sep 21 '06 #3

"gopal" <go*********@gm ail.comwrote in message
news:11******** **************@ d34g2000cwd.goo glegroups.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*********@gm ail.comwrote in message
news:11******** **************@ d34g2000cwd.goo glegroups.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*********@gm ail.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.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 = "someSQLInstanc e";
string user, password;
// Prompt user to enter user and password
Console.Write(" Enter User:");
user = Console.ReadLin e();
Console.Write(" Enter Password:");
password = Console.ReadLin e();
string osqlArgs = String.Format(" /S {0} /U {1} /P {2}", server, user,
password);
....

ProcessStartInf o psi = new ProcessStartInf o("osql.exe",os qlArgs);
// 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.Diagnost ics;
using System;
using System.Threadin g;

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

Console.Write(" Enter User:");
user = Console.ReadLin e();
Console.Write(" Enter Password:");
password = Console.ReadLin e();
string osqlArgs = String.Format(" /S {0} /D {1} /U {2} /P {3} /i {4}",
"local","mySVR" ,user,password, SQLFile);
ProcessStartInf o psi = new ProcessStartInf o("osql.exe",os qlArgs);
psi.UseShellExe cute=false;
psi.CreateNoWin dow=true;
psi.RedirectSta ndardOutput=tru e;
Process proc = Process.Start (psi);
ProcessOutputRe ader po = new ProcessOutputRe ader(proc);
po.Start();
}

class ProcessOutputRe ader
{
Process proc;
public ProcessOutputRe ader (Process proc)
{
this.proc = proc;
}

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

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

Willy Denoyette [MVP] wrote:
"gopal" <go*********@gm ail.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.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 = "someSQLInstanc e";
string user, password;
// Prompt user to enter user and password
Console.Write(" Enter User:");
user = Console.ReadLin e();
Console.Write(" Enter Password:");
password = Console.ReadLin e();
string osqlArgs = String.Format(" /S {0} /U {1} /P {2}", server, user,
password);
....

ProcessStartInf o psi = new ProcessStartInf o("osql.exe",os qlArgs);
// rest of your code
//....

Willy.
Sep 22 '06 #7

"gopal" <go*********@gm ail.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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("Kern el32")]
extern static IntPtr GetStdHandle(in t inp);
[DllImport("Kern el32")]
extern static bool GetConsoleMode( IntPtr handle, ref uint mode);
[DllImport("Kern el32")]
extern static bool SetConsoleMode( IntPtr handle, uint mode);
static uint ENABLE_ECHO_INP UT = 0x0004;
static void Main() {
...

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

static string GetPasswordFrom Console() {

IntPtr hCon = GetStdHandle(-10);
uint oldMode = 0;
GetConsoleMode( hCon, ref oldMode);
uint newMode = oldMode & ~ENABLE_ECHO_IN PUT;
SetConsoleMode( hCon, newMode);
// Prompt user to enter password
Console.Write(" Enter password: ");
string pwd = Console.ReadLin e();
Console.WriteLi ne();
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.Substrin g 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
14061
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 %ERRORLEVEL%.However whenever I have an errorof a wrong dbcompatibility error the retun code id 1 even though sql returns an iformation message from OSQL that the right copatibilty levels are 60,70 and 80.How can i get OSQL to return the right return code...
2
13500
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 background so my approach may not be the best way to tackle this. However, from the research I have done this approach seems reasonable. Also, I know about the undocumented procedure sp_MSforeachtable. That can give me a
2
18983
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 sql file with the database-table structures and the data in it, I thought using OSQL command line utilities should work to out this whole database in MSSQL server. I have run this command from command line:
2
8352
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 have no user for SQL server n it uses by default . I also tried with sa username by entering it with -U switch in command but still it gaves same error. Can anyone help me
24
4751
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 OLTP DB to a the target Operational Data Store. This is still in development so both DB's reside on the same machine for convenience. The stored proc runs successfully from within Query analyzer and this holds true on the following platforms: XP...
1
2379
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 database on each servers from my place. Of course, also it would have been to easy, I don't have all 'sa' password... Is that possible to use the 'guest' account to execute a query like
1
4681
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 Connection.Open() (Connect()) How do I find the username and password to enter in order to create a secure connecion? I installed the MSDE over 2 years ago and cannot remember the setup details I used.
5
5844
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 switches and of course the /N or /i switch to tell it what to run. I have also tried replacing the /U /P switches with the /E switch. My problem is that as long as I specify the users password on the OSQL line (either with /U & /P or with /E & /P) it...
11
3634
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 ProcessStartInfo("osql",""); psi.RedirectStandardInput = false;
0
9602
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8905
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.