【问题标题】:ASP.NET I can't get my datasource/gridview to function since switching from SQL to Access DBASP.NET 从 SQL 切换到 Access DB 后,我无法让我的数据源/gridview 正常工作
【发布时间】:2017-03-02 09:15:32
【问题描述】:

我有一个即将到期的学校项目,它是一个 ASP.NET 中的网络应用程序,我认为我已经完成并使用本地 MySQL 数据库按预期工作。我的教授最近指示我们将我们的数据库更改为 MS Access,我已经成功地将所有转换都作为我的课程之一。有问题的类从我的 DB 表中获取注册的人,并将其列在网格视图中,并带有过滤器并将 DOB 转换为年龄。我将包含我的代码以及错误的屏幕截图。环顾四周,我发现您可以配置 SqlDataSource 以使用访问数据库,因此我按照步骤操作,这是我现在遇到的错误; '在应用程序配置中找不到连接名称'newregDBConnectionString.System.Data.OleDb'或连接字符串为空。'

谁能帮我看看我哪里出错了?提前致谢!

我的 Web.config;

<add name="newregDBConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
 Data Source=C:\Users\Alex\Documents\Database1.accdb"
 providerName="System.Data.OleDb" />

我为使用访问数据库配置 SqlDataSource 所遵循的步骤;

<asp:SqlDataSource 
ID="source"
runat ="server" 
ConnectionString ="<%$ ConnectionStrings:newregDBConnectionString %>"
ProviderName ="<%$ ConnectionStrings:newregDBConnectionString.System.Data.OleDb %>"
SelectCommand= "SELECT firstname, childID, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM children ORDER BY age"   />

我试图在其中显示 gridview 的 C# 代码;

namespace Coursework
{
public partial class Testy1 : System.Web.UI.Page
{
    //create a datasource
    SqlDataSource source = new SqlDataSource();     

    protected void Page_Load(object sender, EventArgs e)
    {
        //always set some defaults for the datasource
        source.ID = "source1";
        source.ConnectionString = ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionStr‌ing;
        source.SelectCommand = "SELECT firstname, childID, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM children ORDER BY age";

        if (!IsPostBack)
        {
            //bind the grid
            GridView1.DataSource = source;
            GridView1.DataBind();
        }
    }       
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //the new database query, now with where clause
        source.SelectCommand = "SELECT firstname, childID, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM children WHERE (DATEDIFF(hour, dob, GETDATE()) / 8766 BETWEEN @start AND @end) ORDER BY age";

        //get the end age from the dropdown and cast as int
        int end = Convert.ToInt32(DropDownList1.SelectedValue);

        //get the start int for the filter
        int start = end - 2;

        //if the filter is resetted, make sure the query returns all ages
        if (end == 5)
        {
            start = 5;
            end = 99;
        }

        //replace the parameters in the query
        source.SelectParameters.Add("start", start.ToString());
        source.SelectParameters.Add("end", end.ToString());

        //rebind the grid
        GridView1.DataSource = source;
        GridView1.DataBind();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string childID = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string deleteSql = "DELETE FROM Children WHERE childID = @childID; ";
        using (var con = new OleDbConnection(ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionStr‌​ing))
        using (var cmd = new OleDbCommand(deleteSql, con))
        {
            cmd.Parameters.Add("@childID", OleDbType.VarChar).Value = childID;
            con.Open();
            int deleted = cmd.ExecuteNonQuery();
        }

        GridView1.DataSource = source;
        GridView1.DataBind();
    }

    protected void backBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx");
    }
}

}

【问题讨论】:

  • 你试过&lt;asp:AccessDataSource ID="source" runat="server" DataFile="[Access DB file path]" ... /&gt;吗? SqlDataSource 用于 SQL Server,其中 Access DB 应使用 AccessDataSource
  • 嗨@TetsuyaYamamoto,我尝试了你的建议,但现在出现错误; “异常详细信息:System.InvalidOperationException:无法设置 AccessDataSource ConnectionString 属性,它是自动生成的。”我正在关注本指南“msdn.microsoft.com/en-us/library/hktw939c(v=vs.85).aspx”,它向我展示了如何将 SqlDataSource 与访问数据库一起使用。我不知道还能尝试什么。
  • 我忘了告诉你不能在AccessDataSource中使用ConnectionString属性,它应该使用DataFile和DB文件路径。还要检查 SqlDataSource 与 Access DB 的使用情况:msdn.microsoft.com/en-us/library/hktw939c(v=vs.85).aspx
  • 啊,谢谢!但是那篇文章和我上面链接的那篇文章是一样的,我已经在使用了:)。文章中的底部框显示以下示例; 这与我最初遵循的示例相同,您可以在我上面链接的第二个 sn-p 代码中看到。
  • @TetsuyaYamamoto 和您的方式都不是(更改为访问数据源,也不是文章方式有效)。当我将其更改为 accessdatasource 时,删除连接字符串并添加 DataFile=DBfilepath 我然后收到此错误;异常详细信息:System.InvalidOperationException:在应用程序配置中找不到连接名称“newregDBConnectionString.System.Data.OleDb”或连接字符串为空..

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


【解决方案1】:

SqlDataSource 中的ProviderName 属性值格式错误:

ProviderName="<%$ ConnectionStrings:newregDBConnectionString.System.Data.OleDb %>"

MSDN documentationConnectionStrings:newregDBConnectionString.providerName数据源绑定中的providerName属性引用web.config连接字符串元素中的providerName属性值。因此,您应该保持 providerName 属性不变:

<asp:SqlDataSource ID="source" runat="server" 
ConnectionString="<%$ ConnectionStrings:newregDBConnectionString %>"
ProviderName="<%$ ConnectionStrings:newregDBConnectionString.providerName %>"
SelectCommand="SELECT firstname, childID, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM children ORDER BY age" />

或直接使用提供者名称:

ProviderName="System.Data.OleDb"

相关问题(查看providerName 属性如何以类似方式使用):

ASP .NET - Configure SQLDataSource to use MySQL .NET Connector

【讨论】:

  • 感谢您迄今为止的所有帮助。我对您提供的数据源使用了代码的 sn-p,但现在出现错误; System.Data.dll 中出现“System.ArgumentException”类型的异常,但未在用户代码中处理其他信息:不支持关键字:“提供者”。此错误指向; “GridView1.DataBind();”代码
猜你喜欢
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 2013-06-27
  • 1970-01-01
  • 2020-12-10
  • 2011-06-03
相关资源
最近更新 更多