I don't know why this error, I tried everything. I want to connect my webForm to the Database .accdb and when I use using(){} I got this error "Keyword not supported: 'provider" Here is the code:
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
{
// .....
}
}
Creating a SqlConnection Object A SqlConnection is an object, just like any other C# object. Most of the time, you just declare and instantiate the SqlConnection all at the same time, as shown below: SqlConnection conn = new SqlConnection( "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
ADO.NET SqlConnection Class. It is used to establish an open connection to the SQL Server database. It is a sealed class so that cannot be inherited. SqlConnection class uses SqlDataAdapter and SqlCommand classes together to increase performance when connecting to a Microsoft SQL Server database.
The SqlConnection draws an open connection from the connection pool if one is available. Otherwise, it establishes a new connection to an instance of SQL Server. If the SqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close.
The sqlConnection will close the connection after it will pass using block and call Dispose method.
Aleksey Mynkov has it right. But here is more detail since you are needing more clarification.
Your web.config is fine. The auto-generated Visual Studios connection string is using the right setup. Instead, on your webform1 file you need to do 2 things.
Add using System.Data.OleDb.OleDbConnection;
to the top of your file, and remove the using System.Data.SqlConnection;
Change your webform1 code to be:
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
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With