John
John

Reputation: 190

How to pass a sql connections string value to a C# program

I am very new to programming world (sorry if this question has been answered many times, at least i didnt get any clue from searching the internet), and i am trying to build a C sharp app recently. This exe will be called in another program, and upon calling, the program will pass a sql connection string value the c sharp exe app. My question is how to make the app receive the value.

the connection string should look like

“Provider=SQLOLEDB.1;Password=xxxxxxxx;Persist Security Info=True;User
ID=xxxxxxxx;Initial Catalog=ATTACH;Data Source=LNGSEAL136504A;
Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation 
ID=LNGSEAL136504A;Use Encryption for Data=False;Tag with column collation when 
possible=False”

What I wrote : Here is my code: class Program { static void Main(string[] args) { Console.WriteLine("its running!"); Console.ReadKey(); try {

            int Len;
            string cnn = null;
            Len = args.Length;
            for (int i = 0; i < args.Length; i++)
            {
                cnn = cnn + " " + args[i];
            }
            Console.WriteLine(cnn);
            Console.WriteLine(Len);               

            Console.ReadKey();

            ADODB.Connection mycnn = new ADODB.Connection();
            mycnn.Open(cnn);
            ADODB.Recordset rs = new ADODB.Recordset();
            rs.Open("sql statements", mycnn);



            mycnn.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            Console.ReadKey();
        }
    }
}

but it throws me a "unable to cast a com object error". Any idea? Please advise.

[Solved]

I used oledbconnection instead of adodb, and it works with no problem. Thanks for everyone who helped.

Upvotes: 1

Views: 3981

Answers (2)

Mohammad Soliman
Mohammad Soliman

Reputation: 1

it is very easy in c#

in sender application by using Process.start("your exe path which you want send Connection string to it","connection string ");

in reciver application and in your exe in main method (founded in program class)

main(string[] args)

args[0] will hold the connection string sended by other program.

try ..

Upvotes: 0

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107508

You can pass the connection string via command line arguments (using whichever technique the source application uses to invoke an executable), and then you can retrieve them from the args array in the Main(string[] args) function of your receiving executable.

For example, on the receiving end:

class TestClass
{
    static void Main(string[] args)
    {
        // avoiding empty/null checks for simplicity
        string connectionString = args[n]; // where "n" is index value of your
                                           // connection string argument

        DoWork(connectionString);
    }
}

With that in mind, there might be better ways to accomplish what you're asking. If you described your project to us with a little more detail we may be able to provide alternate suggestions.

Upvotes: 5

Related Questions