.NET to Sybase connection

Man of Honour
Joined
17 Feb 2003
Posts
29,640
Location
Chelmsford
Hi,

I'm trying to set up a .VB Console to run an SQL scrip on a Sybase server.. I found the following from a Google search. It's attempting a connection but nothing more.. I'm a bit puzzled the the code has no user name and password..


Code:
Imports System
Imports System.Data
Imports System.Data.OleDb

Module Program
    Sub Main(args As String())

        Dim ServerName As String = "ServerName:port"
        Dim DatabaseName As String = "mydatabase"
        Dim DbUserName As String = "user"
        Dim DbPassword As String = "password"
        Dim DoubleQuote As String = Chr(34)
        Dim QueryToExceute As String =
            $"{DoubleQuote}select * from table where x = y{DoubleQuote}"
        Dim ExportFileName As String =
            $"{DoubleQuote}C:\Users\me\Downloads\out.csv{DoubleQuote}"

        Dim Process = New Process()
        Process.StartInfo.UseShellExecute = False
        Process.StartInfo.RedirectStandardOutput = True
        Process.StartInfo.RedirectStandardError = True
        Process.StartInfo.CreateNoWindow = True
        Process.StartInfo.FileName = "SQLCMD.EXE"
        Process.StartInfo.Arguments =
            $"-S {ServerName} -d {DatabaseName}  -E -Q {QueryToExceute} -o {ExportFileName} -h-1 -s"","" -w 700"
        Process.StartInfo.WorkingDirectory = "C:\Data"
        Process.Start()
        Process.WaitForExit()
        Console.WriteLine("Done")
        Console.ReadLine()


        Console.ReadKey()

    End Sub
End Module

Any ideas where I'm going wrong?

Thanks
 
I would guess that the username and password should be included in this line:
Process.StartInfo.Arguments =
$"-S {ServerName} -d {DatabaseName} -E -Q {QueryToExceute} -o {ExportFileName} -h-1 -s"","" -w 700"
 
THanks touch... This is what I was thinking

Some thing like this

Process.StartInfo.Arguments =
$"-S {ServerName} -d {DatabaseName} -U {DbUserName} -P {DbPassword} -E -Q {QueryToExceute} -o {ExportFileName} -h-1 -s"","" -w 700"


Doesn't work though.
 
It'll depend on what the SQLCMD.EXE file is doing. The Process.StartInfo.Arguments are the values which are being passed into that executable to use to run.

There is also Process.StartInfo.UserName and Process.StartInfo.Password properties but I think these will be for the user account used to run the process rather than the account used to connect to the database server. If this code is running on the database server or if it's running on another machine on the same domain you might be able to use a windows account which has permissions to access the database.
 
Back
Top Bottom