MS Access 与 ASP.NET 的连接

我正在尝试将 MS Access 数据库连接到 ASP.Net 以获取数据。但是下面的错误不断弹出。我认为它与 Web.config 连接有关,但我做错了什么,我仍然无法弄清楚?


错误-


strong textSystem.ArgumentException:不支持关键字:“提供者”。在 System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) 在 System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) 在 System. Data.SqlClient.SqlConnectionString..ctor(String connectionString) at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey 键)在 System.Data.SqlClient。


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

    }


Qyouu
浏览 232回答 2
2回答

阿波罗的战车

将 using 添加System.Data.OleDb.OleDbConnection;到文件顶部,并删除 usingSystem.Data.SqlConnection;将您的代码更改为。using (OleDbConnection con = new OleDbConnection(connDB)) //这里是错误{}

互换的青春

而不是使用SqlConnection,您应该使用OleDbConnection,因为SqlConnection试图将您的连接字符串读取为 SQL Server 连接字符串而不是 Access 数据库之一:try{&nbsp; &nbsp; OleDbConnection conn = new OleDbConnection(connectionString);&nbsp; &nbsp; if (conn.State == ConnectionState.Closed)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; conn.Open();&nbsp; &nbsp; }&nbsp; &nbsp; String OleDb = "SELECT [Bank], [Amount] FROM [BankDetails]";&nbsp; &nbsp; using (OleDbCommand cmd = new OleDbCommand(OleDb, conn))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; using (OleDbDataReader reader = cmd.ExecuteReader())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (reader.Read())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Response.Write("" + reader.GetString(0));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}catch (Exception ex){&nbsp; &nbsp; Response.Write(ex);}
打开App,查看更多内容
随时随地看视频慕课网APP