SQL odbc 连接的连接字符串是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/269876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:10:36  来源:igfitidea点击:

What is the connection string for odbc connections?

asp.netsqlsql-serverconnectionconnection-string

提问by rksprst

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.

我一直在做网络应用程序,现在我需要做一个控制台应用程序。我需要同时使用 odbc 连接和常规连接。

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 = @".....";

在 web.config 中,但是我不确定如何使用内联代码做同样的事情。所以像 string connectionString = @".....";

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

我尝试了多种组合,在网上查看(包括 connectionstrings.com),但都没有奏效。

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

谁能帮我吗?我想要 odbc 和常规......因为它们看起来不同应该根据在线示例不同(不起作用)。

回答by x0n

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.

建立连接字符串的一个很酷的技巧是右键单击您的桌面,选择“新文本文档” - 这将创建一个临时记事本 .txt 文件。将其重命名为 .udl,然后双击它 - 您现在可以创建任何连接字符串。完成后单击确定并在记事本中打开文件以查看连接字符串。

UPDATED April 28, 2009 (powershell script):

2009 年 4 月 28 日更新(powershell 脚本):

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
}

回答by jonnii

You should be able to find whatever you need here:

你应该能够在这里找到你需要的任何东西:

http://www.connectionstrings.com/

http://www.connectionstrings.com/

For one of our apps we use this connection string:

对于我们的一个应用程序,我们使用此连接字符串:

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

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

回答by Gustavo Rubio

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/

http://www.connectionstrings.com/

They have plenty of examples there.

他们那里有很多例子。

回答by Nathan Koop

Have you tried something like this for SQLServer?

您是否为 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

这对于 ODBC

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

回答by Nathan Koop

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

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