为了账号安全,请及时绑定邮箱和手机立即绑定

MS Access 与 ASP.NET 的连接

MS Access 与 ASP.NET 的连接

C#
Qyouu 2022-01-09 17:11:16
我正在尝试将 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);    }
查看完整描述

2 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

  1. 将 using 添加System.Data.OleDb.OleDbConnection;到文件顶部,并删除 usingSystem.Data.SqlConnection;

  2. 将您的代码更改为。

    using (OleDbConnection con = new OleDbConnection(connDB)) //这里是错误{}


查看完整回答
反对 回复 2022-01-09
?
互换的青春

TA贡献1797条经验 获得超6个赞

而不是使用SqlConnection,您应该使用OleDbConnection,因为SqlConnection试图将您的连接字符串读取为 SQL Server 连接字符串而不是 Access 数据库之一:


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

}


查看完整回答
反对 回复 2022-01-09
  • 2 回答
  • 0 关注
  • 174 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信