gpt4 book ai didi

sql - 为什么连接到 OLEDB 会出现连接错误

转载 作者:行者123 更新时间:2023-12-04 14:20:12 27 4
gpt4 key购买 nike

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

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

我收到以下错误:

异常详细信息:System.ArgumentException:未在 ConnectionString 中指定 OLE DB 提供程序。例如,“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# 代码中使用以上行?

最佳答案

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

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

这很有用,以防其他人遇到同样的问题。

关于sql - 为什么连接到 OLEDB 会出现连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23570878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com