Thursday, February 17, 2011

What is the connection string for odbc connections?

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).

From stackoverflow
  • 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"

    Nathan Koop : note: "I have tried multiple combinations, looked online (including connectionstrings.com), but none of them worked. "
    jonnii : i gave you an example too.
  • 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.

    Nathan Koop : note: I have tried multiple combinations, looked online (including connectionstrings.com), but none of them worked.
  • 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();
    
  • 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
    }
    
    Sara Chipps : this is very cool, I did not know this.
    EnocNRoll : 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!
    Thomas : How can this only have 15 votes? One of the most incredible hidden gems I've yet seen.
  • <add name="myName" connectionString="dsn=myDSN;UID=myUID;" providerName="System.Data.Odbc" />

0 comments:

Post a Comment