简体   繁体   English

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

[英]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. 我需要同时使用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. 在web.config中,但我不知道如何使用内联代码执行相同的操作。 So like string connectionString = @"....."; 就像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和常规...因为他们看起来不同应该根据在线样本(不起作用)不同。

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. 构建连接字符串的一个很酷的技巧是右键单击桌面,选择“新文本文档” - 这将生成一个临时的记事本.txt文件。 Rename it to .udl and then double click it - you can now create any connection string. 将其重命名为.udl然后双击它 - 您现在可以创建任何连接字符串。 Click ok when done and open the file in notepad to see the connectionstring. 完成后单击“确定”,然后在记事本中打开文件以查看connectionstring。

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
}

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 = {驱动器}; SERVER = server.database; UID =用户名; PWD =密码”

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. 他们有很多例子。

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();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM