Solved

vb.net shell problem

Posted on 2009-05-15
14
674 Views
Last Modified: 2012-05-07
Hi experts,

i'm using shell to execute a command line command

the code i'm using is below

if i run the code below i see a quickly closing shell window, and i can tell that mysql was run with incorrect parameters. (the mysql help/usage text scrolls before closing)

if i break on Shell(cmd,.....) and do
?cmd
in the immediate window i get a string
""C:\Program Files\MySql\MySQL Server 5.1\bin\mysql.exe" -uMyUser -pMyPass -h192.268.1.105 MyDB < "r:\visual studio 2008\projects\MyApp\sql\drop_system.sql""

if i copy the string that is inside the outer "", paste that in a command prompt it runs fine.

what am I missing?


Private Const MYSQL_APP as string = "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"
Dim dir as DirectoryInfo = new DirectoryInfo("r:\visual studio 2008\projects\MyApp\sql\"
dim File as FileInfo=dir.getFiles("*.sql")(0)
 
 
Dim cmd As String = """" & MYSQL_APP & """" & " -uMyUser -pMyPass -h192.168.1.105 MyDB < " & """" & file.DirectoryName & "\drop_system.sql" & """"
Shell(cmd, AppWinStyle.NormalFocus, True)

Open in new window

0
Comment
Question by:sgaggerj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
14 Comments
 
LVL 4

Accepted Solution

by:
Seblue earned 500 total points
ID: 24397721
Hi sgaggerj,

It does not really answer your question, but personaly, I use the class Process to start a external program. The "Process" class has many functionnalities, very useful. You may try something like:

Process.Start(MYSQL_APP," -uMyUser -pMyPass -h192.168.1.105 MyDB < " & """" & file.DirectoryName & "\drop_system.sql" & """")

You are also able to monitor it, checking the result code, and many things:
Dim P As Process = Process.Start(...,...)
P.WaitForExit

or even define other credentials.

The StartProcess function has many overloads. Have a look at it.

Hope this helps.
Seblue.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24397817
i had tried that method also, and it seems to do the same thing, open a window, scroll the text and close.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24397873
I tried again with the method you suggested Seblue, and was able to pause the output of the window.
It doesn't seem like the arguments are getting passed for some reason

Here's the output I was able to pause:

C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe  Ver 14.14 Distrib 5.1.31,
 for Win32 (ia32)
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Usage: C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe [OPTIONS] [database
]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --auto-rehash       Enable automatic rehashing. One doesn't need to use
                      'rehash' to get table and field completion, but startup
                      and reconnecting may take a longer time. Disable with
                      --disable-auto-rehash.
  -A, --no-auto-rehash
                      No automatic rehashing. One has to use 'rehash' to get
                      table and field completion. This gives a quicker start of
                      mysql and disables rehashing on reconnect. WARNING:
                      options deprecated; use --disable-auto-rehash instead.
  -B, --batch         Don't
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 1

Author Comment

by:sgaggerj
ID: 24397946
i paused the output of the shell as well and come up with the same thing.
0
 
LVL 4

Expert Comment

by:Seblue
ID: 24397973
sgaggerj,

Unfortunately, I am not able to have a try with SQL. But you may try another thing. You said it is running with the command prompt. Then try to run a prompt with your full shell command like this:

Shell("cmd","/c " & cmd)

This will run your shell command through the cmd.exe of the system. (That's just an idea for now).

Seblue.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24398043
ran your code as is (using my creation of 'cmd' ) and received the following error

An error occurred creating the form. See Exception.InnerException for details.  The error is: Conversion from string "/c "C:\Program Files\MySQL\MySQL" to type 'Short' is not valid.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24398048
well 'command prompt' in that shell opens up a command prompt type window
0
 
LVL 4

Expert Comment

by:Seblue
ID: 24398084
That's right. CMD.EXE may not support parameters having filenames with space. Apologies.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24398092
thats why i coded to have " " around the long file names.
i can re-try it with dos 8.1 names
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24398169
Error: "An error occurred creating the form. See Exception.InnerException for details.  The error is: Conversion from string "/c C:\Progra~1\MySQL\MySQLS~1.1\" to type 'Short' is not valid."
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24398328
apparently it didn't like the '<' in the argument list.
i created a batch file

@echo off
set logfile=log.log
echo arguments > %logfile%
echo %1 >> %logfile%
echo %2 >> %logfile%
echo %3 >> %logfile%
echo %4 >> %logfile%
echo %5 >> %logfile%

cls
mysql -u%1 -p%2 -h%3 %4 < %5
pause
exit

and it still didn't work.

i then realized there were actually 6 arguments getting passed (the '<' was one)
and i modified the batch file to

@echo off
set logfile=log.log
echo arguments > %logfile%
echo %1 >> %logfile%
echo %2 >> %logfile%
echo %3 >> %logfile%
echo %4 >> %logfile%
echo %6 >> %logfile%

cls
mysql -u%1 -p%2 -h%3 %4 < %6
pause
exit

it still didn't like it.

so i reverted back to the original version of the batch file, removed the '<' from the cmd string and it works, however there's a problem getting to the mysql server so that's another issue entirely.

is there an escape character that can be used in conjunction with the '<' so that dos doesn't think it's some pipe of sorts?
0
 
LVL 4

Expert Comment

by:Seblue
ID: 24398795
I tried :

Shell("cmd.exe /c more < """"c:\sometext.txt""")

That works. So I am not sure the pb comes from the "<".
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 24399040
i know it worked (the full string) if i went to the command prompt and used long file names with quotes.
it didn't work with a shell() or Process though.

i tried it with a batch file and the < was causing problems. (at least in my implementation).
without the < in the string, it's working fine via a batch file, which is not 100% the way i wanted to do it, but it seems to get the job done.
0
 
LVL 4

Expert Comment

by:Seblue
ID: 24399399
Last comment. Remember when I asked you to tried Shell("cmd","/c " & cmd)? I mixed up both shell and process.start. My mistake. I think Shell("cmd /c " & cmd) would do the job.

I had some readings regarding the "<" and its seems that this char is only used correctly with cmd.exe.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question