【发布时间】: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# 代码中使用上述行,如何?
【问题讨论】:
-
不要将 OleDb 用于 SQL Server 连接。使用SqlClient,连接字符串参考connectionstrings.com/sql-server
-
@YuriyGalanter - 你为什么这么说?我曾经使用
Provider=SQLNCLI10和 Provider=SQLNCLI.1 或类似的东西,但不知道为什么。它刚刚奏效。 -
@BoratSagdiyev 我怎么知道哪个是我的?
-
@SiKni8 - 我也想知道你的问题的答案。你可以尝试使用这个网站来生成你的字符串 - developerfusion.com/tools/sql-connection-string。更好的是,尝试 yuriy 链接中给出的一些字符串。
-
@SiKni8 - 如果你有 Visual Studio,那么你可以试试这个视频来创建字符串 - youtube.com/watch?v=OEcd5oRRPmM 更多在这里 - stackoverflow.com/questions/10479763/…
标签: sql sql-server oledb