访问连接字符串问题

时间:2014-07-30 10:56:06

标签: c# asp.net database connection

我刚刚创建了一个简单的访问连接和一个简单的DataAdaptor和数据集,以获取粘贴到gridview中的相关信息。

我得到的唯一问题是它说没有错误但会显示此消息并且无法正常运行:

"An OLE DB Provider was not specified in the ConnectionString.  An example would be, 'Provider=SQLOLEDB;'."

这是我的代码:

public partial class Database : System.Web.UI.Page
{
   public OleDbConnection cn=new OleDbConnection(@"Data Source =Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\jSte\Desktop\database_1.mdb; Integrated Security=true; User Instance =true");
   protected void Page_Load(object sender, EventArgs e)
   {
    OleDbDataAdapter ad = new OleDbDataAdapter ("SELECT * from CustomerDetails", cn);
    DataSet ds = new DataSet();
    ad.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
   }

3 个答案:

答案 0 :(得分:0)

您的连接字符串错误。您已多次指定Data Source属性。试试这个:

public OleDbConnection cn=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\jSte\Desktop\database_1.mdb; Integrated Security=true; User Instance =true");

答案 1 :(得分:0)

你有两个Data Source,这应该可行

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\jSte\Desktop\database_1.mdb; Integrated Security=true; User Instance=true

以下是一些例子:

https://www.connectionstrings.com/access/

您还可以使用OleDbConnectionStringBuilder创建连接字符串。

答案 2 :(得分:0)

您可以尝试这种方式。

public partial class Database : System.Web.UI.Page
{
   public OleDbConnection cn=new OleDbConnection(@"
                           Provider=Microsoft.Jet.OLEDB.4.0;
                           Data Source=C:\Documents and 
                           Settings\jSte\Desktop\database_1.mdb; 
                           Integrated Security=true; User Instance =true; 
                           Persist Security Info=True");  
   protected void Page_Load(object sender, EventArgs e)
   {
      OleDbCommand cmd = new OleDbCommand("SELECT * from CustomerDetails", cn);
      OleDbDataAdapter ad = new OleDbDataAdapter (cmd);
      DataSet ds = new DataSet();
      ad.Fill(ds);
      GridView1.DataSource = ds;
      GridView1.DataBind();
   }
 }

请参阅here以获得更多说明。