我正在尝试将MS Access数据库连接到ASP.Net以获取数据。但是下面的错误不断弹出。 我发现它与Web.config连接有关,但是我在做什么错,我仍然不知道?
错误-
strong textSystem.ArgumentException:不支持关键字:“ provider”。 System.Data.Common.DbConnectionOptions..ctor处的System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable,String connectionString,Boolean buildChain,Hashtable同义词,Boolean firstKey)(System处的String.String.String,Hashtable同义词,Boolean useOdbcRules)位于System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey键,DbConnectionPoolGroupOptions poolOptions,DbConnectionOptions&userConnectionOptions)处的System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString,DbConnectionOptions先前)的Data.SqlClient.SqlConnectionString..ctor(String connectionString) System.Data.SqlClient.SqlClient.SqlConnection..ctor处的System.Data.SqlClient.SqlConnection.set_ConnectionString(String值)处的System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey键)System.Data处的System.Data.SqlClient.SqlConnection..ctor(String connectionString,SqlCredential凭据) Dummy.Page_Lo上的SqlClient.SqlConnection..ctor(String connectionString) C:\ xyz \ Dummy.aspx.cs:第19行中的ad(Object sender,EventArgs e)
<connectionStrings>
<add name="MS_Access_DatabaseConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\temp\\Microsoft Access Database\\MS_Access_Database.mdb;" providerName="System.Data.OleDb" />
</connectionStrings>
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["MS_Access_DatabaseConnectionString"].ConnectionString;
try
{
SqlConnection conn = new SqlConnection(connectionString);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
String sql = "SELECT [Bank], [Amount] FROM [BankDetails]";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Response.Write("" + reader.GetString(0));
}
}
}
}
catch (Exception ex)
{
Response.Write(ex);
}
答案 0 :(得分:1)
您应该使用SqlConnection
而不是使用OleDbConnection
,因为SqlConnection
试图将您的连接字符串读取为SQL Server连接字符串而不是Access数据库1:
try
{
OleDbConnection conn = new OleDbConnection(connectionString);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
String OleDb = "SELECT [Bank], [Amount] FROM [BankDetails]";
using (OleDbCommand cmd = new OleDbCommand(OleDb, conn))
{
using (OleDbDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Response.Write("" + reader.GetString(0));
}
}
}
}
catch (Exception ex)
{
Response.Write(ex);
}
答案 1 :(得分:1)
System.Data.OleDb.OleDbConnection;
添加到文件顶部,然后删除使用System.Data.SqlConnection;
将您的代码更改为。
using(OleDbConnection con = new OleDbConnection(connDB))//这是错误 { }