【问题标题】:Why connecting to an OLEDB is giving me an connection error为什么连接到 OLEDB 给我一个连接错误
【发布时间】:2014-06-27 13:31:26
【问题描述】:

我有以下代码连接到我的数据库并从表中检索一些数据:

    string connectionString = "Data Provider=SQLOLEDB;Data Source=myserver;Initial Catalog=Db;Integrated Security=FALSE;user=zh;pwd=zh12;";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
    connection.Open();

    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    command.CommandText = "SELECT [Location], [URL], [TAGS] FROM [Db].[dbo].[BOOKINGTABLE]";
    command.CommandType = CommandType.Text;

    using (OleDbDataReader reader = command.ExecuteReader())
    {
        menu_ul_1.DataSource = reader;
        menu_ul_1.DataBind();
    }
}

我收到以下错误:

Exception Details: System.ArgumentException: An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'.

当我将连接字符串行更改为:

string connectionString = "Provider=SQLOLEDB;Data Source=myserver;Initial Catalog=Db;Integrated Security=FALSE;user=zh;pwd=zh12;";

我收到以下错误:

Exception Details: System.Data.OleDb.OleDbException: No error message available, result code: DB_E_ERRORSOCCURRED(0x80040E21).

Source Error: 


Line 23: using (OleDbConnection connection = new OleDbConnection(connectionString))
Line 24: {
Line 25:     connection.Open();
Line 26: 
Line 27:     OleDbCommand command = new OleDbCommand(); 

我该如何解决这个问题?

我的 Web.config 文件有以下行:

<add key="ConnStringTEST" value="Data Source=myserver;Initial Catalog=Db;Integrated Security=FALSE;user=zh;pwd=zh12;" />

如果我可以在我的 C# 代码中使用上述行,如何?

【问题讨论】:

标签: sql sql-server oledb


【解决方案1】:

经过多次故障排除后,我能够弄清楚为什么它不起作用。我重写了这样的字符串:

string cString = "Provider=sqloledb;Data Source=myserver;Initial Catalog=mydatabase;User Id=myid;Password=mypassword;";

这就像一个魅力,以防其他人遇到同样的问题。

【讨论】:

    【解决方案2】:

    在提供用户 ID 和密码时不要使用“集成安全性”。

    【讨论】:

      【解决方案3】:

      使用 Borat 在 cmets 中提到的 video,我能够参考连接字符串中的差异来调整我的。该视频演示了 Windows 身份验证,因此如果这不是您想要的,请务必添加您自己的用户 ID 和密码。

      我的问题是我的提供程序属性在连接到 DB/2 时引用:“Provider=IBMDASQL.DataSource.1”,但在视频中显示的连接字符串引用“IBMDA400.DataSource.1”

      搞笑,看完视频我就知道了,用过这个方法却忘记了。我们忘记事情的速度有多快。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多