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

man*_*iqi 13 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>
Run Code Online (Sandbox Code Playgroud)

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
   {
       // .....                            
   }              
}
Run Code Online (Sandbox Code Playgroud)

JCl*_*ill 21

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(connDB))  //here is the error
        {
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)


Sha*_*neh 8

我知道这有点旧线程并已经回答,但我正在添加我的解决方案以供将来参考

我有SQL Server 11.0数据库,当我尝试在SharePoint应用程序中使用它时遇到错误,我没有尝试过其他建议的答案,但我只是删除了"提供者"部分(并重新排序),所以我的连接字符串看起来像这样:

Provider=SQLOLEDB.1;Password=DBPassword;Persist Security Info=True;User ID=sa;Initial Catalog=DBName;Data Source=DBServer
Run Code Online (Sandbox Code Playgroud)

现在看起来像这样:

Data Source=DBServer;Initial Catalog=DBName;Persist Security Info=True;User ID=sa;Password=DBPassword;
Run Code Online (Sandbox Code Playgroud)

它运作得很好


小智 5

你应该用System.Data.OleDb.OleDbConnection.