c# - 不支持关键字 : 'provider' . 打开 SqlConnection

标签 c# asp.net

我不知道为什么会出现这个错误,我什么都试过了。我想将我的 webForm 连接到数据库 .accdb 当我使用 using(){} 时出现错误“不支持关键字:‘provider’” 这是代码:

web.config

<connectionStrings>
    <add name="ConnectionString"
    connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Manuel_2\Documents\Login.accdb"     
    providerName="System.Data.OleDb" />
</connectionStrings>

WebForm1

private static string conDB =        
            ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;    

protected void Page_Load(object sender, EventArgs e)
{    
   using (SqlConnection con = new SqlConnection(connDB))  //here is the error
   {
       // .....                            
   }              
}

最佳答案

Aleksey Mynkov 说得对。但这里有更多详细信息,因为您需要更多说明。

您的 web.config 没问题。自动生成的 Visual Studios 连接字符串使用正确的设置。相反,在您的 webform1 文件中,您需要做两件事。

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

  2. 将您的 webform1 代码更改为:

     private static string conDB = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
     protected void Page_Load(object sender, EventArgs e)
     {
         using (OleDbConnection con = new OleDbConnection(conDB))  //here is the error
         {
         }
     }
    

关于c# - 不支持关键字 : 'provider' . 打开 SqlConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27387912/

相关文章:

c# - Protobuf-net 不序列化基类成员

C# 在静态上下文中访问 IModelMetaDataProvider - IHtmlHelper

c# - 在 C# 中查找鼠标的速度

c# - 将相当于关联数组的内容从 Controller 传递到 MVC 4 中的 View 的最佳方法

javascript - 制作类似facebook的悬停弹出框效果

asp.net - 如何从代码隐藏更改 CSS 类?

c# - Dapper.SimpleCRUD 没有标识的插入问题

c# - 如何将项目添加到ListBox 列表的开头?

c# - 如何在 ASP.NET Web 应用程序中显示错误消息

c# - 如何使用阅读更多链接限制 GridView 中的标签字符串长度?