.net&访问数据库连接问题?

时间:2015-04-08 11:05:22

标签: c# asp.net .net ms-access

我刚刚开始使用Visual Studio 2013 for web来创建一个使用空白表单的网站,我正在努力(我认为)是一个Access数据库连接。我已经创建了可以提取和更新数据库中的数据的表单,但是当我尝试使用文本框创建注册表单来插入数据时,我收到以下错误消息。我知道这段代码不是很好我刚刚开始学习这个!请有人帮帮我吗?

错误讯息:

  

System.ArgumentException:不支持关键字:'provider'。在   System.Data.Common.DbConnectionOptions.ParseInternal(哈希表   parsetable,String connectionString,Boolean buildChain,Hashtable   同义词,布尔值firstKey)at   System.Data.Common.DbConnectionOptions..ctor(String connectionString,   Hashtable的同义词,Boolean useOdbcRules)at   System.Data.SqlClient.SqlConnectionString..ctor(字符串   connectionString)at   System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(字符串   connectionString,DbConnectionOptions上一页)at   System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey   key,DbConnectionPoolGroupOptions poolOptions,DbConnectionOptions&   userConnectionOptions)at   System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey   关键)at   System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)   在System.Data.SqlClient.SqlConnection..ctor(String connectionString,   SqlCredential凭证)   System.Data.SqlClient.SqlConnection..ctor(String connectionString)at   Registration.Button1_Click(Object sender,EventArgs e)in   c:\ Users \ lou \ Documents \ Visual Studio   2013 \ WebSites \ BeautyExperts \ Registration.aspx.cs:第22行

Registion.aspx.cs代码:

    `enter code here`using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data.OleDb;

    public partial class Registration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {

            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["UsersConnectionString"].ConnectionString);
                conn.Open();

                string insertQuery = "insert into login (first_name,last_name) values (@first_name,@last_name)";

                SqlCommand com = new SqlCommand(insertQuery, conn);
                com.Parameters.AddWithValue("@first_name", TextBoxFirstName.Text);
                com.Parameters.AddWithValue("@last_name", TextBoxLastName.Text);


                com.ExecuteNonQuery();
                Response.Redirect("UsersGridView.aspx");
                Response.Write("Registration successful");
                conn.Close();
            }

            catch (Exception ex)
            {
                Response.Write("Error: " + ex.ToString());
            }
    }
}

配置文件:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <add name="LoginConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lou\Documents\BeautyExpertsDB.accdb" providerName="System.Data.OleDb"/>
    <add name="UsersConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lou\Documents\BeautyExpertsDB.accdb" providerName="System.Data.OleDb"/>
    <add name="Users1ConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\BeautyExpertsDatabase.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Web.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
  </appSettings>
</configuration>

1 个答案:

答案 0 :(得分:1)

您正在使用SqlConnection访问Ole因此错误。更改您的C#代码以使用OleDbConnection和相关命令,即OleDbCommand,它应该按预期工作。

using (OleDbConnection connection = new OleDbConnection("Your Connection String"))
{
   //Do something here 
}

进一步阅读here