views:

2553

answers:

5

I've always done web apps and now I need to do a console app. I need to use both an odbc connection and a regular connection.

In the past I would have used:

<add name="LinkConnectionString" connectionString="Data Source=SERENITY\SQLEXPRESS;Initial Catalog=Link;Integrated Security=True" providerName="System.Data.SqlClient"/>

In the web.config, however I am not sure how to do the same thing with inline code. So like string connectionString = @".....";

I have tried multiple combinations, looked online (including connectionstrings.com), but none of them worked.

Can anyone help me out? I want both the odbc and the regular... as they seem different should be different according to the sample ones online (that don't work).

+8  A: 

You should be able to find whatever you need here:

http://www.connectionstrings.com/

For one of our apps we use this connection string:

"DRIVER={driver};SERVER=server.database;UID=username;PWD=password"

jonnii
note: "I have tried multiple combinations, looked online (including connectionstrings.com), but none of them worked. "
Nathan Koop
i gave you an example too.
jonnii
+1  A: 

I think it deppends as to what database you want to connect, because of the Driver that its used to connect to the database engine.

You might want to take a look at:

http://www.connectionstrings.com/

They have plenty of examples there.

Gustavo Rubio
note: I have tried multiple combinations, looked online (including connectionstrings.com), but none of them worked.
Nathan Koop
A: 

Have you tried something like this for SQLServer?

  SqlConnection conn = new SqlConnection(@"Data Source=SERENITY\SQLEXPRESS;Initial Catalog=Link;Integrated Security=True");
  SqlCommand cmd = new SqlCommand("SELECT * FROM tableName", conn);
  conn.Open();
  //<snip> Run Command
  conn.Close();

and this for ODBC

OdbcConnection conn = new OdbcConnection(@"ODBC connection string");
OdbcCommand cmd = new OdbcCommand("SELECT * FROM tableName", conn);
conn.Open();
//Run Command
conn.Close();
Nathan Koop
+18  A: 

A cool trick to building connection strings is to right click on your desktop, choose "new text document" - this will make a temporary notepad .txt file. Rename it to .udl and then double click it - you can now create any connection string. Click ok when done and open the file in notepad to see the connectionstring.

UPDATED April 28, 2009 (powershell script):

function get-oledbconnection ([switch]$Open) {
    $null | set-content ($udl = "$([io.path]::GetTempPath())\temp.udl");
    $psi = new-object Diagnostics.ProcessStartInfo
    $psi.CreateNoWindow = $true
    $psi.UseShellExecute = $true
    $psi.FileName = $udl
    $pi = [System.Diagnostics.Process]::Start($psi)
    $pi.WaitForExit()
    write-host (gc $udl) # verbose 
    if (gc $udl) {
        $conn = new-object data.oledb.oledbconnection (gc $udl)[2]
        if ($Open) { $conn.Open() }
    }
    $conn
}
x0n
this is very cool, I did not know this.
Sara Chipps
I agree. I wonder how long this trick has been around. I'm amazed that I did not know this. A truly bad-ass trick!
EnocNRoll
How can this only have 15 votes? One of the most incredible hidden gems I've yet seen.
Thomas
A: 

<add name="myName" connectionString="dsn=myDSN;UID=myUID;" providerName="System.Data.Odbc" />